LCOV - code coverage report
Current view: top level - src/pl/plpgsql/src - pl_gram.y (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 89.9 % 1403 1261
Test Date: 2026-11-08 11:15:54 Functions: 93.3 % 30 28
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 71.6 % 838 600

             Branch data     Line data    Source code
       1                 :             : %{
       2                 :             : /*-------------------------------------------------------------------------
       3                 :             :  *
       4                 :             :  * pl_gram.y            - Parser for the PL/pgSQL procedural language
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *    src/pl/plpgsql/src/pl_gram.y
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : 
      16                 :             : #include "postgres.h"
      17                 :             : 
      18                 :             : #include "catalog/namespace.h"
      19                 :             : #include "catalog/pg_proc.h"
      20                 :             : #include "catalog/pg_type.h"
      21                 :             : #include "parser/parser.h"
      22                 :             : #include "parser/parse_type.h"
      23                 :             : #include "parser/scanner.h"
      24                 :             : #include "parser/scansup.h"
      25                 :             : #include "utils/builtins.h"
      26                 :             : 
      27                 :             : #include "plpgsql.h"
      28                 :             : 
      29                 :             : #include "pl_gram.h"
      30                 :             : 
      31                 :             : /* Location tracking support --- simpler than bison's default */
      32                 :             : #define YYLLOC_DEFAULT(Current, Rhs, N) \
      33                 :             :     do { \
      34                 :             :         if (N) \
      35                 :             :             (Current) = (Rhs)[1]; \
      36                 :             :         else \
      37                 :             :             (Current) = (Rhs)[0]; \
      38                 :             :     } while (0)
      39                 :             : 
      40                 :             : /*
      41                 :             :  * Bison doesn't allocate anything that needs to live across parser calls,
      42                 :             :  * so we can easily have it use palloc instead of malloc.  This prevents
      43                 :             :  * memory leaks if we error out during parsing.
      44                 :             :  */
      45                 :             : #define YYMALLOC palloc
      46                 :             : #define YYFREE   pfree
      47                 :             : 
      48                 :             : 
      49                 :             : typedef struct
      50                 :             : {
      51                 :             :     int         location;
      52                 :             :     yyscan_t    yyscanner;
      53                 :             : } sql_error_callback_arg;
      54                 :             : 
      55                 :             : #define parser_errposition(pos)  plpgsql_scanner_errposition(pos, yyscanner)
      56                 :             : 
      57                 :             : union YYSTYPE;                  /* need forward reference for tok_is_keyword */
      58                 :             : 
      59                 :             : static  bool            tok_is_keyword(int token, union YYSTYPE *lval,
      60                 :             :                                        int kw_token, const char *kw_str);
      61                 :             : static  void            word_is_not_variable(PLword *word, int location, yyscan_t yyscanner);
      62                 :             : static  void            cword_is_not_variable(PLcword *cword, int location, yyscan_t yyscanner);
      63                 :             : static  void            current_token_is_not_variable(int tok, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      64                 :             : static  PLpgSQL_expr    *make_plpgsql_expr(const char *query,
      65                 :             :                                            RawParseMode parsemode);
      66                 :             : static  void            mark_expr_as_assignment_source(PLpgSQL_expr *expr,
      67                 :             :                                                        PLpgSQL_datum *target);
      68                 :             : static  PLpgSQL_expr    *read_sql_construct(int until,
      69                 :             :                                             int until2,
      70                 :             :                                             int until3,
      71                 :             :                                             const char *expected,
      72                 :             :                                             RawParseMode parsemode,
      73                 :             :                                             bool isexpression,
      74                 :             :                                             bool valid_sql,
      75                 :             :                                             int *startloc,
      76                 :             :                                             int *endtoken,
      77                 :             :                                             YYSTYPE *yylvalp, YYLTYPE *yyllocp,
      78                 :             :                                             yyscan_t yyscanner);
      79                 :             : static  PLpgSQL_expr    *read_sql_expression(int until, const char *expected,
      80                 :             :                                              YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      81                 :             : static  PLpgSQL_expr    *read_sql_expression2(int until, int until2,
      82                 :             :                                               const char *expected, int *endtoken,
      83                 :             :                                               YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      84                 :             : static  PLpgSQL_expr    *read_sql_stmt(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      85                 :             : static  PLpgSQL_type    *read_datatype(int tok, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      86                 :             : static  PLpgSQL_stmt    *make_execsql_stmt(int firsttoken, int location,
      87                 :             :                                            PLword *word, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      88                 :             : static  PLpgSQL_stmt_fetch *read_fetch_direction(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      89                 :             : static  void             complete_direction(PLpgSQL_stmt_fetch *fetch,
      90                 :             :                                             bool *check_FROM, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      91                 :             : static  PLpgSQL_stmt    *make_return_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      92                 :             : static  PLpgSQL_stmt    *make_return_next_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      93                 :             : static  PLpgSQL_stmt    *make_return_query_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
      94                 :             : static  PLpgSQL_stmt    *make_case(int location, PLpgSQL_expr *t_expr,
      95                 :             :                                    List *case_when_list, List *else_stmts, yyscan_t yyscanner);
      96                 :             : static  char            *NameOfDatum(PLwdatum *wdatum);
      97                 :             : static  void             check_assignable(PLpgSQL_datum *datum, int location, yyscan_t yyscanner);
      98                 :             : static  void             read_into_target(PLpgSQL_variable **target, bool *strict,
      99                 :             :                                           YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
     100                 :             : static  PLpgSQL_row     *read_into_scalar_list(char *initial_name,
     101                 :             :                                                PLpgSQL_datum *initial_datum,
     102                 :             :                                                int initial_location,
     103                 :             :                                                YYSTYPE *yylvalp, YYLTYPE *yyllocp,
     104                 :             :                                                yyscan_t yyscanner);
     105                 :             : static  PLpgSQL_row     *make_scalar_list1(char *initial_name,
     106                 :             :                                            PLpgSQL_datum *initial_datum,
     107                 :             :                                            int lineno, int location, yyscan_t yyscanner);
     108                 :             : static  void             check_sql_expr(const char *stmt,
     109                 :             :                                         RawParseMode parseMode, int location, yyscan_t yyscanner);
     110                 :             : static  void             plpgsql_sql_error_callback(void *arg);
     111                 :             : static  PLpgSQL_type    *parse_datatype(const char *string, int location, yyscan_t yyscanner);
     112                 :             : static  void             check_labels(const char *start_label,
     113                 :             :                                       const char *end_label,
     114                 :             :                                       int end_location,
     115                 :             :                                       yyscan_t yyscanner);
     116                 :             : static  PLpgSQL_expr    *read_cursor_args(PLpgSQL_var *cursor, int until,
     117                 :             :                                           YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
     118                 :             : static  List            *read_raise_options(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner);
     119                 :             : static  void            check_raise_parameters(PLpgSQL_stmt_raise *stmt);
     120                 :             : 
     121                 :             : %}
     122                 :             : 
     123                 :             : %parse-param {PLpgSQL_stmt_block **plpgsql_parse_result_p}
     124                 :             : %parse-param {yyscan_t yyscanner}
     125                 :             : %lex-param   {yyscan_t yyscanner}
     126                 :             : %pure-parser
     127                 :             : %expect 0
     128                 :             : %name-prefix="plpgsql_yy"
     129                 :             : %locations
     130                 :             : 
     131                 :             : %union
     132                 :             : {
     133                 :             :     core_YYSTYPE core_yystype;
     134                 :             :     /* these fields must match core_YYSTYPE: */
     135                 :             :     int         ival;
     136                 :             :     char       *str;
     137                 :             :     const char *keyword;
     138                 :             : 
     139                 :             :     PLword      word;
     140                 :             :     PLcword     cword;
     141                 :             :     PLwdatum    wdatum;
     142                 :             :     bool        boolean;
     143                 :             :     Oid         oid;
     144                 :             :     struct
     145                 :             :     {
     146                 :             :         char       *name;
     147                 :             :         int         lineno;
     148                 :             :     }           varname;
     149                 :             :     struct
     150                 :             :     {
     151                 :             :         char       *name;
     152                 :             :         int         lineno;
     153                 :             :         PLpgSQL_datum *scalar;
     154                 :             :         PLpgSQL_datum *row;
     155                 :             :     }           forvariable;
     156                 :             :     struct
     157                 :             :     {
     158                 :             :         char       *label;
     159                 :             :         int         n_initvars;
     160                 :             :         int        *initvarnos;
     161                 :             :     }           declhdr;
     162                 :             :     struct
     163                 :             :     {
     164                 :             :         List       *stmts;
     165                 :             :         char       *end_label;
     166                 :             :         int         end_label_location;
     167                 :             :     }           loop_body;
     168                 :             :     List       *list;
     169                 :             :     PLpgSQL_type *dtype;
     170                 :             :     PLpgSQL_datum *datum;
     171                 :             :     PLpgSQL_var *var;
     172                 :             :     PLpgSQL_expr *expr;
     173                 :             :     PLpgSQL_stmt *stmt;
     174                 :             :     PLpgSQL_condition *condition;
     175                 :             :     PLpgSQL_exception *exception;
     176                 :             :     PLpgSQL_exception_block *exception_block;
     177                 :             :     PLpgSQL_nsitem *nsitem;
     178                 :             :     PLpgSQL_diag_item *diagitem;
     179                 :             :     PLpgSQL_stmt_fetch *fetch;
     180                 :             :     PLpgSQL_case_when *casewhen;
     181                 :             : }
     182                 :             : 
     183                 :             : %type <declhdr> decl_sect
     184                 :             : %type <varname> decl_varname
     185                 :             : %type <boolean>   decl_const decl_notnull exit_type
     186                 :             : %type <expr>  decl_defval decl_cursor_query
     187                 :             : %type <dtype> decl_datatype
     188                 :             : %type <oid>       decl_collate
     189                 :             : %type <datum> decl_cursor_args
     190                 :             : %type <list>  decl_cursor_arglist
     191                 :             : %type <nsitem>    decl_aliasitem
     192                 :             : 
     193                 :             : %type <expr>  expr_until_semi
     194                 :             : %type <expr>  expr_until_then expr_until_loop opt_expr_until_when
     195                 :             : %type <expr>  opt_exitcond
     196                 :             : 
     197                 :             : %type <var>       cursor_variable
     198                 :             : %type <datum> decl_cursor_arg
     199                 :             : %type <forvariable>   for_variable
     200                 :             : %type <ival>  foreach_slice
     201                 :             : %type <stmt>  for_control
     202                 :             : 
     203                 :             : %type <str>       any_identifier opt_block_label opt_loop_label opt_label
     204                 :             : %type <str>       option_value
     205                 :             : 
     206                 :             : %type <list>  proc_sect stmt_elsifs stmt_else
     207                 :             : %type <loop_body> loop_body
     208                 :             : %type <stmt>  proc_stmt pl_block
     209                 :             : %type <stmt>  stmt_assign stmt_if stmt_loop stmt_while stmt_exit
     210                 :             : %type <stmt>  stmt_return stmt_raise stmt_assert stmt_execsql
     211                 :             : %type <stmt>  stmt_dynexecute stmt_for stmt_perform stmt_call stmt_getdiag
     212                 :             : %type <stmt>  stmt_open stmt_fetch stmt_move stmt_close stmt_null
     213                 :             : %type <stmt>  stmt_commit stmt_rollback
     214                 :             : %type <stmt>  stmt_case stmt_foreach_a
     215                 :             : 
     216                 :             : %type <list>  proc_exceptions
     217                 :             : %type <exception_block> exception_sect
     218                 :             : %type <exception> proc_exception
     219                 :             : %type <condition> proc_conditions proc_condition
     220                 :             : 
     221                 :             : %type <casewhen>  case_when
     222                 :             : %type <list>  case_when_list opt_case_else
     223                 :             : 
     224                 :             : %type <boolean>   getdiag_area_opt
     225                 :             : %type <list>  getdiag_list
     226                 :             : %type <diagitem> getdiag_list_item
     227                 :             : %type <datum> getdiag_target
     228                 :             : %type <ival>  getdiag_item
     229                 :             : 
     230                 :             : %type <ival>  opt_scrollable
     231                 :             : %type <fetch> opt_fetch_direction
     232                 :             : 
     233                 :             : %type <ival>  opt_transaction_chain
     234                 :             : 
     235                 :             : %type <keyword>   unreserved_keyword
     236                 :             : 
     237                 :             : 
     238                 :             : /*
     239                 :             :  * Basic non-keyword token types.  These are hard-wired into the core lexer.
     240                 :             :  * They must be listed first so that their numeric codes do not depend on
     241                 :             :  * the set of keywords.  Keep this list in sync with backend/parser/gram.y!
     242                 :             :  *
     243                 :             :  * Some of these are not directly referenced in this file, but they must be
     244                 :             :  * here anyway.
     245                 :             :  */
     246                 :             : %token <str>  IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op
     247                 :             : %token <ival> ICONST PARAM
     248                 :             : %token          TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
     249                 :             : %token          LESS_EQUALS GREATER_EQUALS NOT_EQUALS
     250                 :             : 
     251                 :             : /*
     252                 :             :  * Other tokens recognized by plpgsql's lexer interface layer (pl_scanner.c).
     253                 :             :  */
     254                 :             : %token <word>     T_WORD      /* unrecognized simple identifier */
     255                 :             : %token <cword>        T_CWORD     /* unrecognized composite identifier */
     256                 :             : %token <wdatum>       T_DATUM     /* a VAR, ROW, REC, or RECFIELD variable */
     257                 :             : %token              LESS_LESS
     258                 :             : %token              GREATER_GREATER
     259                 :             : 
     260                 :             : /*
     261                 :             :  * Keyword tokens.  Some of these are reserved and some are not;
     262                 :             :  * see pl_scanner.c for info.  Be sure unreserved keywords are listed
     263                 :             :  * in the "unreserved_keyword" production below.
     264                 :             :  */
     265                 :             : %token <keyword>  K_ABSOLUTE
     266                 :             : %token <keyword>  K_ALIAS
     267                 :             : %token <keyword>  K_ALL
     268                 :             : %token <keyword>  K_AND
     269                 :             : %token <keyword>  K_ARRAY
     270                 :             : %token <keyword>  K_ASSERT
     271                 :             : %token <keyword>  K_BACKWARD
     272                 :             : %token <keyword>  K_BEGIN
     273                 :             : %token <keyword>  K_BY
     274                 :             : %token <keyword>  K_CALL
     275                 :             : %token <keyword>  K_CASE
     276                 :             : %token <keyword>  K_CHAIN
     277                 :             : %token <keyword>  K_CLOSE
     278                 :             : %token <keyword>  K_COLLATE
     279                 :             : %token <keyword>  K_COLUMN
     280                 :             : %token <keyword>  K_COLUMN_NAME
     281                 :             : %token <keyword>  K_COMMIT
     282                 :             : %token <keyword>  K_CONSTANT
     283                 :             : %token <keyword>  K_CONSTRAINT
     284                 :             : %token <keyword>  K_CONSTRAINT_NAME
     285                 :             : %token <keyword>  K_CONTINUE
     286                 :             : %token <keyword>  K_CURRENT
     287                 :             : %token <keyword>  K_CURSOR
     288                 :             : %token <keyword>  K_DATATYPE
     289                 :             : %token <keyword>  K_DEBUG
     290                 :             : %token <keyword>  K_DECLARE
     291                 :             : %token <keyword>  K_DEFAULT
     292                 :             : %token <keyword>  K_DETAIL
     293                 :             : %token <keyword>  K_DIAGNOSTICS
     294                 :             : %token <keyword>  K_DO
     295                 :             : %token <keyword>  K_DUMP
     296                 :             : %token <keyword>  K_ELSE
     297                 :             : %token <keyword>  K_ELSIF
     298                 :             : %token <keyword>  K_END
     299                 :             : %token <keyword>  K_ERRCODE
     300                 :             : %token <keyword>  K_ERROR
     301                 :             : %token <keyword>  K_EXCEPTION
     302                 :             : %token <keyword>  K_EXECUTE
     303                 :             : %token <keyword>  K_EXIT
     304                 :             : %token <keyword>  K_FETCH
     305                 :             : %token <keyword>  K_FIRST
     306                 :             : %token <keyword>  K_FOR
     307                 :             : %token <keyword>  K_FOREACH
     308                 :             : %token <keyword>  K_FORWARD
     309                 :             : %token <keyword>  K_FROM
     310                 :             : %token <keyword>  K_GET
     311                 :             : %token <keyword>  K_HINT
     312                 :             : %token <keyword>  K_IF
     313                 :             : %token <keyword>  K_IMPORT
     314                 :             : %token <keyword>  K_IN
     315                 :             : %token <keyword>  K_INFO
     316                 :             : %token <keyword>  K_INSERT
     317                 :             : %token <keyword>  K_INTO
     318                 :             : %token <keyword>  K_IS
     319                 :             : %token <keyword>  K_LAST
     320                 :             : %token <keyword>  K_LOG
     321                 :             : %token <keyword>  K_LOOP
     322                 :             : %token <keyword>  K_MERGE
     323                 :             : %token <keyword>  K_MESSAGE
     324                 :             : %token <keyword>  K_MESSAGE_TEXT
     325                 :             : %token <keyword>  K_MOVE
     326                 :             : %token <keyword>  K_NEXT
     327                 :             : %token <keyword>  K_NO
     328                 :             : %token <keyword>  K_NOT
     329                 :             : %token <keyword>  K_NOTICE
     330                 :             : %token <keyword>  K_NULL
     331                 :             : %token <keyword>  K_OPEN
     332                 :             : %token <keyword>  K_OPTION
     333                 :             : %token <keyword>  K_OR
     334                 :             : %token <keyword>  K_PERFORM
     335                 :             : %token <keyword>  K_PG_CONTEXT
     336                 :             : %token <keyword>  K_PG_DATATYPE_NAME
     337                 :             : %token <keyword>  K_PG_EXCEPTION_CONTEXT
     338                 :             : %token <keyword>  K_PG_EXCEPTION_DETAIL
     339                 :             : %token <keyword>  K_PG_EXCEPTION_HINT
     340                 :             : %token <keyword>  K_PG_ROUTINE_OID
     341                 :             : %token <keyword>  K_PRINT_STRICT_PARAMS
     342                 :             : %token <keyword>  K_PRIOR
     343                 :             : %token <keyword>  K_QUERY
     344                 :             : %token <keyword>  K_RAISE
     345                 :             : %token <keyword>  K_RELATIVE
     346                 :             : %token <keyword>  K_RETURN
     347                 :             : %token <keyword>  K_RETURNED_SQLSTATE
     348                 :             : %token <keyword>  K_REVERSE
     349                 :             : %token <keyword>  K_ROLLBACK
     350                 :             : %token <keyword>  K_ROW_COUNT
     351                 :             : %token <keyword>  K_ROWTYPE
     352                 :             : %token <keyword>  K_SCHEMA
     353                 :             : %token <keyword>  K_SCHEMA_NAME
     354                 :             : %token <keyword>  K_SCROLL
     355                 :             : %token <keyword>  K_SLICE
     356                 :             : %token <keyword>  K_SQLSTATE
     357                 :             : %token <keyword>  K_STACKED
     358                 :             : %token <keyword>  K_STRICT
     359                 :             : %token <keyword>  K_TABLE
     360                 :             : %token <keyword>  K_TABLE_NAME
     361                 :             : %token <keyword>  K_THEN
     362                 :             : %token <keyword>  K_TO
     363                 :             : %token <keyword>  K_TYPE
     364                 :             : %token <keyword>  K_USE_COLUMN
     365                 :             : %token <keyword>  K_USE_VARIABLE
     366                 :             : %token <keyword>  K_USING
     367                 :             : %token <keyword>  K_VARIABLE_CONFLICT
     368                 :             : %token <keyword>  K_WARNING
     369                 :             : %token <keyword>  K_WHEN
     370                 :             : %token <keyword>  K_WHILE
     371                 :             : 
     372                 :             : %%
     373                 :             : 
     374                 :             : pl_function     : comp_options pl_block opt_semi
     375                 :             :                     {
     376                 :        5917 :                         *plpgsql_parse_result_p = (PLpgSQL_stmt_block *) $2;
     377                 :             :                         (void) yynerrs;     /* suppress compiler warning */
     378                 :             :                     }
     379                 :             :                 ;
     380                 :             : 
     381                 :             : comp_options    :
     382                 :             :                 | comp_options comp_option
     383                 :             :                 ;
     384                 :             : 
     385                 :             : comp_option     : '#' K_OPTION K_DUMP
     386                 :             :                     {
     387                 :           0 :                         plpgsql_DumpExecTree = true;
     388                 :             :                     }
     389                 :             :                 | '#' K_PRINT_STRICT_PARAMS option_value
     390                 :             :                     {
     391         [ +  + ]:           8 :                         if (strcmp($3, "on") == 0)
     392                 :           4 :                             plpgsql_curr_compile->print_strict_params = true;
     393         [ +  - ]:           4 :                         else if (strcmp($3, "off") == 0)
     394                 :           4 :                             plpgsql_curr_compile->print_strict_params = false;
     395                 :             :                         else
     396         [ #  # ]:           0 :                             ereport(ERROR,
     397                 :             :                                     (errcode(ERRCODE_SYNTAX_ERROR),
     398                 :             :                                      errmsg("unrecognized %s option \"%s\"", "print_strict_params", $3)));
     399                 :             :                     }
     400                 :             :                 | '#' K_VARIABLE_CONFLICT K_ERROR
     401                 :             :                     {
     402                 :           0 :                         plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_ERROR;
     403                 :             :                     }
     404                 :             :                 | '#' K_VARIABLE_CONFLICT K_USE_VARIABLE
     405                 :             :                     {
     406                 :           4 :                         plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_VARIABLE;
     407                 :             :                     }
     408                 :             :                 | '#' K_VARIABLE_CONFLICT K_USE_COLUMN
     409                 :             :                     {
     410                 :           4 :                         plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_COLUMN;
     411                 :             :                     }
     412                 :             :                 ;
     413                 :             : 
     414                 :             : option_value : T_WORD
     415                 :             :                 {
     416                 :           8 :                     $$ = $1.ident;
     417                 :             :                 }
     418                 :             :              | unreserved_keyword
     419                 :             :                 {
     420                 :           0 :                     $$ = pstrdup($1);
     421                 :             :                 }
     422                 :             : 
     423                 :             : opt_semi        :
     424                 :             :                 | ';'
     425                 :             :                 ;
     426                 :             : 
     427                 :             : pl_block        : decl_sect K_BEGIN proc_sect exception_sect K_END opt_label
     428                 :             :                     {
     429                 :             :                         PLpgSQL_stmt_block *new;
     430                 :             : 
     431                 :        6074 :                         new = palloc0_object(PLpgSQL_stmt_block);
     432                 :             : 
     433                 :        6074 :                         new->cmd_type    = PLPGSQL_STMT_BLOCK;
     434                 :        6074 :                         new->lineno      = plpgsql_location_to_lineno(@2, yyscanner);
     435                 :        6074 :                         new->stmtid      = ++plpgsql_curr_compile->nstatements;
     436                 :        6074 :                         new->label       = $1.label;
     437                 :        6074 :                         new->n_initvars = $1.n_initvars;
     438                 :        6074 :                         new->initvarnos = $1.initvarnos;
     439                 :        6074 :                         new->body        = $3;
     440                 :        6074 :                         new->exceptions  = $4;
     441                 :             : 
     442                 :        6074 :                         check_labels($1.label, $6, @6, yyscanner);
     443                 :        6074 :                         plpgsql_ns_pop();
     444                 :             : 
     445                 :        6074 :                         $$ = (PLpgSQL_stmt *) new;
     446                 :             :                     }
     447                 :             :                 ;
     448                 :             : 
     449                 :             : 
     450                 :             : decl_sect       : opt_block_label
     451                 :             :                     {
     452                 :             :                         /* done with decls, so resume identifier lookup */
     453                 :        3926 :                         plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
     454                 :        3926 :                         $$.label      = $1;
     455                 :        3926 :                         $$.n_initvars = 0;
     456                 :        3926 :                         $$.initvarnos = NULL;
     457                 :             :                     }
     458                 :             :                 | opt_block_label decl_start
     459                 :             :                     {
     460                 :           6 :                         plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
     461                 :           6 :                         $$.label      = $1;
     462                 :           6 :                         $$.n_initvars = 0;
     463                 :           6 :                         $$.initvarnos = NULL;
     464                 :             :                     }
     465                 :             :                 | opt_block_label decl_start decl_stmts
     466                 :             :                     {
     467                 :        2222 :                         plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
     468                 :        2222 :                         $$.label      = $1;
     469                 :             :                         /* Remember variables declared in decl_stmts */
     470                 :        2222 :                         $$.n_initvars = plpgsql_add_initdatums(&($$.initvarnos));
     471                 :             :                     }
     472                 :             :                 ;
     473                 :             : 
     474                 :             : decl_start      : K_DECLARE
     475                 :             :                     {
     476                 :             :                         /* Forget any variables created before block */
     477                 :        2250 :                         plpgsql_add_initdatums(NULL);
     478                 :             :                         /*
     479                 :             :                          * Disable scanner lookup of identifiers while
     480                 :             :                          * we process the decl_stmts
     481                 :             :                          */
     482                 :        2250 :                         plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
     483                 :             :                     }
     484                 :             :                 ;
     485                 :             : 
     486                 :             : decl_stmts      : decl_stmts decl_stmt
     487                 :             :                 | decl_stmt
     488                 :             :                 ;
     489                 :             : 
     490                 :             : decl_stmt       : decl_statement
     491                 :             :                 | K_DECLARE
     492                 :             :                     {
     493                 :             :                         /* We allow useless extra DECLAREs */
     494                 :             :                     }
     495                 :             :                 | LESS_LESS any_identifier GREATER_GREATER
     496                 :             :                     {
     497                 :             :                         /*
     498                 :             :                          * Throw a helpful error if user tries to put block
     499                 :             :                          * label just before BEGIN, instead of before DECLARE.
     500                 :             :                          */
     501         [ #  # ]:           0 :                         ereport(ERROR,
     502                 :             :                                 (errcode(ERRCODE_SYNTAX_ERROR),
     503                 :             :                                  errmsg("block label must be placed before DECLARE, not after"),
     504                 :             :                                  parser_errposition(@1)));
     505                 :             :                     }
     506                 :             :                 ;
     507                 :             : 
     508                 :             : decl_statement  : decl_varname decl_const decl_datatype decl_collate decl_notnull decl_defval
     509                 :             :                     {
     510                 :             :                         PLpgSQL_variable    *var;
     511                 :             : 
     512                 :             :                         /*
     513                 :             :                          * If a collation is supplied, insert it into the
     514                 :             :                          * datatype.  We assume decl_datatype always returns
     515                 :             :                          * a freshly built struct not shared with other
     516                 :             :                          * variables.
     517                 :             :                          */
     518         [ +  + ]:        3590 :                         if (OidIsValid($4))
     519                 :             :                         {
     520         [ -  + ]:          16 :                             if (!OidIsValid($3->collation))
     521         [ #  # ]:           0 :                                 ereport(ERROR,
     522                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     523                 :             :                                          errmsg("collations are not supported by type %s",
     524                 :             :                                                 format_type_be($3->typoid)),
     525                 :             :                                          parser_errposition(@4)));
     526                 :          16 :                             $3->collation = $4;
     527                 :             :                         }
     528                 :             : 
     529                 :        3590 :                         var = plpgsql_build_variable($1.name, $1.lineno,
     530                 :        3590 :                                                      $3, true);
     531                 :        3588 :                         var->isconst = $2;
     532                 :        3588 :                         var->notnull = $5;
     533                 :        3588 :                         var->default_val = $6;
     534                 :             : 
     535                 :             :                         /*
     536                 :             :                          * The combination of NOT NULL without an initializer
     537                 :             :                          * can't work, so let's reject it at compile time.
     538                 :             :                          */
     539   [ +  +  +  + ]:        3588 :                         if (var->notnull && var->default_val == NULL)
     540         [ +  - ]:           3 :                             ereport(ERROR,
     541                 :             :                                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
     542                 :             :                                      errmsg("variable \"%s\" must have a default value, since it's declared NOT NULL",
     543                 :             :                                             var->refname),
     544                 :             :                                      parser_errposition(@5)));
     545                 :             : 
     546         [ +  + ]:        3585 :                         if (var->default_val != NULL)
     547                 :         529 :                             mark_expr_as_assignment_source(var->default_val,
     548                 :             :                                                            (PLpgSQL_datum *) var);
     549                 :             :                     }
     550                 :             :                 | decl_varname K_ALIAS K_FOR decl_aliasitem ';'
     551                 :             :                     {
     552                 :          56 :                         plpgsql_ns_additem($4->itemtype,
     553                 :          56 :                                            $4->itemno, $1.name);
     554                 :             :                     }
     555                 :             :                 | decl_varname opt_scrollable K_CURSOR
     556                 :          81 :                     { plpgsql_ns_push($1.name, PLPGSQL_LABEL_OTHER); }
     557                 :             :                   decl_cursor_args decl_is_for decl_cursor_query
     558                 :             :                     {
     559                 :             :                         PLpgSQL_var *new;
     560                 :             : 
     561                 :             :                         /* pop local namespace for cursor args */
     562                 :          81 :                         plpgsql_ns_pop();
     563                 :             : 
     564                 :             :                         new = (PLpgSQL_var *)
     565                 :          81 :                             plpgsql_build_variable($1.name, $1.lineno,
     566                 :             :                                                    plpgsql_build_datatype(REFCURSOROID,
     567                 :             :                                                                           -1,
     568                 :             :                                                                           InvalidOid,
     569                 :             :                                                                           NULL),
     570                 :             :                                                    true);
     571                 :             : 
     572                 :          81 :                         new->cursor_explicit_expr = $7;
     573         [ +  + ]:          81 :                         if ($5 == NULL)
     574                 :          33 :                             new->cursor_explicit_argrow = -1;
     575                 :             :                         else
     576                 :          48 :                             new->cursor_explicit_argrow = $5->dno;
     577                 :          81 :                         new->cursor_options = CURSOR_OPT_FAST_PLAN | $2;
     578                 :             :                     }
     579                 :             :                 ;
     580                 :             : 
     581                 :             : opt_scrollable :
     582                 :             :                     {
     583                 :          73 :                         $$ = 0;
     584                 :             :                     }
     585                 :             :                 | K_NO K_SCROLL
     586                 :             :                     {
     587                 :           4 :                         $$ = CURSOR_OPT_NO_SCROLL;
     588                 :             :                     }
     589                 :             :                 | K_SCROLL
     590                 :             :                     {
     591                 :           4 :                         $$ = CURSOR_OPT_SCROLL;
     592                 :             :                     }
     593                 :             :                 ;
     594                 :             : 
     595                 :             : decl_cursor_query :
     596                 :             :                     {
     597                 :          81 :                         $$ = read_sql_stmt(&yylval, &yylloc, yyscanner);
     598                 :             :                     }
     599                 :             :                 ;
     600                 :             : 
     601                 :             : decl_cursor_args :
     602                 :             :                     {
     603                 :          33 :                         $$ = NULL;
     604                 :             :                     }
     605                 :             :                 | '(' decl_cursor_arglist ')'
     606                 :             :                     {
     607                 :             :                         PLpgSQL_row *new;
     608                 :             :                         int         i;
     609                 :             :                         ListCell   *l;
     610                 :             : 
     611                 :          48 :                         new = palloc0_object(PLpgSQL_row);
     612                 :          48 :                         new->dtype = PLPGSQL_DTYPE_ROW;
     613                 :          48 :                         new->refname = "(unnamed row)";
     614                 :          48 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
     615                 :          48 :                         new->rowtupdesc = NULL;
     616                 :          48 :                         new->nfields = list_length($2);
     617                 :          48 :                         new->fieldnames = palloc_array(char *, new->nfields);
     618                 :          48 :                         new->varnos = palloc_array(int, new->nfields);
     619                 :             : 
     620                 :          48 :                         i = 0;
     621   [ +  -  +  +  :         144 :                         foreach (l, $2)
                   +  + ]
     622                 :             :                         {
     623                 :          96 :                             PLpgSQL_variable *arg = (PLpgSQL_variable *) lfirst(l);
     624                 :             :                             Assert(!arg->isconst);
     625                 :          96 :                             new->fieldnames[i] = arg->refname;
     626                 :          96 :                             new->varnos[i] = arg->dno;
     627                 :          96 :                             i++;
     628                 :             :                         }
     629                 :          48 :                         list_free($2);
     630                 :             : 
     631                 :          48 :                         plpgsql_adddatum((PLpgSQL_datum *) new);
     632                 :          48 :                         $$ = (PLpgSQL_datum *) new;
     633                 :             :                     }
     634                 :             :                 ;
     635                 :             : 
     636                 :             : decl_cursor_arglist : decl_cursor_arg
     637                 :             :                     {
     638                 :          48 :                         $$ = list_make1($1);
     639                 :             :                     }
     640                 :             :                 | decl_cursor_arglist ',' decl_cursor_arg
     641                 :             :                     {
     642                 :          48 :                         $$ = lappend($1, $3);
     643                 :             :                     }
     644                 :             :                 ;
     645                 :             : 
     646                 :             : decl_cursor_arg : decl_varname decl_datatype
     647                 :             :                     {
     648                 :          96 :                         $$ = (PLpgSQL_datum *)
     649                 :          96 :                             plpgsql_build_variable($1.name, $1.lineno,
     650                 :             :                                                    $2, true);
     651                 :             :                     }
     652                 :             :                 ;
     653                 :             : 
     654                 :             : decl_is_for     :   K_IS |      /* Oracle */
     655                 :             :                     K_FOR;      /* SQL standard */
     656                 :             : 
     657                 :             : decl_aliasitem  : T_WORD
     658                 :             :                     {
     659                 :             :                         PLpgSQL_nsitem *nsi;
     660                 :             : 
     661                 :          56 :                         nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
     662                 :          56 :                                                 $1.ident, NULL, NULL,
     663                 :             :                                                 NULL);
     664         [ -  + ]:          56 :                         if (nsi == NULL)
     665         [ #  # ]:           0 :                             ereport(ERROR,
     666                 :             :                                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     667                 :             :                                      errmsg("variable \"%s\" does not exist",
     668                 :             :                                             $1.ident),
     669                 :             :                                      parser_errposition(@1)));
     670                 :          56 :                         $$ = nsi;
     671                 :             :                     }
     672                 :             :                 | unreserved_keyword
     673                 :             :                     {
     674                 :             :                         PLpgSQL_nsitem *nsi;
     675                 :             : 
     676                 :           0 :                         nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
     677                 :             :                                                 $1, NULL, NULL,
     678                 :             :                                                 NULL);
     679         [ #  # ]:           0 :                         if (nsi == NULL)
     680         [ #  # ]:           0 :                             ereport(ERROR,
     681                 :             :                                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     682                 :             :                                      errmsg("variable \"%s\" does not exist",
     683                 :             :                                             $1),
     684                 :             :                                      parser_errposition(@1)));
     685                 :           0 :                         $$ = nsi;
     686                 :             :                     }
     687                 :             :                 | T_CWORD
     688                 :             :                     {
     689                 :             :                         PLpgSQL_nsitem *nsi;
     690                 :             : 
     691         [ #  # ]:           0 :                         if (list_length($1.idents) == 2)
     692                 :           0 :                             nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
     693                 :           0 :                                                     strVal(linitial($1.idents)),
     694                 :           0 :                                                     strVal(lsecond($1.idents)),
     695                 :             :                                                     NULL,
     696                 :             :                                                     NULL);
     697         [ #  # ]:           0 :                         else if (list_length($1.idents) == 3)
     698                 :           0 :                             nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
     699                 :           0 :                                                     strVal(linitial($1.idents)),
     700                 :           0 :                                                     strVal(lsecond($1.idents)),
     701                 :           0 :                                                     strVal(lthird($1.idents)),
     702                 :             :                                                     NULL);
     703                 :             :                         else
     704                 :           0 :                             nsi = NULL;
     705         [ #  # ]:           0 :                         if (nsi == NULL)
     706         [ #  # ]:           0 :                             ereport(ERROR,
     707                 :             :                                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     708                 :             :                                      errmsg("variable \"%s\" does not exist",
     709                 :             :                                             NameListToString($1.idents)),
     710                 :             :                                      parser_errposition(@1)));
     711                 :           0 :                         $$ = nsi;
     712                 :             :                     }
     713                 :             :                 ;
     714                 :             : 
     715                 :             : decl_varname    : T_WORD
     716                 :             :                     {
     717                 :        3820 :                         $$.name = $1.ident;
     718                 :        3820 :                         $$.lineno = plpgsql_location_to_lineno(@1, yyscanner);
     719                 :             :                         /*
     720                 :             :                          * Check to make sure name isn't already declared
     721                 :             :                          * in the current block.
     722                 :             :                          */
     723         [ -  + ]:        3820 :                         if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
     724                 :        3820 :                                               $1.ident, NULL, NULL,
     725                 :             :                                               NULL) != NULL)
     726                 :           0 :                             yyerror(&yylloc, NULL, yyscanner, "duplicate declaration");
     727                 :             : 
     728         [ +  + ]:        3820 :                         if (plpgsql_curr_compile->extra_warnings & PLPGSQL_XCHECK_SHADOWVAR ||
     729         [ -  + ]:        3772 :                             plpgsql_curr_compile->extra_errors & PLPGSQL_XCHECK_SHADOWVAR)
     730                 :             :                         {
     731                 :             :                             PLpgSQL_nsitem *nsi;
     732                 :          48 :                             nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
     733                 :          48 :                                                     $1.ident, NULL, NULL, NULL);
     734         [ +  + ]:          48 :                             if (nsi != NULL)
     735   [ +  +  +  - ]:          36 :                                 ereport(plpgsql_curr_compile->extra_errors & PLPGSQL_XCHECK_SHADOWVAR ? ERROR : WARNING,
     736                 :             :                                         (errcode(ERRCODE_DUPLICATE_ALIAS),
     737                 :             :                                          errmsg("variable \"%s\" shadows a previously defined variable",
     738                 :             :                                                 $1.ident),
     739                 :             :                                          parser_errposition(@1)));
     740                 :             :                         }
     741                 :             : 
     742                 :             :                     }
     743                 :             :                 | unreserved_keyword
     744                 :             :                     {
     745                 :          20 :                         $$.name = pstrdup($1);
     746                 :          20 :                         $$.lineno = plpgsql_location_to_lineno(@1, yyscanner);
     747                 :             :                         /*
     748                 :             :                          * Check to make sure name isn't already declared
     749                 :             :                          * in the current block.
     750                 :             :                          */
     751         [ -  + ]:          20 :                         if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
     752                 :             :                                               $1, NULL, NULL,
     753                 :             :                                               NULL) != NULL)
     754                 :           0 :                             yyerror(&yylloc, NULL, yyscanner, "duplicate declaration");
     755                 :             : 
     756         [ +  - ]:          20 :                         if (plpgsql_curr_compile->extra_warnings & PLPGSQL_XCHECK_SHADOWVAR ||
     757         [ -  + ]:          20 :                             plpgsql_curr_compile->extra_errors & PLPGSQL_XCHECK_SHADOWVAR)
     758                 :             :                         {
     759                 :             :                             PLpgSQL_nsitem *nsi;
     760                 :           0 :                             nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
     761                 :             :                                                     $1, NULL, NULL, NULL);
     762         [ #  # ]:           0 :                             if (nsi != NULL)
     763   [ #  #  #  # ]:           0 :                                 ereport(plpgsql_curr_compile->extra_errors & PLPGSQL_XCHECK_SHADOWVAR ? ERROR : WARNING,
     764                 :             :                                         (errcode(ERRCODE_DUPLICATE_ALIAS),
     765                 :             :                                          errmsg("variable \"%s\" shadows a previously defined variable",
     766                 :             :                                                 $1),
     767                 :             :                                          parser_errposition(@1)));
     768                 :             :                         }
     769                 :             : 
     770                 :             :                     }
     771                 :             :                 ;
     772                 :             : 
     773                 :             : decl_const      :
     774                 :        3587 :                     { $$ = false; }
     775                 :             :                 | K_CONSTANT
     776                 :          16 :                     { $$ = true; }
     777                 :             :                 ;
     778                 :             : 
     779                 :             : decl_datatype   :
     780                 :             :                     {
     781                 :             :                         /*
     782                 :             :                          * If there's a lookahead token, read_datatype() will
     783                 :             :                          * consume it, and then we must tell bison to forget
     784                 :             :                          * it.
     785                 :             :                          */
     786                 :        3699 :                         $$ = read_datatype(yychar, &yylval, &yylloc, yyscanner);
     787                 :        3686 :                         yyclearin;
     788                 :             :                     }
     789                 :             :                 ;
     790                 :             : 
     791                 :             : decl_collate    :
     792                 :        3574 :                     { $$ = InvalidOid; }
     793                 :             :                 | K_COLLATE T_WORD
     794                 :             :                     {
     795                 :          16 :                         $$ = get_collation_oid(list_make1(makeString($2.ident)),
     796                 :             :                                                false);
     797                 :             :                     }
     798                 :             :                 | K_COLLATE unreserved_keyword
     799                 :             :                     {
     800                 :           0 :                         $$ = get_collation_oid(list_make1(makeString(pstrdup($2))),
     801                 :             :                                                false);
     802                 :             :                     }
     803                 :             :                 | K_COLLATE T_CWORD
     804                 :             :                     {
     805                 :           0 :                         $$ = get_collation_oid($2.idents, false);
     806                 :             :                     }
     807                 :             :                 ;
     808                 :             : 
     809                 :             : decl_notnull    :
     810                 :        3577 :                     { $$ = false; }
     811                 :             :                 | K_NOT K_NULL
     812                 :          13 :                     { $$ = true; }
     813                 :             :                 ;
     814                 :             : 
     815                 :             : decl_defval     : ';'
     816                 :        3061 :                     { $$ = NULL; }
     817                 :             :                 | decl_defkey
     818                 :             :                     {
     819                 :         529 :                         $$ = read_sql_expression(';', ";", &yylval, &yylloc, yyscanner);
     820                 :             :                     }
     821                 :             :                 ;
     822                 :             : 
     823                 :             : decl_defkey     : assign_operator
     824                 :             :                 | K_DEFAULT
     825                 :             :                 ;
     826                 :             : 
     827                 :             : /*
     828                 :             :  * Ada-based PL/SQL uses := for assignment and variable defaults, while
     829                 :             :  * the SQL standard uses equals for these cases and for GET
     830                 :             :  * DIAGNOSTICS, so we support both.  FOR and OPEN only support :=.
     831                 :             :  */
     832                 :             : assign_operator : '='
     833                 :             :                 | COLON_EQUALS
     834                 :             :                 ;
     835                 :             : 
     836                 :             : proc_sect       :
     837                 :       12677 :                     { $$ = NIL; }
     838                 :             :                 | proc_sect proc_stmt
     839                 :             :                     {
     840                 :             :                         /* don't bother linking null statements into list */
     841         [ +  + ]:       24306 :                         if ($2 == NULL)
     842                 :          29 :                             $$ = $1;
     843                 :             :                         else
     844                 :       24277 :                             $$ = lappend($1, $2);
     845                 :             :                     }
     846                 :             :                 ;
     847                 :             : 
     848                 :             : proc_stmt       : pl_block ';'
     849                 :         157 :                         { $$ = $1; }
     850                 :             :                 | stmt_assign
     851                 :        4502 :                         { $$ = $1; }
     852                 :             :                 | stmt_if
     853                 :        3791 :                         { $$ = $1; }
     854                 :             :                 | stmt_case
     855                 :          11 :                         { $$ = $1; }
     856                 :             :                 | stmt_loop
     857                 :          54 :                         { $$ = $1; }
     858                 :             :                 | stmt_while
     859                 :          51 :                         { $$ = $1; }
     860                 :             :                 | stmt_for
     861                 :         933 :                         { $$ = $1; }
     862                 :             :                 | stmt_foreach_a
     863                 :          41 :                         { $$ = $1; }
     864                 :             :                 | stmt_exit
     865                 :         136 :                         { $$ = $1; }
     866                 :             :                 | stmt_return
     867                 :        5839 :                         { $$ = $1; }
     868                 :             :                 | stmt_raise
     869                 :        4287 :                         { $$ = $1; }
     870                 :             :                 | stmt_assert
     871                 :          35 :                         { $$ = $1; }
     872                 :             :                 | stmt_execsql
     873                 :        2570 :                         { $$ = $1; }
     874                 :             :                 | stmt_dynexecute
     875                 :         651 :                         { $$ = $1; }
     876                 :             :                 | stmt_perform
     877                 :         789 :                         { $$ = $1; }
     878                 :             :                 | stmt_call
     879                 :          54 :                         { $$ = $1; }
     880                 :             :                 | stmt_getdiag
     881                 :          90 :                         { $$ = $1; }
     882                 :             :                 | stmt_open
     883                 :          77 :                         { $$ = $1; }
     884                 :             :                 | stmt_fetch
     885                 :          81 :                         { $$ = $1; }
     886                 :             :                 | stmt_move
     887                 :          12 :                         { $$ = $1; }
     888                 :             :                 | stmt_close
     889                 :          44 :                         { $$ = $1; }
     890                 :             :                 | stmt_null
     891                 :          29 :                         { $$ = $1; }
     892                 :             :                 | stmt_commit
     893                 :          39 :                         { $$ = $1; }
     894                 :             :                 | stmt_rollback
     895                 :          33 :                         { $$ = $1; }
     896                 :             :                 ;
     897                 :             : 
     898                 :             : stmt_perform    : K_PERFORM
     899                 :             :                     {
     900                 :             :                         PLpgSQL_stmt_perform *new;
     901                 :             :                         int         startloc;
     902                 :             : 
     903                 :         789 :                         new = palloc0_object(PLpgSQL_stmt_perform);
     904                 :         789 :                         new->cmd_type = PLPGSQL_STMT_PERFORM;
     905                 :         789 :                         new->lineno   = plpgsql_location_to_lineno(@1, yyscanner);
     906                 :         789 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
     907                 :         789 :                         plpgsql_push_back_token(K_PERFORM, &yylval, &yylloc, yyscanner);
     908                 :             : 
     909                 :             :                         /*
     910                 :             :                          * Since PERFORM isn't legal SQL, we have to cheat to
     911                 :             :                          * the extent of substituting "SELECT" for "PERFORM"
     912                 :             :                          * in the parsed text.  It does not seem worth
     913                 :             :                          * inventing a separate parse mode for this one case.
     914                 :             :                          * We can't do syntax-checking until after we make the
     915                 :             :                          * substitution.
     916                 :             :                          */
     917                 :         789 :                         new->expr = read_sql_construct(';', 0, 0, ";",
     918                 :             :                                                        RAW_PARSE_DEFAULT,
     919                 :             :                                                        false, false,
     920                 :             :                                                        &startloc, NULL,
     921                 :             :                                                        &yylval, &yylloc, yyscanner);
     922                 :             :                         /* overwrite "perform" ... */
     923                 :         789 :                         memcpy(new->expr->query, " SELECT", 7);
     924                 :             :                         /* left-justify to get rid of the leading space */
     925                 :         789 :                         memmove(new->expr->query, new->expr->query + 1,
     926                 :         789 :                                 strlen(new->expr->query));
     927                 :             :                         /* offset syntax error position to account for that */
     928                 :         789 :                         check_sql_expr(new->expr->query, new->expr->parseMode,
     929                 :             :                                        startloc + 1, yyscanner);
     930                 :             : 
     931                 :         789 :                         $$ = (PLpgSQL_stmt *) new;
     932                 :             :                     }
     933                 :             :                 ;
     934                 :             : 
     935                 :             : stmt_call       : K_CALL
     936                 :             :                     {
     937                 :             :                         PLpgSQL_stmt_call *new;
     938                 :             : 
     939                 :          53 :                         new = palloc0_object(PLpgSQL_stmt_call);
     940                 :          53 :                         new->cmd_type = PLPGSQL_STMT_CALL;
     941                 :          53 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
     942                 :          53 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
     943                 :          53 :                         plpgsql_push_back_token(K_CALL, &yylval, &yylloc, yyscanner);
     944                 :          53 :                         new->expr = read_sql_stmt(&yylval, &yylloc, yyscanner);
     945                 :          53 :                         new->is_call = true;
     946                 :             : 
     947                 :             :                         /* Remember we may need a procedure resource owner */
     948                 :          53 :                         plpgsql_curr_compile->requires_procedure_resowner = true;
     949                 :             : 
     950                 :          53 :                         $$ = (PLpgSQL_stmt *) new;
     951                 :             : 
     952                 :             :                     }
     953                 :             :                 | K_DO
     954                 :             :                     {
     955                 :             :                         /* use the same structures as for CALL, for simplicity */
     956                 :             :                         PLpgSQL_stmt_call *new;
     957                 :             : 
     958                 :           1 :                         new = palloc0_object(PLpgSQL_stmt_call);
     959                 :           1 :                         new->cmd_type = PLPGSQL_STMT_CALL;
     960                 :           1 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
     961                 :           1 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
     962                 :           1 :                         plpgsql_push_back_token(K_DO, &yylval, &yylloc, yyscanner);
     963                 :           1 :                         new->expr = read_sql_stmt(&yylval, &yylloc, yyscanner);
     964                 :           1 :                         new->is_call = false;
     965                 :             : 
     966                 :             :                         /* Remember we may need a procedure resource owner */
     967                 :           1 :                         plpgsql_curr_compile->requires_procedure_resowner = true;
     968                 :             : 
     969                 :           1 :                         $$ = (PLpgSQL_stmt *) new;
     970                 :             : 
     971                 :             :                     }
     972                 :             :                 ;
     973                 :             : 
     974                 :             : stmt_assign     : T_DATUM
     975                 :             :                     {
     976                 :             :                         PLpgSQL_stmt_assign *new;
     977                 :             :                         RawParseMode pmode;
     978                 :             : 
     979                 :             :                         /* see how many names identify the datum */
     980   [ +  +  +  +  :        4506 :                         switch ($1.ident ? 1 : list_length($1.idents))
                   +  - ]
     981                 :             :                         {
     982                 :        4068 :                             case 1:
     983                 :        4068 :                                 pmode = RAW_PARSE_PLPGSQL_ASSIGN1;
     984                 :        4068 :                                 break;
     985                 :         431 :                             case 2:
     986                 :         431 :                                 pmode = RAW_PARSE_PLPGSQL_ASSIGN2;
     987                 :         431 :                                 break;
     988                 :           7 :                             case 3:
     989                 :           7 :                                 pmode = RAW_PARSE_PLPGSQL_ASSIGN3;
     990                 :           7 :                                 break;
     991                 :           0 :                             default:
     992         [ #  # ]:           0 :                                 elog(ERROR, "unexpected number of names");
     993                 :             :                                 pmode = 0; /* keep compiler quiet */
     994                 :             :                         }
     995                 :             : 
     996                 :        4506 :                         check_assignable($1.datum, @1, yyscanner);
     997                 :        4503 :                         new = palloc0_object(PLpgSQL_stmt_assign);
     998                 :        4503 :                         new->cmd_type = PLPGSQL_STMT_ASSIGN;
     999                 :        4503 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    1000                 :        4503 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    1001                 :        4503 :                         new->varno = $1.datum->dno;
    1002                 :             :                         /* Push back the head name to include it in the stmt */
    1003                 :        4503 :                         plpgsql_push_back_token(T_DATUM, &yylval, &yylloc, yyscanner);
    1004                 :        4503 :                         new->expr = read_sql_construct(';', 0, 0, ";",
    1005                 :             :                                                        pmode,
    1006                 :             :                                                        false, true,
    1007                 :             :                                                        NULL, NULL,
    1008                 :             :                                                        &yylval, &yylloc, yyscanner);
    1009                 :        4502 :                         mark_expr_as_assignment_source(new->expr, $1.datum);
    1010                 :             : 
    1011                 :        4502 :                         $$ = (PLpgSQL_stmt *) new;
    1012                 :             :                     }
    1013                 :             :                 ;
    1014                 :             : 
    1015                 :             : stmt_getdiag    : K_GET getdiag_area_opt K_DIAGNOSTICS getdiag_list ';'
    1016                 :             :                     {
    1017                 :             :                         PLpgSQL_stmt_getdiag *new;
    1018                 :             :                         ListCell       *lc;
    1019                 :             : 
    1020                 :          90 :                         new = palloc0_object(PLpgSQL_stmt_getdiag);
    1021                 :          90 :                         new->cmd_type = PLPGSQL_STMT_GETDIAG;
    1022                 :          90 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    1023                 :          90 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    1024                 :          90 :                         new->is_stacked = $2;
    1025                 :          90 :                         new->diag_items = $4;
    1026                 :             : 
    1027                 :             :                         /*
    1028                 :             :                          * Check information items are valid for area option.
    1029                 :             :                          */
    1030   [ +  -  +  +  :         228 :                         foreach(lc, new->diag_items)
                   +  + ]
    1031                 :             :                         {
    1032                 :         138 :                             PLpgSQL_diag_item *ditem = (PLpgSQL_diag_item *) lfirst(lc);
    1033                 :             : 
    1034   [ +  +  +  - ]:         138 :                             switch (ditem->kind)
    1035                 :             :                             {
    1036                 :             :                                 /* these fields are disallowed in stacked case */
    1037                 :          56 :                                 case PLPGSQL_GETDIAG_ROW_COUNT:
    1038                 :             :                                 case PLPGSQL_GETDIAG_ROUTINE_OID:
    1039         [ -  + ]:          56 :                                     if (new->is_stacked)
    1040         [ #  # ]:           0 :                                         ereport(ERROR,
    1041                 :             :                                                 (errcode(ERRCODE_SYNTAX_ERROR),
    1042                 :             :                                                  errmsg("diagnostics item %s is not allowed in GET STACKED DIAGNOSTICS",
    1043                 :             :                                                         plpgsql_getdiag_kindname(ditem->kind)),
    1044                 :             :                                                  parser_errposition(@1)));
    1045                 :          56 :                                     break;
    1046                 :             :                                 /* these fields are disallowed in current case */
    1047                 :          66 :                                 case PLPGSQL_GETDIAG_ERROR_CONTEXT:
    1048                 :             :                                 case PLPGSQL_GETDIAG_ERROR_DETAIL:
    1049                 :             :                                 case PLPGSQL_GETDIAG_ERROR_HINT:
    1050                 :             :                                 case PLPGSQL_GETDIAG_RETURNED_SQLSTATE:
    1051                 :             :                                 case PLPGSQL_GETDIAG_COLUMN_NAME:
    1052                 :             :                                 case PLPGSQL_GETDIAG_CONSTRAINT_NAME:
    1053                 :             :                                 case PLPGSQL_GETDIAG_DATATYPE_NAME:
    1054                 :             :                                 case PLPGSQL_GETDIAG_MESSAGE_TEXT:
    1055                 :             :                                 case PLPGSQL_GETDIAG_TABLE_NAME:
    1056                 :             :                                 case PLPGSQL_GETDIAG_SCHEMA_NAME:
    1057         [ -  + ]:          66 :                                     if (!new->is_stacked)
    1058         [ #  # ]:           0 :                                         ereport(ERROR,
    1059                 :             :                                                 (errcode(ERRCODE_SYNTAX_ERROR),
    1060                 :             :                                                  errmsg("diagnostics item %s is not allowed in GET CURRENT DIAGNOSTICS",
    1061                 :             :                                                         plpgsql_getdiag_kindname(ditem->kind)),
    1062                 :             :                                                  parser_errposition(@1)));
    1063                 :          66 :                                     break;
    1064                 :             :                                 /* these fields are allowed in either case */
    1065                 :          16 :                                 case PLPGSQL_GETDIAG_CONTEXT:
    1066                 :          16 :                                     break;
    1067                 :           0 :                                 default:
    1068         [ #  # ]:           0 :                                     elog(ERROR, "unrecognized diagnostic item kind: %d",
    1069                 :             :                                          ditem->kind);
    1070                 :             :                                     break;
    1071                 :             :                             }
    1072                 :             :                         }
    1073                 :             : 
    1074                 :          90 :                         $$ = (PLpgSQL_stmt *) new;
    1075                 :             :                     }
    1076                 :             :                 ;
    1077                 :             : 
    1078                 :             : getdiag_area_opt :
    1079                 :             :                     {
    1080                 :          76 :                         $$ = false;
    1081                 :             :                     }
    1082                 :             :                 | K_CURRENT
    1083                 :             :                     {
    1084                 :           0 :                         $$ = false;
    1085                 :             :                     }
    1086                 :             :                 | K_STACKED
    1087                 :             :                     {
    1088                 :          18 :                         $$ = true;
    1089                 :             :                     }
    1090                 :             :                 ;
    1091                 :             : 
    1092                 :             : getdiag_list : getdiag_list ',' getdiag_list_item
    1093                 :             :                     {
    1094                 :          48 :                         $$ = lappend($1, $3);
    1095                 :             :                     }
    1096                 :             :                 | getdiag_list_item
    1097                 :             :                     {
    1098                 :          90 :                         $$ = list_make1($1);
    1099                 :             :                     }
    1100                 :             :                 ;
    1101                 :             : 
    1102                 :             : getdiag_list_item : getdiag_target assign_operator getdiag_item
    1103                 :             :                     {
    1104                 :             :                         PLpgSQL_diag_item *new;
    1105                 :             : 
    1106                 :         138 :                         new = palloc_object(PLpgSQL_diag_item);
    1107                 :         138 :                         new->target = $1->dno;
    1108                 :         138 :                         new->kind = $3;
    1109                 :             : 
    1110                 :         138 :                         $$ = new;
    1111                 :             :                     }
    1112                 :             :                 ;
    1113                 :             : 
    1114                 :             : getdiag_item :
    1115                 :             :                     {
    1116                 :         138 :                         int         tok = yylex(&yylval, &yylloc, yyscanner);
    1117                 :             : 
    1118         [ +  + ]:         138 :                         if (tok_is_keyword(tok, &yylval,
    1119                 :             :                                            K_ROW_COUNT, "row_count"))
    1120                 :          44 :                             $$ = PLPGSQL_GETDIAG_ROW_COUNT;
    1121         [ +  + ]:          94 :                         else if (tok_is_keyword(tok, &yylval,
    1122                 :             :                                                 K_PG_ROUTINE_OID, "pg_routine_oid"))
    1123                 :          12 :                             $$ = PLPGSQL_GETDIAG_ROUTINE_OID;
    1124         [ +  + ]:          82 :                         else if (tok_is_keyword(tok, &yylval,
    1125                 :             :                                                 K_PG_CONTEXT, "pg_context"))
    1126                 :          16 :                             $$ = PLPGSQL_GETDIAG_CONTEXT;
    1127         [ +  + ]:          66 :                         else if (tok_is_keyword(tok, &yylval,
    1128                 :             :                                                 K_PG_EXCEPTION_DETAIL, "pg_exception_detail"))
    1129                 :          10 :                             $$ = PLPGSQL_GETDIAG_ERROR_DETAIL;
    1130         [ +  + ]:          56 :                         else if (tok_is_keyword(tok, &yylval,
    1131                 :             :                                                 K_PG_EXCEPTION_HINT, "pg_exception_hint"))
    1132                 :           9 :                             $$ = PLPGSQL_GETDIAG_ERROR_HINT;
    1133         [ +  + ]:          47 :                         else if (tok_is_keyword(tok, &yylval,
    1134                 :             :                                                 K_PG_EXCEPTION_CONTEXT, "pg_exception_context"))
    1135                 :           4 :                             $$ = PLPGSQL_GETDIAG_ERROR_CONTEXT;
    1136         [ +  + ]:          43 :                         else if (tok_is_keyword(tok, &yylval,
    1137                 :             :                                                 K_COLUMN_NAME, "column_name"))
    1138                 :           5 :                             $$ = PLPGSQL_GETDIAG_COLUMN_NAME;
    1139         [ +  + ]:          38 :                         else if (tok_is_keyword(tok, &yylval,
    1140                 :             :                                                 K_CONSTRAINT_NAME, "constraint_name"))
    1141                 :           5 :                             $$ = PLPGSQL_GETDIAG_CONSTRAINT_NAME;
    1142         [ +  + ]:          33 :                         else if (tok_is_keyword(tok, &yylval,
    1143                 :             :                                                 K_PG_DATATYPE_NAME, "pg_datatype_name"))
    1144                 :           5 :                             $$ = PLPGSQL_GETDIAG_DATATYPE_NAME;
    1145         [ +  + ]:          28 :                         else if (tok_is_keyword(tok, &yylval,
    1146                 :             :                                                 K_MESSAGE_TEXT, "message_text"))
    1147                 :          13 :                             $$ = PLPGSQL_GETDIAG_MESSAGE_TEXT;
    1148         [ +  + ]:          15 :                         else if (tok_is_keyword(tok, &yylval,
    1149                 :             :                                                 K_TABLE_NAME, "table_name"))
    1150                 :           5 :                             $$ = PLPGSQL_GETDIAG_TABLE_NAME;
    1151         [ +  + ]:          10 :                         else if (tok_is_keyword(tok, &yylval,
    1152                 :             :                                                 K_SCHEMA_NAME, "schema_name"))
    1153                 :           5 :                             $$ = PLPGSQL_GETDIAG_SCHEMA_NAME;
    1154         [ +  - ]:           5 :                         else if (tok_is_keyword(tok, &yylval,
    1155                 :             :                                                 K_RETURNED_SQLSTATE, "returned_sqlstate"))
    1156                 :           5 :                             $$ = PLPGSQL_GETDIAG_RETURNED_SQLSTATE;
    1157                 :             :                         else
    1158                 :           0 :                             yyerror(&yylloc, NULL, yyscanner, "unrecognized GET DIAGNOSTICS item");
    1159                 :             :                     }
    1160                 :             :                 ;
    1161                 :             : 
    1162                 :             : getdiag_target  : T_DATUM
    1163                 :             :                     {
    1164                 :             :                         /*
    1165                 :             :                          * In principle we should support a getdiag_target
    1166                 :             :                          * that is an array element, but for now we don't, so
    1167                 :             :                          * just throw an error if next token is '['.
    1168                 :             :                          */
    1169         [ +  - ]:         142 :                         if ($1.datum->dtype == PLPGSQL_DTYPE_ROW ||
    1170   [ +  +  -  + ]:         280 :                             $1.datum->dtype == PLPGSQL_DTYPE_REC ||
    1171                 :         138 :                             plpgsql_peek(yyscanner) == '[')
    1172         [ +  - ]:           4 :                             ereport(ERROR,
    1173                 :             :                                     (errcode(ERRCODE_SYNTAX_ERROR),
    1174                 :             :                                      errmsg("\"%s\" is not a scalar variable",
    1175                 :             :                                             NameOfDatum(&($1))),
    1176                 :             :                                      parser_errposition(@1)));
    1177                 :         138 :                         check_assignable($1.datum, @1, yyscanner);
    1178                 :         138 :                         $$ = $1.datum;
    1179                 :             :                     }
    1180                 :             :                 | T_WORD
    1181                 :             :                     {
    1182                 :             :                         /* just to give a better message than "syntax error" */
    1183                 :           0 :                         word_is_not_variable(&($1), @1, yyscanner);
    1184                 :             :                     }
    1185                 :             :                 | T_CWORD
    1186                 :             :                     {
    1187                 :             :                         /* just to give a better message than "syntax error" */
    1188                 :           0 :                         cword_is_not_variable(&($1), @1, yyscanner);
    1189                 :             :                     }
    1190                 :             :                 ;
    1191                 :             : 
    1192                 :             : stmt_if         : K_IF expr_until_then proc_sect stmt_elsifs stmt_else K_END K_IF ';'
    1193                 :             :                     {
    1194                 :             :                         PLpgSQL_stmt_if *new;
    1195                 :             : 
    1196                 :        3791 :                         new = palloc0_object(PLpgSQL_stmt_if);
    1197                 :        3791 :                         new->cmd_type = PLPGSQL_STMT_IF;
    1198                 :        3791 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    1199                 :        3791 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    1200                 :        3791 :                         new->cond = $2;
    1201                 :        3791 :                         new->then_body = $3;
    1202                 :        3791 :                         new->elsif_list = $4;
    1203                 :        3791 :                         new->else_body = $5;
    1204                 :             : 
    1205                 :        3791 :                         $$ = (PLpgSQL_stmt *) new;
    1206                 :             :                     }
    1207                 :             :                 ;
    1208                 :             : 
    1209                 :             : stmt_elsifs     :
    1210                 :             :                     {
    1211                 :        3791 :                         $$ = NIL;
    1212                 :             :                     }
    1213                 :             :                 | stmt_elsifs K_ELSIF expr_until_then proc_sect
    1214                 :             :                     {
    1215                 :             :                         PLpgSQL_if_elsif *new;
    1216                 :             : 
    1217                 :         643 :                         new = palloc0_object(PLpgSQL_if_elsif);
    1218                 :         643 :                         new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
    1219                 :         643 :                         new->cond = $3;
    1220                 :         643 :                         new->stmts = $4;
    1221                 :             : 
    1222                 :         643 :                         $$ = lappend($1, new);
    1223                 :             :                     }
    1224                 :             :                 ;
    1225                 :             : 
    1226                 :             : stmt_else       :
    1227                 :             :                     {
    1228                 :        3118 :                         $$ = NIL;
    1229                 :             :                     }
    1230                 :             :                 | K_ELSE proc_sect
    1231                 :             :                     {
    1232                 :         673 :                         $$ = $2;
    1233                 :             :                     }
    1234                 :             :                 ;
    1235                 :             : 
    1236                 :             : stmt_case       : K_CASE opt_expr_until_when case_when_list opt_case_else K_END K_CASE ';'
    1237                 :             :                     {
    1238                 :          11 :                         $$ = make_case(@1, $2, $3, $4, yyscanner);
    1239                 :             :                     }
    1240                 :             :                 ;
    1241                 :             : 
    1242                 :             : opt_expr_until_when :
    1243                 :             :                     {
    1244                 :          11 :                         PLpgSQL_expr *expr = NULL;
    1245                 :          11 :                         int         tok = yylex(&yylval, &yylloc, yyscanner);
    1246                 :             : 
    1247         [ +  + ]:          11 :                         if (tok != K_WHEN)
    1248                 :             :                         {
    1249                 :          10 :                             plpgsql_push_back_token(tok, &yylval, &yylloc, yyscanner);
    1250                 :          10 :                             expr = read_sql_expression(K_WHEN, "WHEN", &yylval, &yylloc, yyscanner);
    1251                 :             :                         }
    1252                 :          11 :                         plpgsql_push_back_token(K_WHEN, &yylval, &yylloc, yyscanner);
    1253                 :          11 :                         $$ = expr;
    1254                 :             :                     }
    1255                 :             :                 ;
    1256                 :             : 
    1257                 :             : case_when_list  : case_when_list case_when
    1258                 :             :                     {
    1259                 :          13 :                         $$ = lappend($1, $2);
    1260                 :             :                     }
    1261                 :             :                 | case_when
    1262                 :             :                     {
    1263                 :          11 :                         $$ = list_make1($1);
    1264                 :             :                     }
    1265                 :             :                 ;
    1266                 :             : 
    1267                 :             : case_when       : K_WHEN expr_until_then proc_sect
    1268                 :             :                     {
    1269                 :          24 :                         PLpgSQL_case_when *new = palloc_object(PLpgSQL_case_when);
    1270                 :             : 
    1271                 :          24 :                         new->lineno  = plpgsql_location_to_lineno(@1, yyscanner);
    1272                 :          24 :                         new->expr = $2;
    1273                 :          24 :                         new->stmts = $3;
    1274                 :          24 :                         $$ = new;
    1275                 :             :                     }
    1276                 :             :                 ;
    1277                 :             : 
    1278                 :             : opt_case_else   :
    1279                 :             :                     {
    1280                 :           9 :                         $$ = NIL;
    1281                 :             :                     }
    1282                 :             :                 | K_ELSE proc_sect
    1283                 :             :                     {
    1284                 :             :                         /*
    1285                 :             :                          * proc_sect could return an empty list, but we
    1286                 :             :                          * must distinguish that from not having ELSE at all.
    1287                 :             :                          * Simplest fix is to return a list with one NULL
    1288                 :             :                          * pointer, which make_case() must take care of.
    1289                 :             :                          */
    1290         [ +  - ]:           2 :                         if ($2 != NIL)
    1291                 :           2 :                             $$ = $2;
    1292                 :             :                         else
    1293                 :           0 :                             $$ = list_make1(NULL);
    1294                 :             :                     }
    1295                 :             :                 ;
    1296                 :             : 
    1297                 :             : stmt_loop       : opt_loop_label K_LOOP loop_body
    1298                 :             :                     {
    1299                 :             :                         PLpgSQL_stmt_loop *new;
    1300                 :             : 
    1301                 :          54 :                         new = palloc0_object(PLpgSQL_stmt_loop);
    1302                 :          54 :                         new->cmd_type = PLPGSQL_STMT_LOOP;
    1303                 :          54 :                         new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
    1304                 :          54 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    1305                 :          54 :                         new->label = $1;
    1306                 :          54 :                         new->body = $3.stmts;
    1307                 :             : 
    1308                 :          54 :                         check_labels($1, $3.end_label, $3.end_label_location, yyscanner);
    1309                 :          54 :                         plpgsql_ns_pop();
    1310                 :             : 
    1311                 :          54 :                         $$ = (PLpgSQL_stmt *) new;
    1312                 :             :                     }
    1313                 :             :                 ;
    1314                 :             : 
    1315                 :             : stmt_while      : opt_loop_label K_WHILE expr_until_loop loop_body
    1316                 :             :                     {
    1317                 :             :                         PLpgSQL_stmt_while *new;
    1318                 :             : 
    1319                 :          51 :                         new = palloc0_object(PLpgSQL_stmt_while);
    1320                 :          51 :                         new->cmd_type = PLPGSQL_STMT_WHILE;
    1321                 :          51 :                         new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
    1322                 :          51 :                         new->stmtid  = ++plpgsql_curr_compile->nstatements;
    1323                 :          51 :                         new->label = $1;
    1324                 :          51 :                         new->cond = $3;
    1325                 :          51 :                         new->body = $4.stmts;
    1326                 :             : 
    1327                 :          51 :                         check_labels($1, $4.end_label, $4.end_label_location, yyscanner);
    1328                 :          51 :                         plpgsql_ns_pop();
    1329                 :             : 
    1330                 :          51 :                         $$ = (PLpgSQL_stmt *) new;
    1331                 :             :                     }
    1332                 :             :                 ;
    1333                 :             : 
    1334                 :             : stmt_for        : opt_loop_label K_FOR for_control loop_body
    1335                 :             :                     {
    1336                 :             :                         /* This runs after we've scanned the loop body */
    1337         [ +  + ]:         936 :                         if ($3->cmd_type == PLPGSQL_STMT_FORI)
    1338                 :             :                         {
    1339                 :             :                             PLpgSQL_stmt_fori *new;
    1340                 :             : 
    1341                 :         462 :                             new = (PLpgSQL_stmt_fori *) $3;
    1342                 :         462 :                             new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
    1343                 :         462 :                             new->label = $1;
    1344                 :         462 :                             new->body = $4.stmts;
    1345                 :         462 :                             $$ = (PLpgSQL_stmt *) new;
    1346                 :             :                         }
    1347                 :             :                         else
    1348                 :             :                         {
    1349                 :             :                             PLpgSQL_stmt_forq *new;
    1350                 :             : 
    1351                 :             :                             Assert($3->cmd_type == PLPGSQL_STMT_FORS ||
    1352                 :             :                                    $3->cmd_type == PLPGSQL_STMT_FORC ||
    1353                 :             :                                    $3->cmd_type == PLPGSQL_STMT_DYNFORS);
    1354                 :             :                             /* forq is the common supertype of all three */
    1355                 :         474 :                             new = (PLpgSQL_stmt_forq *) $3;
    1356                 :         474 :                             new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
    1357                 :         474 :                             new->label = $1;
    1358                 :         474 :                             new->body = $4.stmts;
    1359                 :         474 :                             $$ = (PLpgSQL_stmt *) new;
    1360                 :             :                         }
    1361                 :             : 
    1362                 :         936 :                         check_labels($1, $4.end_label, $4.end_label_location, yyscanner);
    1363                 :             :                         /* close namespace started in opt_loop_label */
    1364                 :         933 :                         plpgsql_ns_pop();
    1365                 :             :                     }
    1366                 :             :                 ;
    1367                 :             : 
    1368                 :             : for_control     : for_variable K_IN
    1369                 :             :                     {
    1370                 :         944 :                         int         tok = yylex(&yylval, &yylloc, yyscanner);
    1371                 :         944 :                         int         tokloc = yylloc;
    1372                 :             : 
    1373         [ +  + ]:         944 :                         if (tok_is_keyword(tok, &yylval,
    1374                 :             :                                            K_EXECUTE, "execute"))
    1375                 :             :                         {
    1376                 :             :                             /* EXECUTE means it's a dynamic FOR loop */
    1377                 :             :                             PLpgSQL_stmt_dynfors *new;
    1378                 :             :                             PLpgSQL_expr *expr;
    1379                 :             :                             int         term;
    1380                 :             : 
    1381                 :         189 :                             expr = read_sql_expression2(K_LOOP, K_USING,
    1382                 :             :                                                         "LOOP or USING",
    1383                 :             :                                                         &term, &yylval, &yylloc, yyscanner);
    1384                 :             : 
    1385                 :         189 :                             new = palloc0_object(PLpgSQL_stmt_dynfors);
    1386                 :         189 :                             new->cmd_type = PLPGSQL_STMT_DYNFORS;
    1387                 :         189 :                             new->stmtid = ++plpgsql_curr_compile->nstatements;
    1388         [ +  + ]:         189 :                             if ($1.row)
    1389                 :             :                             {
    1390                 :          31 :                                 new->var = (PLpgSQL_variable *) $1.row;
    1391                 :          31 :                                 check_assignable($1.row, @1, yyscanner);
    1392                 :             :                             }
    1393         [ +  - ]:         158 :                             else if ($1.scalar)
    1394                 :             :                             {
    1395                 :             :                                 /* convert single scalar to list */
    1396                 :         158 :                                 new->var = (PLpgSQL_variable *)
    1397                 :         158 :                                     make_scalar_list1($1.name, $1.scalar,
    1398                 :         158 :                                                       $1.lineno, @1, yyscanner);
    1399                 :             :                                 /* make_scalar_list1 did check_assignable */
    1400                 :             :                             }
    1401                 :             :                             else
    1402                 :             :                             {
    1403         [ #  # ]:           0 :                                 ereport(ERROR,
    1404                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
    1405                 :             :                                          errmsg("loop variable of loop over rows must be a record variable or list of scalar variables"),
    1406                 :             :                                          parser_errposition(@1)));
    1407                 :             :                             }
    1408                 :         189 :                             new->query = expr;
    1409                 :             : 
    1410         [ +  + ]:         189 :                             if (term == K_USING)
    1411                 :             :                             {
    1412                 :             :                                 do
    1413                 :             :                                 {
    1414                 :           8 :                                     expr = read_sql_expression2(',', K_LOOP,
    1415                 :             :                                                                 ", or LOOP",
    1416                 :             :                                                                 &term, &yylval, &yylloc, yyscanner);
    1417                 :           8 :                                     new->params = lappend(new->params, expr);
    1418         [ -  + ]:           8 :                                 } while (term == ',');
    1419                 :             :                             }
    1420                 :             : 
    1421                 :         189 :                             $$ = (PLpgSQL_stmt *) new;
    1422                 :             :                         }
    1423         [ +  + ]:         755 :                         else if (tok == T_DATUM &&
    1424         [ +  - ]:          86 :                                  yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_VAR &&
    1425         [ +  + ]:          86 :                                  ((PLpgSQL_var *) yylval.wdatum.datum)->datatype->typoid == REFCURSOROID)
    1426                 :          33 :                         {
    1427                 :             :                             /* It's FOR var IN cursor */
    1428                 :             :                             PLpgSQL_stmt_forc *new;
    1429                 :          37 :                             PLpgSQL_var *cursor = (PLpgSQL_var *) yylval.wdatum.datum;
    1430                 :             : 
    1431                 :          37 :                             new = palloc0_object(PLpgSQL_stmt_forc);
    1432                 :          37 :                             new->cmd_type = PLPGSQL_STMT_FORC;
    1433                 :          37 :                             new->stmtid = ++plpgsql_curr_compile->nstatements;
    1434                 :          37 :                             new->curvar = cursor->dno;
    1435                 :             : 
    1436                 :             :                             /* Should have had a single variable name */
    1437   [ -  +  -  - ]:          37 :                             if ($1.scalar && $1.row)
    1438         [ #  # ]:           0 :                                 ereport(ERROR,
    1439                 :             :                                         (errcode(ERRCODE_SYNTAX_ERROR),
    1440                 :             :                                          errmsg("cursor FOR loop must have only one target variable"),
    1441                 :             :                                          parser_errposition(@1)));
    1442                 :             : 
    1443                 :             :                             /* can't use an unbound cursor this way */
    1444         [ +  + ]:          37 :                             if (cursor->cursor_explicit_expr == NULL)
    1445         [ +  - ]:           4 :                                 ereport(ERROR,
    1446                 :             :                                         (errcode(ERRCODE_SYNTAX_ERROR),
    1447                 :             :                                          errmsg("cursor FOR loop must use a bound cursor variable"),
    1448                 :             :                                          parser_errposition(tokloc)));
    1449                 :             : 
    1450                 :             :                             /* collect cursor's parameters if any */
    1451                 :          33 :                             new->argquery = read_cursor_args(cursor, K_LOOP, &yylval, &yylloc, yyscanner);
    1452                 :             : 
    1453                 :             :                             /* create loop's private RECORD variable */
    1454                 :          33 :                             new->var = (PLpgSQL_variable *)
    1455                 :          33 :                                 plpgsql_build_record($1.name,
    1456                 :          33 :                                                      $1.lineno,
    1457                 :             :                                                      NULL,
    1458                 :             :                                                      RECORDOID,
    1459                 :             :                                                      true);
    1460                 :             : 
    1461                 :          33 :                             $$ = (PLpgSQL_stmt *) new;
    1462                 :             :                         }
    1463                 :             :                         else
    1464                 :             :                         {
    1465                 :             :                             PLpgSQL_expr *expr1;
    1466                 :             :                             int         expr1loc;
    1467                 :         718 :                             bool        reverse = false;
    1468                 :             : 
    1469                 :             :                             /*
    1470                 :             :                              * We have to distinguish between two
    1471                 :             :                              * alternatives: FOR var IN a .. b and FOR
    1472                 :             :                              * var IN query. Unfortunately this is
    1473                 :             :                              * tricky, since the query in the second
    1474                 :             :                              * form needn't start with a SELECT
    1475                 :             :                              * keyword.  We use the ugly hack of
    1476                 :             :                              * looking for two periods after the first
    1477                 :             :                              * token. We also check for the REVERSE
    1478                 :             :                              * keyword, which means it must be an
    1479                 :             :                              * integer loop.
    1480                 :             :                              */
    1481         [ +  + ]:         718 :                             if (tok_is_keyword(tok, &yylval,
    1482                 :             :                                                K_REVERSE, "reverse"))
    1483                 :           3 :                                 reverse = true;
    1484                 :             :                             else
    1485                 :         715 :                                 plpgsql_push_back_token(tok, &yylval, &yylloc, yyscanner);
    1486                 :             : 
    1487                 :             :                             /*
    1488                 :             :                              * Read tokens until we see either a ".."
    1489                 :             :                              * or a LOOP.  The text we read may be either
    1490                 :             :                              * an expression or a whole SQL statement, so
    1491                 :             :                              * we need to invoke read_sql_construct directly,
    1492                 :             :                              * and tell it not to check syntax yet.
    1493                 :             :                              */
    1494                 :         718 :                             expr1 = read_sql_construct(DOT_DOT,
    1495                 :             :                                                        K_LOOP,
    1496                 :             :                                                        0,
    1497                 :             :                                                        "LOOP",
    1498                 :             :                                                        RAW_PARSE_DEFAULT,
    1499                 :             :                                                        true,
    1500                 :             :                                                        false,
    1501                 :             :                                                        &expr1loc,
    1502                 :             :                                                        &tok,
    1503                 :             :                                                        &yylval, &yylloc, yyscanner);
    1504                 :             : 
    1505         [ +  + ]:         718 :                             if (tok == DOT_DOT)
    1506                 :             :                             {
    1507                 :             :                                 /* Saw "..", so it must be an integer loop */
    1508                 :             :                                 PLpgSQL_expr *expr2;
    1509                 :             :                                 PLpgSQL_expr *expr_by;
    1510                 :             :                                 PLpgSQL_var *fvar;
    1511                 :             :                                 PLpgSQL_stmt_fori *new;
    1512                 :             : 
    1513                 :             :                                 /*
    1514                 :             :                                  * Relabel first expression as an expression;
    1515                 :             :                                  * then we can check its syntax.
    1516                 :             :                                  */
    1517                 :         462 :                                 expr1->parseMode = RAW_PARSE_PLPGSQL_EXPR;
    1518                 :         462 :                                 check_sql_expr(expr1->query, expr1->parseMode,
    1519                 :             :                                                expr1loc, yyscanner);
    1520                 :             : 
    1521                 :             :                                 /* Read and check the second one */
    1522                 :         462 :                                 expr2 = read_sql_expression2(K_LOOP, K_BY,
    1523                 :             :                                                              "LOOP",
    1524                 :             :                                                              &tok, &yylval, &yylloc, yyscanner);
    1525                 :             : 
    1526                 :             :                                 /* Get the BY clause if any */
    1527         [ +  + ]:         462 :                                 if (tok == K_BY)
    1528                 :           9 :                                     expr_by = read_sql_expression(K_LOOP,
    1529                 :             :                                                                   "LOOP", &yylval, &yylloc, yyscanner);
    1530                 :             :                                 else
    1531                 :         453 :                                     expr_by = NULL;
    1532                 :             : 
    1533                 :             :                                 /* Should have had a single variable name */
    1534   [ +  +  -  + ]:         462 :                                 if ($1.scalar && $1.row)
    1535         [ #  # ]:           0 :                                     ereport(ERROR,
    1536                 :             :                                             (errcode(ERRCODE_SYNTAX_ERROR),
    1537                 :             :                                              errmsg("integer FOR loop must have only one target variable"),
    1538                 :             :                                              parser_errposition(@1)));
    1539                 :             : 
    1540                 :             :                                 /* create loop's private variable */
    1541                 :             :                                 fvar = (PLpgSQL_var *)
    1542                 :         462 :                                     plpgsql_build_variable($1.name,
    1543                 :         462 :                                                            $1.lineno,
    1544                 :             :                                                            plpgsql_build_datatype(INT4OID,
    1545                 :             :                                                                                   -1,
    1546                 :             :                                                                                   InvalidOid,
    1547                 :             :                                                                                   NULL),
    1548                 :             :                                                            true);
    1549                 :             : 
    1550                 :         462 :                                 new = palloc0_object(PLpgSQL_stmt_fori);
    1551                 :         462 :                                 new->cmd_type = PLPGSQL_STMT_FORI;
    1552                 :         462 :                                 new->stmtid  = ++plpgsql_curr_compile->nstatements;
    1553                 :         462 :                                 new->var = fvar;
    1554                 :         462 :                                 new->reverse = reverse;
    1555                 :         462 :                                 new->lower = expr1;
    1556                 :         462 :                                 new->upper = expr2;
    1557                 :         462 :                                 new->step = expr_by;
    1558                 :             : 
    1559                 :         462 :                                 $$ = (PLpgSQL_stmt *) new;
    1560                 :             :                             }
    1561                 :             :                             else
    1562                 :             :                             {
    1563                 :             :                                 /*
    1564                 :             :                                  * No "..", so it must be a query loop.
    1565                 :             :                                  */
    1566                 :             :                                 PLpgSQL_stmt_fors *new;
    1567                 :             : 
    1568         [ -  + ]:         256 :                                 if (reverse)
    1569         [ #  # ]:           0 :                                     ereport(ERROR,
    1570                 :             :                                             (errcode(ERRCODE_SYNTAX_ERROR),
    1571                 :             :                                              errmsg("cannot specify REVERSE in query FOR loop"),
    1572                 :             :                                              parser_errposition(tokloc)));
    1573                 :             : 
    1574                 :             :                                 /* Check syntax as a regular query */
    1575                 :         256 :                                 check_sql_expr(expr1->query, expr1->parseMode,
    1576                 :             :                                                expr1loc, yyscanner);
    1577                 :             : 
    1578                 :         252 :                                 new = palloc0_object(PLpgSQL_stmt_fors);
    1579                 :         252 :                                 new->cmd_type = PLPGSQL_STMT_FORS;
    1580                 :         252 :                                 new->stmtid = ++plpgsql_curr_compile->nstatements;
    1581         [ +  + ]:         252 :                                 if ($1.row)
    1582                 :             :                                 {
    1583                 :         164 :                                     new->var = (PLpgSQL_variable *) $1.row;
    1584                 :         164 :                                     check_assignable($1.row, @1, yyscanner);
    1585                 :             :                                 }
    1586         [ +  - ]:          88 :                                 else if ($1.scalar)
    1587                 :             :                                 {
    1588                 :             :                                     /* convert single scalar to list */
    1589                 :          88 :                                     new->var = (PLpgSQL_variable *)
    1590                 :          88 :                                         make_scalar_list1($1.name, $1.scalar,
    1591                 :          88 :                                                           $1.lineno, @1, yyscanner);
    1592                 :             :                                     /* make_scalar_list1 did check_assignable */
    1593                 :             :                                 }
    1594                 :             :                                 else
    1595                 :             :                                 {
    1596         [ #  # ]:           0 :                                     ereport(ERROR,
    1597                 :             :                                             (errcode(ERRCODE_SYNTAX_ERROR),
    1598                 :             :                                              errmsg("loop variable of loop over rows must be a record variable or list of scalar variables"),
    1599                 :             :                                              parser_errposition(@1)));
    1600                 :             :                                 }
    1601                 :             : 
    1602                 :         252 :                                 new->query = expr1;
    1603                 :         252 :                                 $$ = (PLpgSQL_stmt *) new;
    1604                 :             :                             }
    1605                 :             :                         }
    1606                 :             :                     }
    1607                 :             :                 ;
    1608                 :             : 
    1609                 :             : /*
    1610                 :             :  * Processing the for_variable is tricky because we don't yet know if the
    1611                 :             :  * FOR is an integer FOR loop or a loop over query results.  In the former
    1612                 :             :  * case, the variable is just a name that we must instantiate as a loop
    1613                 :             :  * local variable, regardless of any other definition it might have.
    1614                 :             :  * Therefore, we always save the actual identifier into $$.name where it
    1615                 :             :  * can be used for that case.  We also save the outer-variable definition,
    1616                 :             :  * if any, because that's what we need for the loop-over-query case.  Note
    1617                 :             :  * that we must NOT apply check_assignable() or any other semantic check
    1618                 :             :  * until we know what's what.
    1619                 :             :  *
    1620                 :             :  * However, if we see a comma-separated list of names, we know that it
    1621                 :             :  * can't be an integer FOR loop and so it's OK to check the variables
    1622                 :             :  * immediately.  In particular, for T_WORD followed by comma, we should
    1623                 :             :  * complain that the name is not known rather than say it's a syntax error.
    1624                 :             :  * Note that the non-error result of this case sets *both* $$.scalar and
    1625                 :             :  * $$.row; see the for_control production.
    1626                 :             :  */
    1627                 :             : for_variable    : T_DATUM
    1628                 :             :                     {
    1629                 :         505 :                         $$.name = NameOfDatum(&($1));
    1630                 :         505 :                         $$.lineno = plpgsql_location_to_lineno(@1, yyscanner);
    1631         [ +  - ]:         505 :                         if ($1.datum->dtype == PLPGSQL_DTYPE_ROW ||
    1632         [ +  + ]:         505 :                             $1.datum->dtype == PLPGSQL_DTYPE_REC)
    1633                 :             :                         {
    1634                 :         205 :                             $$.scalar = NULL;
    1635                 :         205 :                             $$.row = $1.datum;
    1636                 :             :                         }
    1637                 :             :                         else
    1638                 :             :                         {
    1639                 :             :                             int         tok;
    1640                 :             : 
    1641                 :         300 :                             $$.scalar = $1.datum;
    1642                 :         300 :                             $$.row = NULL;
    1643                 :             :                             /* check for comma-separated list */
    1644                 :         300 :                             tok = yylex(&yylval, &yylloc, yyscanner);
    1645                 :         300 :                             plpgsql_push_back_token(tok, &yylval, &yylloc, yyscanner);
    1646         [ +  + ]:         300 :                             if (tok == ',')
    1647                 :          10 :                                 $$.row = (PLpgSQL_datum *)
    1648                 :          12 :                                     read_into_scalar_list($$.name,
    1649                 :             :                                                           $$.scalar,
    1650                 :             :                                                           @1,
    1651                 :             :                                                           &yylval, &yylloc,
    1652                 :             :                                                           yyscanner);
    1653                 :             :                         }
    1654                 :             :                     }
    1655                 :             :                 | T_WORD
    1656                 :             :                     {
    1657                 :             :                         int         tok;
    1658                 :             : 
    1659                 :         483 :                         $$.name = $1.ident;
    1660                 :         483 :                         $$.lineno = plpgsql_location_to_lineno(@1, yyscanner);
    1661                 :         483 :                         $$.scalar = NULL;
    1662                 :         483 :                         $$.row = NULL;
    1663                 :             :                         /* check for comma-separated list */
    1664                 :         483 :                         tok = yylex(&yylval, &yylloc, yyscanner);
    1665                 :         483 :                         plpgsql_push_back_token(tok, &yylval, &yylloc, yyscanner);
    1666         [ -  + ]:         483 :                         if (tok == ',')
    1667                 :           0 :                             word_is_not_variable(&($1), @1, yyscanner);
    1668                 :             :                     }
    1669                 :             :                 | T_CWORD
    1670                 :             :                     {
    1671                 :             :                         /* just to give a better message than "syntax error" */
    1672                 :           0 :                         cword_is_not_variable(&($1), @1, yyscanner);
    1673                 :             :                     }
    1674                 :             :                 ;
    1675                 :             : 
    1676                 :             : stmt_foreach_a  : opt_loop_label K_FOREACH for_variable foreach_slice K_IN K_ARRAY expr_until_loop loop_body
    1677                 :             :                     {
    1678                 :             :                         PLpgSQL_stmt_foreach_a *new;
    1679                 :             : 
    1680                 :          41 :                         new = palloc0_object(PLpgSQL_stmt_foreach_a);
    1681                 :          41 :                         new->cmd_type = PLPGSQL_STMT_FOREACH_A;
    1682                 :          41 :                         new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
    1683                 :          41 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    1684                 :          41 :                         new->label = $1;
    1685                 :          41 :                         new->slice = $4;
    1686                 :          41 :                         new->expr = $7;
    1687                 :          41 :                         new->body = $8.stmts;
    1688                 :             : 
    1689         [ +  + ]:          41 :                         if ($3.row)
    1690                 :             :                         {
    1691                 :          16 :                             new->varno = $3.row->dno;
    1692                 :          16 :                             check_assignable($3.row, @3, yyscanner);
    1693                 :             :                         }
    1694         [ +  - ]:          25 :                         else if ($3.scalar)
    1695                 :             :                         {
    1696                 :          25 :                             new->varno = $3.scalar->dno;
    1697                 :          25 :                             check_assignable($3.scalar, @3, yyscanner);
    1698                 :             :                         }
    1699                 :             :                         else
    1700                 :             :                         {
    1701         [ #  # ]:           0 :                             ereport(ERROR,
    1702                 :             :                                     (errcode(ERRCODE_SYNTAX_ERROR),
    1703                 :             :                                      errmsg("loop variable of FOREACH must be a known variable or list of variables"),
    1704                 :             :                                              parser_errposition(@3)));
    1705                 :             :                         }
    1706                 :             : 
    1707                 :          41 :                         check_labels($1, $8.end_label, $8.end_label_location, yyscanner);
    1708                 :          41 :                         plpgsql_ns_pop();
    1709                 :             : 
    1710                 :          41 :                         $$ = (PLpgSQL_stmt *) new;
    1711                 :             :                     }
    1712                 :             :                 ;
    1713                 :             : 
    1714                 :             : foreach_slice   :
    1715                 :             :                     {
    1716                 :          21 :                         $$ = 0;
    1717                 :             :                     }
    1718                 :             :                 | K_SLICE ICONST
    1719                 :             :                     {
    1720                 :          20 :                         $$ = $2;
    1721                 :             :                     }
    1722                 :             :                 ;
    1723                 :             : 
    1724                 :             : stmt_exit       : exit_type opt_label opt_exitcond
    1725                 :             :                     {
    1726                 :             :                         PLpgSQL_stmt_exit *new;
    1727                 :             : 
    1728                 :         141 :                         new = palloc0_object(PLpgSQL_stmt_exit);
    1729                 :         141 :                         new->cmd_type = PLPGSQL_STMT_EXIT;
    1730                 :         141 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    1731                 :         141 :                         new->is_exit = $1;
    1732                 :         141 :                         new->lineno  = plpgsql_location_to_lineno(@1, yyscanner);
    1733                 :         141 :                         new->label = $2;
    1734                 :         141 :                         new->cond = $3;
    1735                 :             : 
    1736         [ +  + ]:         141 :                         if ($2)
    1737                 :             :                         {
    1738                 :             :                             /* We have a label, so verify it exists */
    1739                 :             :                             PLpgSQL_nsitem *label;
    1740                 :             : 
    1741                 :          16 :                             label = plpgsql_ns_lookup_label(plpgsql_ns_top(), $2);
    1742         [ +  + ]:          16 :                             if (label == NULL)
    1743         [ +  - ]:           2 :                                 ereport(ERROR,
    1744                 :             :                                         (errcode(ERRCODE_SYNTAX_ERROR),
    1745                 :             :                                          errmsg("there is no label \"%s\" "
    1746                 :             :                                                 "attached to any block or loop enclosing this statement",
    1747                 :             :                                                 $2),
    1748                 :             :                                          parser_errposition(@2)));
    1749                 :             :                             /* CONTINUE only allows loop labels */
    1750   [ +  +  +  + ]:          14 :                             if (label->itemno != PLPGSQL_LABEL_LOOP && !new->is_exit)
    1751         [ +  - ]:           1 :                                 ereport(ERROR,
    1752                 :             :                                         (errcode(ERRCODE_SYNTAX_ERROR),
    1753                 :             :                                          errmsg("block label \"%s\" cannot be used in CONTINUE",
    1754                 :             :                                                 $2),
    1755                 :             :                                          parser_errposition(@2)));
    1756                 :             :                         }
    1757                 :             :                         else
    1758                 :             :                         {
    1759                 :             :                             /*
    1760                 :             :                              * No label, so make sure there is some loop (an
    1761                 :             :                              * unlabeled EXIT does not match a block, so this
    1762                 :             :                              * is the same test for both EXIT and CONTINUE)
    1763                 :             :                              */
    1764         [ +  + ]:         125 :                             if (plpgsql_ns_find_nearest_loop(plpgsql_ns_top()) == NULL)
    1765   [ +  -  +  + ]:           2 :                                 ereport(ERROR,
    1766                 :             :                                         (errcode(ERRCODE_SYNTAX_ERROR),
    1767                 :             :                                          new->is_exit ?
    1768                 :             :                                          errmsg("EXIT cannot be used outside a loop, unless it has a label") :
    1769                 :             :                                          errmsg("CONTINUE cannot be used outside a loop"),
    1770                 :             :                                          parser_errposition(@1)));
    1771                 :             :                         }
    1772                 :             : 
    1773                 :         136 :                         $$ = (PLpgSQL_stmt *) new;
    1774                 :             :                     }
    1775                 :             :                 ;
    1776                 :             : 
    1777                 :             : exit_type       : K_EXIT
    1778                 :             :                     {
    1779                 :          78 :                         $$ = true;
    1780                 :             :                     }
    1781                 :             :                 | K_CONTINUE
    1782                 :             :                     {
    1783                 :          63 :                         $$ = false;
    1784                 :             :                     }
    1785                 :             :                 ;
    1786                 :             : 
    1787                 :             : stmt_return     : K_RETURN
    1788                 :             :                     {
    1789                 :             :                         int         tok;
    1790                 :             : 
    1791                 :        5856 :                         tok = yylex(&yylval, &yylloc, yyscanner);
    1792         [ -  + ]:        5856 :                         if (tok == 0)
    1793                 :           0 :                             yyerror(&yylloc, NULL, yyscanner, "unexpected end of function definition");
    1794                 :             : 
    1795         [ +  + ]:        5856 :                         if (tok_is_keyword(tok, &yylval,
    1796                 :             :                                            K_NEXT, "next"))
    1797                 :             :                         {
    1798                 :         227 :                             $$ = make_return_next_stmt(@1, &yylval, &yylloc, yyscanner);
    1799                 :             :                         }
    1800         [ +  + ]:        5629 :                         else if (tok_is_keyword(tok, &yylval,
    1801                 :             :                                                 K_QUERY, "query"))
    1802                 :             :                         {
    1803                 :         119 :                             $$ = make_return_query_stmt(@1, &yylval, &yylloc, yyscanner);
    1804                 :             :                         }
    1805                 :             :                         else
    1806                 :             :                         {
    1807                 :        5510 :                             plpgsql_push_back_token(tok, &yylval, &yylloc, yyscanner);
    1808                 :        5510 :                             $$ = make_return_stmt(@1, &yylval, &yylloc, yyscanner);
    1809                 :             :                         }
    1810                 :             :                     }
    1811                 :             :                 ;
    1812                 :             : 
    1813                 :             : stmt_raise      : K_RAISE
    1814                 :             :                     {
    1815                 :             :                         PLpgSQL_stmt_raise *new;
    1816                 :             :                         int         tok;
    1817                 :             : 
    1818                 :        4295 :                         new = palloc_object(PLpgSQL_stmt_raise);
    1819                 :             : 
    1820                 :        4295 :                         new->cmd_type = PLPGSQL_STMT_RAISE;
    1821                 :        4295 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    1822                 :        4295 :                         new->stmtid  = ++plpgsql_curr_compile->nstatements;
    1823                 :        4295 :                         new->elog_level = ERROR; /* default */
    1824                 :        4295 :                         new->condname = NULL;
    1825                 :        4295 :                         new->message = NULL;
    1826                 :        4295 :                         new->params = NIL;
    1827                 :        4295 :                         new->options = NIL;
    1828                 :             : 
    1829                 :        4295 :                         tok = yylex(&yylval, &yylloc, yyscanner);
    1830         [ -  + ]:        4295 :                         if (tok == 0)
    1831                 :           0 :                             yyerror(&yylloc, NULL, yyscanner, "unexpected end of function definition");
    1832                 :             : 
    1833                 :             :                         /*
    1834                 :             :                          * We could have just RAISE, meaning to re-throw
    1835                 :             :                          * the current error.
    1836                 :             :                          */
    1837         [ +  + ]:        4295 :                         if (tok != ';')
    1838                 :             :                         {
    1839                 :             :                             /*
    1840                 :             :                              * First is an optional elog severity level.
    1841                 :             :                              */
    1842         [ +  + ]:        4267 :                             if (tok_is_keyword(tok, &yylval,
    1843                 :             :                                                K_EXCEPTION, "exception"))
    1844                 :             :                             {
    1845                 :         548 :                                 new->elog_level = ERROR;
    1846                 :         548 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    1847                 :             :                             }
    1848         [ +  + ]:        3719 :                             else if (tok_is_keyword(tok, &yylval,
    1849                 :             :                                                     K_WARNING, "warning"))
    1850                 :             :                             {
    1851                 :         148 :                                 new->elog_level = WARNING;
    1852                 :         148 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    1853                 :             :                             }
    1854         [ +  + ]:        3571 :                             else if (tok_is_keyword(tok, &yylval,
    1855                 :             :                                                     K_NOTICE, "notice"))
    1856                 :             :                             {
    1857                 :        3179 :                                 new->elog_level = NOTICE;
    1858                 :        3179 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    1859                 :             :                             }
    1860         [ +  + ]:         392 :                             else if (tok_is_keyword(tok, &yylval,
    1861                 :             :                                                     K_INFO, "info"))
    1862                 :             :                             {
    1863                 :         160 :                                 new->elog_level = INFO;
    1864                 :         160 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    1865                 :             :                             }
    1866         [ +  + ]:         232 :                             else if (tok_is_keyword(tok, &yylval,
    1867                 :             :                                                     K_LOG, "log"))
    1868                 :             :                             {
    1869                 :          85 :                                 new->elog_level = LOG;
    1870                 :          85 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    1871                 :             :                             }
    1872         [ +  + ]:         147 :                             else if (tok_is_keyword(tok, &yylval,
    1873                 :             :                                                     K_DEBUG, "debug"))
    1874                 :             :                             {
    1875                 :          29 :                                 new->elog_level = DEBUG1;
    1876                 :          29 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    1877                 :             :                             }
    1878         [ -  + ]:        4267 :                             if (tok == 0)
    1879                 :           0 :                                 yyerror(&yylloc, NULL, yyscanner, "unexpected end of function definition");
    1880                 :             : 
    1881                 :             :                             /*
    1882                 :             :                              * Next we can have a condition name, or
    1883                 :             :                              * equivalently SQLSTATE 'xxxxx', or a string
    1884                 :             :                              * literal that is the old-style message format,
    1885                 :             :                              * or USING to start the option list immediately.
    1886                 :             :                              */
    1887         [ +  + ]:        4267 :                             if (tok == SCONST)
    1888                 :             :                             {
    1889                 :             :                                 /* old style message and parameters */
    1890                 :        4171 :                                 new->message = yylval.str;
    1891                 :             :                                 /*
    1892                 :             :                                  * We expect either a semi-colon, which
    1893                 :             :                                  * indicates no parameters, or a comma that
    1894                 :             :                                  * begins the list of parameter expressions,
    1895                 :             :                                  * or USING to begin the options list.
    1896                 :             :                                  */
    1897                 :        4171 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    1898   [ +  +  +  +  :        4171 :                                 if (tok != ',' && tok != ';' && tok != K_USING)
                   -  + ]
    1899                 :           0 :                                     yyerror(&yylloc, NULL, yyscanner, "syntax error");
    1900                 :             : 
    1901         [ +  + ]:       12918 :                                 while (tok == ',')
    1902                 :             :                                 {
    1903                 :             :                                     PLpgSQL_expr *expr;
    1904                 :             : 
    1905                 :        8747 :                                     expr = read_sql_construct(',', ';', K_USING,
    1906                 :             :                                                               ", or ; or USING",
    1907                 :             :                                                               RAW_PARSE_PLPGSQL_EXPR,
    1908                 :             :                                                               true, true,
    1909                 :             :                                                               NULL, &tok,
    1910                 :             :                                                               &yylval, &yylloc, yyscanner);
    1911                 :        8747 :                                     new->params = lappend(new->params, expr);
    1912                 :             :                                 }
    1913                 :             :                             }
    1914         [ +  + ]:          96 :                             else if (tok != K_USING)
    1915                 :             :                             {
    1916                 :             :                                 /* must be condition name or SQLSTATE */
    1917         [ +  + ]:          84 :                                 if (tok_is_keyword(tok, &yylval,
    1918                 :             :                                                    K_SQLSTATE, "sqlstate"))
    1919                 :             :                                 {
    1920                 :             :                                     /* next token should be a string literal */
    1921                 :             :                                     char       *sqlstatestr;
    1922                 :             : 
    1923         [ -  + ]:          56 :                                     if (yylex(&yylval, &yylloc, yyscanner) != SCONST)
    1924                 :           0 :                                         yyerror(&yylloc, NULL, yyscanner, "syntax error");
    1925                 :          56 :                                     sqlstatestr = yylval.str;
    1926                 :             : 
    1927         [ -  + ]:          56 :                                     if (strlen(sqlstatestr) != 5)
    1928                 :           0 :                                         yyerror(&yylloc, NULL, yyscanner, "invalid SQLSTATE code");
    1929         [ -  + ]:          56 :                                     if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
    1930                 :           0 :                                         yyerror(&yylloc, NULL, yyscanner, "invalid SQLSTATE code");
    1931                 :          56 :                                     new->condname = sqlstatestr;
    1932                 :             :                                 }
    1933                 :             :                                 else
    1934                 :             :                                 {
    1935         [ +  - ]:          28 :                                     if (tok == T_WORD)
    1936                 :          28 :                                         new->condname = yylval.word.ident;
    1937         [ #  # ]:           0 :                                     else if (plpgsql_token_is_unreserved_keyword(tok))
    1938                 :           0 :                                         new->condname = pstrdup(yylval.keyword);
    1939                 :             :                                     else
    1940                 :           0 :                                         yyerror(&yylloc, NULL, yyscanner, "syntax error");
    1941                 :          28 :                                     plpgsql_recognize_err_condition(new->condname,
    1942                 :             :                                                                     false);
    1943                 :             :                                 }
    1944                 :          84 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    1945   [ +  +  -  + ]:          84 :                                 if (tok != ';' && tok != K_USING)
    1946                 :           0 :                                     yyerror(&yylloc, NULL, yyscanner, "syntax error");
    1947                 :             :                             }
    1948                 :             : 
    1949         [ +  + ]:        4267 :                             if (tok == K_USING)
    1950                 :         106 :                                 new->options = read_raise_options(&yylval, &yylloc, yyscanner);
    1951                 :             :                         }
    1952                 :             : 
    1953                 :        4295 :                         check_raise_parameters(new);
    1954                 :             : 
    1955                 :        4287 :                         $$ = (PLpgSQL_stmt *) new;
    1956                 :             :                     }
    1957                 :             :                 ;
    1958                 :             : 
    1959                 :             : stmt_assert     : K_ASSERT
    1960                 :             :                     {
    1961                 :             :                         PLpgSQL_stmt_assert *new;
    1962                 :             :                         int         tok;
    1963                 :             : 
    1964                 :          35 :                         new = palloc_object(PLpgSQL_stmt_assert);
    1965                 :             : 
    1966                 :          35 :                         new->cmd_type = PLPGSQL_STMT_ASSERT;
    1967                 :          35 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    1968                 :          35 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    1969                 :             : 
    1970                 :          35 :                         new->cond = read_sql_expression2(',', ';',
    1971                 :             :                                                          ", or ;",
    1972                 :             :                                                          &tok, &yylval, &yylloc, yyscanner);
    1973                 :             : 
    1974         [ +  + ]:          35 :                         if (tok == ',')
    1975                 :          14 :                             new->message = read_sql_expression(';', ";", &yylval, &yylloc, yyscanner);
    1976                 :             :                         else
    1977                 :          21 :                             new->message = NULL;
    1978                 :             : 
    1979                 :          35 :                         $$ = (PLpgSQL_stmt *) new;
    1980                 :             :                     }
    1981                 :             :                 ;
    1982                 :             : 
    1983                 :             : loop_body       : proc_sect K_END K_LOOP opt_label ';'
    1984                 :             :                     {
    1985                 :        1082 :                         $$.stmts = $1;
    1986                 :        1082 :                         $$.end_label = $4;
    1987                 :        1082 :                         $$.end_label_location = @4;
    1988                 :             :                     }
    1989                 :             :                 ;
    1990                 :             : 
    1991                 :             : /*
    1992                 :             :  * T_WORD+T_CWORD match any initial identifier that is not a known plpgsql
    1993                 :             :  * variable.  (The composite case is probably a syntax error, but we'll let
    1994                 :             :  * the core parser decide that.)  Normally, we should assume that such a
    1995                 :             :  * word is a SQL statement keyword that isn't also a plpgsql keyword.
    1996                 :             :  * However, if the next token is assignment or '[' or '.', it can't be a valid
    1997                 :             :  * SQL statement, and what we're probably looking at is an intended variable
    1998                 :             :  * assignment.  Give an appropriate complaint for that, instead of letting
    1999                 :             :  * the core parser throw an unhelpful "syntax error".
    2000                 :             :  */
    2001                 :             : stmt_execsql    : K_IMPORT
    2002                 :             :                     {
    2003                 :           0 :                         $$ = make_execsql_stmt(K_IMPORT, @1, NULL, &yylval, &yylloc, yyscanner);
    2004                 :             :                     }
    2005                 :             :                 | K_INSERT
    2006                 :             :                     {
    2007                 :         590 :                         $$ = make_execsql_stmt(K_INSERT, @1, NULL, &yylval, &yylloc, yyscanner);
    2008                 :             :                     }
    2009                 :             :                 | K_MERGE
    2010                 :             :                     {
    2011                 :          36 :                         $$ = make_execsql_stmt(K_MERGE, @1, NULL, &yylval, &yylloc, yyscanner);
    2012                 :             :                     }
    2013                 :             :                 | T_WORD
    2014                 :             :                     {
    2015                 :             :                         int         tok;
    2016                 :             : 
    2017                 :        1948 :                         tok = yylex(&yylval, &yylloc, yyscanner);
    2018                 :        1948 :                         plpgsql_push_back_token(tok, &yylval, &yylloc, yyscanner);
    2019   [ +  -  +  -  :        1948 :                         if (tok == '=' || tok == COLON_EQUALS ||
                   +  - ]
    2020         [ -  + ]:        1948 :                             tok == '[' || tok == '.')
    2021                 :           0 :                             word_is_not_variable(&($1), @1, yyscanner);
    2022                 :        1948 :                         $$ = make_execsql_stmt(T_WORD, @1, &($1), &yylval, &yylloc, yyscanner);
    2023                 :             :                     }
    2024                 :             :                 | T_CWORD
    2025                 :             :                     {
    2026                 :             :                         int         tok;
    2027                 :             : 
    2028                 :           2 :                         tok = yylex(&yylval, &yylloc, yyscanner);
    2029                 :           2 :                         plpgsql_push_back_token(tok, &yylval, &yylloc, yyscanner);
    2030   [ +  +  +  -  :           2 :                         if (tok == '=' || tok == COLON_EQUALS ||
                   +  - ]
    2031         [ +  - ]:           1 :                             tok == '[' || tok == '.')
    2032                 :           2 :                             cword_is_not_variable(&($1), @1, yyscanner);
    2033                 :           0 :                         $$ = make_execsql_stmt(T_CWORD, @1, NULL, &yylval, &yylloc, yyscanner);
    2034                 :             :                     }
    2035                 :             :                 ;
    2036                 :             : 
    2037                 :             : stmt_dynexecute : K_EXECUTE
    2038                 :             :                     {
    2039                 :             :                         PLpgSQL_stmt_dynexecute *new;
    2040                 :             :                         PLpgSQL_expr *expr;
    2041                 :             :                         int         endtoken;
    2042                 :             : 
    2043                 :         651 :                         expr = read_sql_construct(K_INTO, K_USING, ';',
    2044                 :             :                                                   "INTO or USING or ;",
    2045                 :             :                                                   RAW_PARSE_PLPGSQL_EXPR,
    2046                 :             :                                                   true, true,
    2047                 :             :                                                   NULL, &endtoken,
    2048                 :             :                                                   &yylval, &yylloc, yyscanner);
    2049                 :             : 
    2050                 :         651 :                         new = palloc_object(PLpgSQL_stmt_dynexecute);
    2051                 :         651 :                         new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
    2052                 :         651 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    2053                 :         651 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    2054                 :         651 :                         new->query = expr;
    2055                 :         651 :                         new->into = false;
    2056                 :         651 :                         new->strict = false;
    2057                 :         651 :                         new->target = NULL;
    2058                 :         651 :                         new->params = NIL;
    2059                 :             : 
    2060                 :             :                         /*
    2061                 :             :                          * We loop to allow the INTO and USING clauses to
    2062                 :             :                          * appear in either order, since people easily get
    2063                 :             :                          * that wrong.  This coding also prevents "INTO foo"
    2064                 :             :                          * from getting absorbed into a USING expression,
    2065                 :             :                          * which is *really* confusing.
    2066                 :             :                          */
    2067                 :             :                         for (;;)
    2068                 :             :                         {
    2069         [ +  + ]:        1165 :                             if (endtoken == K_INTO)
    2070                 :             :                             {
    2071         [ -  + ]:         319 :                                 if (new->into)           /* multiple INTO */
    2072                 :           0 :                                     yyerror(&yylloc, NULL, yyscanner, "syntax error");
    2073                 :         319 :                                 new->into = true;
    2074                 :         319 :                                 read_into_target(&new->target, &new->strict, &yylval, &yylloc, yyscanner);
    2075                 :         319 :                                 endtoken = yylex(&yylval, &yylloc, yyscanner);
    2076                 :             :                             }
    2077         [ +  + ]:         846 :                             else if (endtoken == K_USING)
    2078                 :             :                             {
    2079         [ -  + ]:         195 :                                 if (new->params)     /* multiple USING */
    2080                 :           0 :                                     yyerror(&yylloc, NULL, yyscanner, "syntax error");
    2081                 :             :                                 do
    2082                 :             :                                 {
    2083                 :         386 :                                     expr = read_sql_construct(',', ';', K_INTO,
    2084                 :             :                                                               ", or ; or INTO",
    2085                 :             :                                                               RAW_PARSE_PLPGSQL_EXPR,
    2086                 :             :                                                               true, true,
    2087                 :             :                                                               NULL, &endtoken,
    2088                 :             :                                                               &yylval, &yylloc, yyscanner);
    2089                 :         386 :                                     new->params = lappend(new->params, expr);
    2090         [ +  + ]:         386 :                                 } while (endtoken == ',');
    2091                 :             :                             }
    2092         [ +  - ]:         651 :                             else if (endtoken == ';')
    2093                 :         651 :                                 break;
    2094                 :             :                             else
    2095                 :           0 :                                 yyerror(&yylloc, NULL, yyscanner, "syntax error");
    2096                 :             :                         }
    2097                 :             : 
    2098                 :         651 :                         $$ = (PLpgSQL_stmt *) new;
    2099                 :             :                     }
    2100                 :             :                 ;
    2101                 :             : 
    2102                 :             : 
    2103                 :             : stmt_open       : K_OPEN cursor_variable
    2104                 :             :                     {
    2105                 :             :                         PLpgSQL_stmt_open *new;
    2106                 :             :                         int         tok;
    2107                 :             : 
    2108                 :          93 :                         new = palloc0_object(PLpgSQL_stmt_open);
    2109                 :          93 :                         new->cmd_type = PLPGSQL_STMT_OPEN;
    2110                 :          93 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    2111                 :          93 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    2112                 :          93 :                         new->curvar = $2->dno;
    2113                 :          93 :                         new->cursor_options = CURSOR_OPT_FAST_PLAN;
    2114                 :             : 
    2115         [ +  + ]:          93 :                         if ($2->cursor_explicit_expr == NULL)
    2116                 :             :                         {
    2117                 :             :                             /* be nice if we could use opt_scrollable here */
    2118                 :          37 :                             tok = yylex(&yylval, &yylloc, yyscanner);
    2119         [ -  + ]:          37 :                             if (tok_is_keyword(tok, &yylval,
    2120                 :             :                                                K_NO, "no"))
    2121                 :             :                             {
    2122                 :           0 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    2123         [ #  # ]:           0 :                                 if (tok_is_keyword(tok, &yylval,
    2124                 :             :                                                    K_SCROLL, "scroll"))
    2125                 :             :                                 {
    2126                 :           0 :                                     new->cursor_options |= CURSOR_OPT_NO_SCROLL;
    2127                 :           0 :                                     tok = yylex(&yylval, &yylloc, yyscanner);
    2128                 :             :                                 }
    2129                 :             :                             }
    2130         [ +  + ]:          37 :                             else if (tok_is_keyword(tok, &yylval,
    2131                 :             :                                                     K_SCROLL, "scroll"))
    2132                 :             :                             {
    2133                 :          13 :                                 new->cursor_options |= CURSOR_OPT_SCROLL;
    2134                 :          13 :                                 tok = yylex(&yylval, &yylloc, yyscanner);
    2135                 :             :                             }
    2136                 :             : 
    2137         [ -  + ]:          37 :                             if (tok != K_FOR)
    2138                 :           0 :                                 yyerror(&yylloc, NULL, yyscanner, "syntax error, expected \"FOR\"");
    2139                 :             : 
    2140                 :          37 :                             tok = yylex(&yylval, &yylloc, yyscanner);
    2141         [ +  + ]:          37 :                             if (tok_is_keyword(tok, &yylval,
    2142                 :             :                                                K_EXECUTE, "execute"))
    2143                 :             :                             {
    2144                 :             :                                 int         endtoken;
    2145                 :             : 
    2146                 :          12 :                                 new->dynquery =
    2147                 :          12 :                                     read_sql_expression2(K_USING, ';',
    2148                 :             :                                                          "USING or ;",
    2149                 :             :                                                          &endtoken, &yylval, &yylloc, yyscanner);
    2150                 :             : 
    2151                 :             :                                 /* If we found "USING", collect argument(s) */
    2152         [ +  + ]:          12 :                                 if (endtoken == K_USING)
    2153                 :             :                                 {
    2154                 :             :                                     PLpgSQL_expr *expr;
    2155                 :             : 
    2156                 :             :                                     do
    2157                 :             :                                     {
    2158                 :           4 :                                         expr = read_sql_expression2(',', ';',
    2159                 :             :                                                                     ", or ;",
    2160                 :             :                                                                     &endtoken, &yylval, &yylloc, yyscanner);
    2161                 :           4 :                                         new->params = lappend(new->params,
    2162                 :             :                                                               expr);
    2163         [ -  + ]:           4 :                                     } while (endtoken == ',');
    2164                 :             :                                 }
    2165                 :             :                             }
    2166                 :             :                             else
    2167                 :             :                             {
    2168                 :          25 :                                 plpgsql_push_back_token(tok, &yylval, &yylloc, yyscanner);
    2169                 :          25 :                                 new->query = read_sql_stmt(&yylval, &yylloc, yyscanner);
    2170                 :             :                             }
    2171                 :             :                         }
    2172                 :             :                         else
    2173                 :             :                         {
    2174                 :             :                             /* predefined cursor query, so read args */
    2175                 :          56 :                             new->argquery = read_cursor_args($2, ';', &yylval, &yylloc, yyscanner);
    2176                 :             :                         }
    2177                 :             : 
    2178                 :          77 :                         $$ = (PLpgSQL_stmt *) new;
    2179                 :             :                     }
    2180                 :             :                 ;
    2181                 :             : 
    2182                 :             : stmt_fetch      : K_FETCH opt_fetch_direction cursor_variable K_INTO
    2183                 :             :                     {
    2184                 :          81 :                         PLpgSQL_stmt_fetch *fetch = $2;
    2185                 :             :                         PLpgSQL_variable *target;
    2186                 :             : 
    2187                 :             :                         /* We have already parsed everything through the INTO keyword */
    2188                 :          81 :                         read_into_target(&target, NULL, &yylval, &yylloc, yyscanner);
    2189                 :             : 
    2190         [ -  + ]:          81 :                         if (yylex(&yylval, &yylloc, yyscanner) != ';')
    2191                 :           0 :                             yyerror(&yylloc, NULL, yyscanner, "syntax error");
    2192                 :             : 
    2193                 :             :                         /*
    2194                 :             :                          * We don't allow multiple rows in PL/pgSQL's FETCH
    2195                 :             :                          * statement, only in MOVE.
    2196                 :             :                          */
    2197         [ -  + ]:          81 :                         if (fetch->returns_multiple_rows)
    2198         [ #  # ]:           0 :                             ereport(ERROR,
    2199                 :             :                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2200                 :             :                                      errmsg("FETCH statement cannot return multiple rows"),
    2201                 :             :                                      parser_errposition(@1)));
    2202                 :             : 
    2203                 :          81 :                         fetch->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    2204                 :          81 :                         fetch->target    = target;
    2205                 :          81 :                         fetch->curvar    = $3->dno;
    2206                 :          81 :                         fetch->is_move   = false;
    2207                 :             : 
    2208                 :          81 :                         $$ = (PLpgSQL_stmt *) fetch;
    2209                 :             :                     }
    2210                 :             :                 ;
    2211                 :             : 
    2212                 :             : stmt_move       : K_MOVE opt_fetch_direction cursor_variable ';'
    2213                 :             :                     {
    2214                 :          12 :                         PLpgSQL_stmt_fetch *fetch = $2;
    2215                 :             : 
    2216                 :          12 :                         fetch->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    2217                 :          12 :                         fetch->curvar = $3->dno;
    2218                 :          12 :                         fetch->is_move = true;
    2219                 :             : 
    2220                 :          12 :                         $$ = (PLpgSQL_stmt *) fetch;
    2221                 :             :                     }
    2222                 :             :                 ;
    2223                 :             : 
    2224                 :             : opt_fetch_direction :
    2225                 :             :                     {
    2226                 :          93 :                         $$ = read_fetch_direction(&yylval, &yylloc, yyscanner);
    2227                 :             :                     }
    2228                 :             :                 ;
    2229                 :             : 
    2230                 :             : stmt_close      : K_CLOSE cursor_variable ';'
    2231                 :             :                     {
    2232                 :             :                         PLpgSQL_stmt_close *new;
    2233                 :             : 
    2234                 :          44 :                         new = palloc_object(PLpgSQL_stmt_close);
    2235                 :          44 :                         new->cmd_type = PLPGSQL_STMT_CLOSE;
    2236                 :          44 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    2237                 :          44 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    2238                 :          44 :                         new->curvar = $2->dno;
    2239                 :             : 
    2240                 :          44 :                         $$ = (PLpgSQL_stmt *) new;
    2241                 :             :                     }
    2242                 :             :                 ;
    2243                 :             : 
    2244                 :             : stmt_null       : K_NULL ';'
    2245                 :             :                     {
    2246                 :             :                         /* We do not bother building a node for NULL */
    2247                 :          29 :                         $$ = NULL;
    2248                 :             :                     }
    2249                 :             :                 ;
    2250                 :             : 
    2251                 :             : stmt_commit     : K_COMMIT opt_transaction_chain ';'
    2252                 :             :                     {
    2253                 :             :                         PLpgSQL_stmt_commit *new;
    2254                 :             : 
    2255                 :          39 :                         new = palloc_object(PLpgSQL_stmt_commit);
    2256                 :          39 :                         new->cmd_type = PLPGSQL_STMT_COMMIT;
    2257                 :          39 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    2258                 :          39 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    2259                 :          39 :                         new->chain = $2;
    2260                 :             : 
    2261                 :          39 :                         $$ = (PLpgSQL_stmt *) new;
    2262                 :             :                     }
    2263                 :             :                 ;
    2264                 :             : 
    2265                 :             : stmt_rollback   : K_ROLLBACK opt_transaction_chain ';'
    2266                 :             :                     {
    2267                 :             :                         PLpgSQL_stmt_rollback *new;
    2268                 :             : 
    2269                 :          33 :                         new = palloc_object(PLpgSQL_stmt_rollback);
    2270                 :          33 :                         new->cmd_type = PLPGSQL_STMT_ROLLBACK;
    2271                 :          33 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    2272                 :          33 :                         new->stmtid = ++plpgsql_curr_compile->nstatements;
    2273                 :          33 :                         new->chain = $2;
    2274                 :             : 
    2275                 :          33 :                         $$ = (PLpgSQL_stmt *) new;
    2276                 :             :                     }
    2277                 :             :                 ;
    2278                 :             : 
    2279                 :             : opt_transaction_chain:
    2280                 :           2 :             K_AND K_CHAIN           { $$ = true; }
    2281                 :           0 :             | K_AND K_NO K_CHAIN    { $$ = false; }
    2282                 :          70 :             | /* EMPTY */           { $$ = false; }
    2283                 :             :                 ;
    2284                 :             : 
    2285                 :             : 
    2286                 :             : cursor_variable : T_DATUM
    2287                 :             :                     {
    2288                 :             :                         /*
    2289                 :             :                          * In principle we should support a cursor_variable
    2290                 :             :                          * that is an array element, but for now we don't, so
    2291                 :             :                          * just throw an error if next token is '['.
    2292                 :             :                          */
    2293   [ +  -  -  + ]:         460 :                         if ($1.datum->dtype != PLPGSQL_DTYPE_VAR ||
    2294                 :         230 :                             plpgsql_peek(yyscanner) == '[')
    2295         [ #  # ]:           0 :                             ereport(ERROR,
    2296                 :             :                                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    2297                 :             :                                      errmsg("cursor variable must be a simple variable"),
    2298                 :             :                                      parser_errposition(@1)));
    2299                 :             : 
    2300         [ -  + ]:         230 :                         if (((PLpgSQL_var *) $1.datum)->datatype->typoid != REFCURSOROID)
    2301         [ #  # ]:           0 :                             ereport(ERROR,
    2302                 :             :                                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    2303                 :             :                                      errmsg("variable \"%s\" must be of type cursor or refcursor",
    2304                 :             :                                             ((PLpgSQL_var *) $1.datum)->refname),
    2305                 :             :                                      parser_errposition(@1)));
    2306                 :         230 :                         $$ = (PLpgSQL_var *) $1.datum;
    2307                 :             :                     }
    2308                 :             :                 | T_WORD
    2309                 :             :                     {
    2310                 :             :                         /* just to give a better message than "syntax error" */
    2311                 :           0 :                         word_is_not_variable(&($1), @1, yyscanner);
    2312                 :             :                     }
    2313                 :             :                 | T_CWORD
    2314                 :             :                     {
    2315                 :             :                         /* just to give a better message than "syntax error" */
    2316                 :           0 :                         cword_is_not_variable(&($1), @1, yyscanner);
    2317                 :             :                     }
    2318                 :             :                 ;
    2319                 :             : 
    2320                 :             : exception_sect  :
    2321                 :        5776 :                     { $$ = NULL; }
    2322                 :             :                 | K_EXCEPTION
    2323                 :             :                     {
    2324                 :             :                         /*
    2325                 :             :                          * We use a mid-rule action to add these
    2326                 :             :                          * special variables to the namespace before
    2327                 :             :                          * parsing the WHEN clauses themselves.  The
    2328                 :             :                          * scope of the names extends to the end of the
    2329                 :             :                          * current block.
    2330                 :             :                          */
    2331                 :         298 :                         int         lineno = plpgsql_location_to_lineno(@1, yyscanner);
    2332                 :         298 :                         PLpgSQL_exception_block *new = palloc_object(PLpgSQL_exception_block);
    2333                 :             :                         PLpgSQL_variable *var;
    2334                 :             : 
    2335                 :         298 :                         plpgsql_curr_compile->has_exception_block = true;
    2336                 :             : 
    2337                 :         298 :                         var = plpgsql_build_variable("sqlstate", lineno,
    2338                 :             :                                                      plpgsql_build_datatype(TEXTOID,
    2339                 :             :                                                                             -1,
    2340                 :         298 :                                                                             plpgsql_curr_compile->fn_input_collation,
    2341                 :             :                                                                             NULL),
    2342                 :             :                                                      true);
    2343                 :         298 :                         var->isconst = true;
    2344                 :         298 :                         new->sqlstate_varno = var->dno;
    2345                 :             : 
    2346                 :         298 :                         var = plpgsql_build_variable("sqlerrm", lineno,
    2347                 :             :                                                      plpgsql_build_datatype(TEXTOID,
    2348                 :             :                                                                             -1,
    2349                 :         298 :                                                                             plpgsql_curr_compile->fn_input_collation,
    2350                 :             :                                                                             NULL),
    2351                 :             :                                                      true);
    2352                 :         298 :                         var->isconst = true;
    2353                 :         298 :                         new->sqlerrm_varno = var->dno;
    2354                 :             : 
    2355                 :         298 :                         $<exception_block>$ = new;
    2356                 :             :                     }
    2357                 :             :                     proc_exceptions
    2358                 :             :                     {
    2359                 :         298 :                         PLpgSQL_exception_block *new = $<exception_block>2;
    2360                 :         298 :                         new->exc_list = $3;
    2361                 :             : 
    2362                 :         298 :                         $$ = new;
    2363                 :             :                     }
    2364                 :             :                 ;
    2365                 :             : 
    2366                 :             : proc_exceptions : proc_exceptions proc_exception
    2367                 :             :                         {
    2368                 :           7 :                             $$ = lappend($1, $2);
    2369                 :             :                         }
    2370                 :             :                 | proc_exception
    2371                 :             :                         {
    2372                 :         298 :                             $$ = list_make1($1);
    2373                 :             :                         }
    2374                 :             :                 ;
    2375                 :             : 
    2376                 :             : proc_exception  : K_WHEN proc_conditions K_THEN proc_sect
    2377                 :             :                     {
    2378                 :             :                         PLpgSQL_exception *new;
    2379                 :             : 
    2380                 :         305 :                         new = palloc0_object(PLpgSQL_exception);
    2381                 :         305 :                         new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
    2382                 :         305 :                         new->conditions = $2;
    2383                 :         305 :                         new->action = $4;
    2384                 :             : 
    2385                 :         305 :                         $$ = new;
    2386                 :             :                     }
    2387                 :             :                 ;
    2388                 :             : 
    2389                 :             : proc_conditions : proc_conditions K_OR proc_condition
    2390                 :             :                         {
    2391                 :             :                             PLpgSQL_condition   *old;
    2392                 :             : 
    2393         [ -  + ]:           5 :                             for (old = $1; old->next != NULL; old = old->next)
    2394                 :             :                                 /* skip */ ;
    2395                 :           5 :                             old->next = $3;
    2396                 :           5 :                             $$ = $1;
    2397                 :             :                         }
    2398                 :             :                 | proc_condition
    2399                 :             :                         {
    2400                 :         305 :                             $$ = $1;
    2401                 :             :                         }
    2402                 :             :                 ;
    2403                 :             : 
    2404                 :             : proc_condition  : any_identifier
    2405                 :             :                         {
    2406         [ +  + ]:         310 :                             if (strcmp($1, "sqlstate") != 0)
    2407                 :             :                             {
    2408                 :         292 :                                 $$ = plpgsql_parse_err_condition($1);
    2409                 :             :                             }
    2410                 :             :                             else
    2411                 :             :                             {
    2412                 :             :                                 PLpgSQL_condition *new;
    2413                 :             :                                 char   *sqlstatestr;
    2414                 :             : 
    2415                 :             :                                 /* next token should be a string literal */
    2416         [ -  + ]:          18 :                                 if (yylex(&yylval, &yylloc, yyscanner) != SCONST)
    2417                 :           0 :                                     yyerror(&yylloc, NULL, yyscanner, "syntax error");
    2418                 :          18 :                                 sqlstatestr = yylval.str;
    2419                 :             : 
    2420         [ -  + ]:          18 :                                 if (strlen(sqlstatestr) != 5)
    2421                 :           0 :                                     yyerror(&yylloc, NULL, yyscanner, "invalid SQLSTATE code");
    2422         [ -  + ]:          18 :                                 if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
    2423                 :           0 :                                     yyerror(&yylloc, NULL, yyscanner, "invalid SQLSTATE code");
    2424                 :             : 
    2425                 :          18 :                                 new = palloc_object(PLpgSQL_condition);
    2426                 :          18 :                                 new->sqlerrstate =
    2427                 :          18 :                                     MAKE_SQLSTATE(sqlstatestr[0],
    2428                 :             :                                                   sqlstatestr[1],
    2429                 :             :                                                   sqlstatestr[2],
    2430                 :             :                                                   sqlstatestr[3],
    2431                 :             :                                                   sqlstatestr[4]);
    2432                 :          18 :                                 new->condname = sqlstatestr;
    2433                 :          18 :                                 new->next = NULL;
    2434                 :             : 
    2435                 :          18 :                                 $$ = new;
    2436                 :             :                             }
    2437                 :             :                         }
    2438                 :             :                 ;
    2439                 :             : 
    2440                 :             : expr_until_semi :
    2441                 :          93 :                     { $$ = read_sql_expression(';', ";", &yylval, &yylloc, yyscanner); }
    2442                 :             :                 ;
    2443                 :             : 
    2444                 :             : expr_until_then :
    2445                 :        4459 :                     { $$ = read_sql_expression(K_THEN, "THEN", &yylval, &yylloc, yyscanner); }
    2446                 :             :                 ;
    2447                 :             : 
    2448                 :             : expr_until_loop :
    2449                 :          92 :                     { $$ = read_sql_expression(K_LOOP, "LOOP", &yylval, &yylloc, yyscanner); }
    2450                 :             :                 ;
    2451                 :             : 
    2452                 :             : opt_block_label :
    2453                 :             :                     {
    2454                 :        6141 :                         plpgsql_ns_push(NULL, PLPGSQL_LABEL_BLOCK);
    2455                 :        6141 :                         $$ = NULL;
    2456                 :             :                     }
    2457                 :             :                 | LESS_LESS any_identifier GREATER_GREATER
    2458                 :             :                     {
    2459                 :          35 :                         plpgsql_ns_push($2, PLPGSQL_LABEL_BLOCK);
    2460                 :          35 :                         $$ = $2;
    2461                 :             :                     }
    2462                 :             :                 ;
    2463                 :             : 
    2464                 :             : opt_loop_label  :
    2465                 :             :                     {
    2466                 :        1081 :                         plpgsql_ns_push(NULL, PLPGSQL_LABEL_LOOP);
    2467                 :        1081 :                         $$ = NULL;
    2468                 :             :                     }
    2469                 :             :                 | LESS_LESS any_identifier GREATER_GREATER
    2470                 :             :                     {
    2471                 :          15 :                         plpgsql_ns_push($2, PLPGSQL_LABEL_LOOP);
    2472                 :          15 :                         $$ = $2;
    2473                 :             :                     }
    2474                 :             :                 ;
    2475                 :             : 
    2476                 :             : opt_label   :
    2477                 :             :                     {
    2478                 :        7276 :                         $$ = NULL;
    2479                 :             :                     }
    2480                 :             :                 | any_identifier
    2481                 :             :                     {
    2482                 :             :                         /* label validity will be checked by outer production */
    2483                 :          21 :                         $$ = $1;
    2484                 :             :                     }
    2485                 :             :                 ;
    2486                 :             : 
    2487                 :             : opt_exitcond    : ';'
    2488                 :          48 :                     { $$ = NULL; }
    2489                 :             :                 | K_WHEN expr_until_semi
    2490                 :          93 :                     { $$ = $2; }
    2491                 :             :                 ;
    2492                 :             : 
    2493                 :             : /*
    2494                 :             :  * need to allow DATUM because scanner will have tried to resolve as variable
    2495                 :             :  */
    2496                 :             : any_identifier  : T_WORD
    2497                 :             :                     {
    2498                 :         363 :                         $$ = $1.ident;
    2499                 :             :                     }
    2500                 :             :                 | unreserved_keyword
    2501                 :             :                     {
    2502                 :           0 :                         $$ = pstrdup($1);
    2503                 :             :                     }
    2504                 :             :                 | T_DATUM
    2505                 :             :                     {
    2506         [ -  + ]:          18 :                         if ($1.ident == NULL) /* composite name not OK */
    2507                 :           0 :                             yyerror(&yylloc, NULL, yyscanner, "syntax error");
    2508                 :          18 :                         $$ = $1.ident;
    2509                 :             :                     }
    2510                 :             :                 ;
    2511                 :             : 
    2512                 :             : unreserved_keyword  :
    2513                 :             :                 K_ABSOLUTE
    2514                 :             :                 | K_ALIAS
    2515                 :             :                 | K_AND
    2516                 :             :                 | K_ARRAY
    2517                 :             :                 | K_ASSERT
    2518                 :             :                 | K_BACKWARD
    2519                 :             :                 | K_CALL
    2520                 :             :                 | K_CHAIN
    2521                 :             :                 | K_CLOSE
    2522                 :             :                 | K_COLLATE
    2523                 :             :                 | K_COLUMN
    2524                 :             :                 | K_COLUMN_NAME
    2525                 :             :                 | K_COMMIT
    2526                 :             :                 | K_CONSTANT
    2527                 :             :                 | K_CONSTRAINT
    2528                 :             :                 | K_CONSTRAINT_NAME
    2529                 :             :                 | K_CONTINUE
    2530                 :             :                 | K_CURRENT
    2531                 :             :                 | K_CURSOR
    2532                 :             :                 | K_DATATYPE
    2533                 :             :                 | K_DEBUG
    2534                 :             :                 | K_DEFAULT
    2535                 :             :                 | K_DETAIL
    2536                 :             :                 | K_DIAGNOSTICS
    2537                 :             :                 | K_DO
    2538                 :             :                 | K_DUMP
    2539                 :             :                 | K_ELSIF
    2540                 :             :                 | K_ERRCODE
    2541                 :             :                 | K_ERROR
    2542                 :             :                 | K_EXCEPTION
    2543                 :             :                 | K_EXECUTE
    2544                 :             :                 | K_EXIT
    2545                 :             :                 | K_FETCH
    2546                 :             :                 | K_FIRST
    2547                 :             :                 | K_FORWARD
    2548                 :             :                 | K_GET
    2549                 :             :                 | K_HINT
    2550                 :             :                 | K_IMPORT
    2551                 :             :                 | K_INFO
    2552                 :             :                 | K_INSERT
    2553                 :             :                 | K_IS
    2554                 :             :                 | K_LAST
    2555                 :             :                 | K_LOG
    2556                 :             :                 | K_MERGE
    2557                 :             :                 | K_MESSAGE
    2558                 :             :                 | K_MESSAGE_TEXT
    2559                 :             :                 | K_MOVE
    2560                 :             :                 | K_NEXT
    2561                 :             :                 | K_NO
    2562                 :             :                 | K_NOTICE
    2563                 :             :                 | K_OPEN
    2564                 :             :                 | K_OPTION
    2565                 :             :                 | K_PERFORM
    2566                 :             :                 | K_PG_CONTEXT
    2567                 :             :                 | K_PG_DATATYPE_NAME
    2568                 :             :                 | K_PG_EXCEPTION_CONTEXT
    2569                 :             :                 | K_PG_EXCEPTION_DETAIL
    2570                 :             :                 | K_PG_EXCEPTION_HINT
    2571                 :             :                 | K_PG_ROUTINE_OID
    2572                 :             :                 | K_PRINT_STRICT_PARAMS
    2573                 :             :                 | K_PRIOR
    2574                 :             :                 | K_QUERY
    2575                 :             :                 | K_RAISE
    2576                 :             :                 | K_RELATIVE
    2577                 :             :                 | K_RETURN
    2578                 :             :                 | K_RETURNED_SQLSTATE
    2579                 :             :                 | K_REVERSE
    2580                 :             :                 | K_ROLLBACK
    2581                 :             :                 | K_ROW_COUNT
    2582                 :             :                 | K_ROWTYPE
    2583                 :             :                 | K_SCHEMA
    2584                 :             :                 | K_SCHEMA_NAME
    2585                 :             :                 | K_SCROLL
    2586                 :             :                 | K_SLICE
    2587                 :             :                 | K_SQLSTATE
    2588                 :             :                 | K_STACKED
    2589                 :             :                 | K_STRICT
    2590                 :             :                 | K_TABLE
    2591                 :             :                 | K_TABLE_NAME
    2592                 :             :                 | K_TYPE
    2593                 :             :                 | K_USE_COLUMN
    2594                 :             :                 | K_USE_VARIABLE
    2595                 :             :                 | K_VARIABLE_CONFLICT
    2596                 :             :                 | K_WARNING
    2597                 :             :                 ;
    2598                 :             : 
    2599                 :             : %%
    2600                 :             : 
    2601                 :             : /*
    2602                 :             :  * Check whether a token represents an "unreserved keyword".
    2603                 :             :  * We have various places where we want to recognize a keyword in preference
    2604                 :             :  * to a variable name, but not reserve that keyword in other contexts.
    2605                 :             :  * Hence, this kluge.
    2606                 :             :  */
    2607                 :             : static bool
    2608                 :       28717 : tok_is_keyword(int token, union YYSTYPE *lval,
    2609                 :             :                int kw_token, const char *kw_str)
    2610                 :             : {
    2611         [ +  + ]:       28717 :     if (token == kw_token)
    2612                 :             :     {
    2613                 :             :         /* Normal case, was recognized by scanner (no conflicting variable) */
    2614                 :        5387 :         return true;
    2615                 :             :     }
    2616         [ +  + ]:       23330 :     else if (token == T_DATUM)
    2617                 :             :     {
    2618                 :             :         /*
    2619                 :             :          * It's a variable, so recheck the string name.  Note we will not
    2620                 :             :          * match composite names (hence an unreserved word followed by "."
    2621                 :             :          * will not be recognized).
    2622                 :             :          */
    2623   [ +  -  +  + ]:        8027 :         if (!lval->wdatum.quoted && lval->wdatum.ident != NULL &&
    2624         [ +  + ]:        7863 :             strcmp(lval->wdatum.ident, kw_str) == 0)
    2625                 :           4 :             return true;
    2626                 :             :     }
    2627                 :       23326 :     return false;               /* not the keyword */
    2628                 :             : }
    2629                 :             : 
    2630                 :             : /*
    2631                 :             :  * Convenience routine to complain when we expected T_DATUM and got T_WORD,
    2632                 :             :  * ie, unrecognized variable.
    2633                 :             :  */
    2634                 :             : static void
    2635                 :           0 : word_is_not_variable(PLword *word, int location, yyscan_t yyscanner)
    2636                 :             : {
    2637         [ #  # ]:           0 :     ereport(ERROR,
    2638                 :             :             (errcode(ERRCODE_SYNTAX_ERROR),
    2639                 :             :              errmsg("\"%s\" is not a known variable",
    2640                 :             :                     word->ident),
    2641                 :             :              parser_errposition(location)));
    2642                 :             : }
    2643                 :             : 
    2644                 :             : /* Same, for a CWORD */
    2645                 :             : static void
    2646                 :           2 : cword_is_not_variable(PLcword *cword, int location, yyscan_t yyscanner)
    2647                 :             : {
    2648         [ +  - ]:           2 :     ereport(ERROR,
    2649                 :             :             (errcode(ERRCODE_SYNTAX_ERROR),
    2650                 :             :              errmsg("\"%s\" is not a known variable",
    2651                 :             :                     NameListToString(cword->idents)),
    2652                 :             :              parser_errposition(location)));
    2653                 :             : }
    2654                 :             : 
    2655                 :             : /*
    2656                 :             :  * Convenience routine to complain when we expected T_DATUM and got
    2657                 :             :  * something else.  "tok" must be the current token, since we also
    2658                 :             :  * look at yylval and yylloc.
    2659                 :             :  */
    2660                 :             : static void
    2661                 :           0 : current_token_is_not_variable(int tok, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    2662                 :             : {
    2663         [ #  # ]:           0 :     if (tok == T_WORD)
    2664                 :           0 :         word_is_not_variable(&(yylvalp->word), *yyllocp, yyscanner);
    2665         [ #  # ]:           0 :     else if (tok == T_CWORD)
    2666                 :           0 :         cword_is_not_variable(&(yylvalp->cword), *yyllocp, yyscanner);
    2667                 :             :     else
    2668                 :           0 :         yyerror(yyllocp, NULL, yyscanner, "syntax error");
    2669                 :           0 : }
    2670                 :             : 
    2671                 :             : /* Convenience routine to construct a PLpgSQL_expr struct */
    2672                 :             : static PLpgSQL_expr *
    2673                 :       27242 : make_plpgsql_expr(const char *query,
    2674                 :             :                   RawParseMode parsemode)
    2675                 :             : {
    2676                 :       27242 :     PLpgSQL_expr *expr = palloc0_object(PLpgSQL_expr);
    2677                 :             : 
    2678                 :       27242 :     expr->query = pstrdup(query);
    2679                 :       27242 :     expr->parseMode = parsemode;
    2680                 :       27242 :     expr->func = plpgsql_curr_compile;
    2681                 :       27242 :     expr->ns = plpgsql_ns_top();
    2682                 :             :     /* might get changed later during parsing: */
    2683                 :       27242 :     expr->target_param = -1;
    2684                 :       27242 :     expr->target_is_local = false;
    2685                 :             :     /* other fields are left as zeroes until first execution */
    2686                 :       27242 :     return expr;
    2687                 :             : }
    2688                 :             : 
    2689                 :             : /* Mark a PLpgSQL_expr as being the source of an assignment to target */
    2690                 :             : static void
    2691                 :        5031 : mark_expr_as_assignment_source(PLpgSQL_expr *expr, PLpgSQL_datum *target)
    2692                 :             : {
    2693                 :             :     /*
    2694                 :             :      * Mark the expression as being an assignment source, if target is a
    2695                 :             :      * simple variable.  We don't currently support optimized assignments to
    2696                 :             :      * other DTYPEs, so no need to mark in other cases.
    2697                 :             :      */
    2698         [ +  + ]:        5031 :     if (target->dtype == PLPGSQL_DTYPE_VAR)
    2699                 :             :     {
    2700                 :        3791 :         expr->target_param = target->dno;
    2701                 :             : 
    2702                 :             :         /*
    2703                 :             :          * For now, assume the target is local to the nearest enclosing
    2704                 :             :          * exception block.  That's correct if the function contains no
    2705                 :             :          * exception blocks; otherwise we'll update this later.
    2706                 :             :          */
    2707                 :        3791 :         expr->target_is_local = true;
    2708                 :             :     }
    2709                 :             :     else
    2710                 :             :     {
    2711                 :        1240 :         expr->target_param = -1; /* should be that already */
    2712                 :        1240 :         expr->target_is_local = false; /* ditto */
    2713                 :             :     }
    2714                 :        5031 : }
    2715                 :             : 
    2716                 :             : /* Convenience routine to read an expression with one possible terminator */
    2717                 :             : static PLpgSQL_expr *
    2718                 :        7579 : read_sql_expression(int until, const char *expected, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    2719                 :             : {
    2720                 :        7579 :     return read_sql_construct(until, 0, 0, expected,
    2721                 :             :                               RAW_PARSE_PLPGSQL_EXPR,
    2722                 :             :                               true, true, NULL, NULL,
    2723                 :             :                               yylvalp, yyllocp, yyscanner);
    2724                 :             : }
    2725                 :             : 
    2726                 :             : /* Convenience routine to read an expression with two possible terminators */
    2727                 :             : static PLpgSQL_expr *
    2728                 :         970 : read_sql_expression2(int until, int until2, const char *expected,
    2729                 :             :                      int *endtoken, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    2730                 :             : {
    2731                 :         970 :     return read_sql_construct(until, until2, 0, expected,
    2732                 :             :                               RAW_PARSE_PLPGSQL_EXPR,
    2733                 :             :                               true, true, NULL, endtoken,
    2734                 :             :                               yylvalp, yyllocp, yyscanner);
    2735                 :             : }
    2736                 :             : 
    2737                 :             : /* Convenience routine to read a SQL statement that must end with ';' */
    2738                 :             : static PLpgSQL_expr *
    2739                 :         215 : read_sql_stmt(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    2740                 :             : {
    2741                 :         215 :     return read_sql_construct(';', 0, 0, ";",
    2742                 :             :                               RAW_PARSE_DEFAULT,
    2743                 :             :                               false, true, NULL, NULL,
    2744                 :             :                               yylvalp, yyllocp, yyscanner);
    2745                 :             : }
    2746                 :             : 
    2747                 :             : /*
    2748                 :             :  * Read a SQL construct and build a PLpgSQL_expr for it.
    2749                 :             :  *
    2750                 :             :  * until:       token code for expected terminator
    2751                 :             :  * until2:      token code for alternate terminator (pass 0 if none)
    2752                 :             :  * until3:      token code for another alternate terminator (pass 0 if none)
    2753                 :             :  * expected:    text to use in complaining that terminator was not found
    2754                 :             :  * parsemode:   raw_parser() mode to use
    2755                 :             :  * isexpression: whether to say we're reading an "expression" or a "statement"
    2756                 :             :  * valid_sql:   whether to check the syntax of the expr
    2757                 :             :  * startloc:    if not NULL, location of first token is stored at *startloc
    2758                 :             :  * endtoken:    if not NULL, ending token is stored at *endtoken
    2759                 :             :  *              (this is only interesting if until2 or until3 isn't zero)
    2760                 :             :  */
    2761                 :             : static PLpgSQL_expr *
    2762                 :       24642 : read_sql_construct(int until,
    2763                 :             :                    int until2,
    2764                 :             :                    int until3,
    2765                 :             :                    const char *expected,
    2766                 :             :                    RawParseMode parsemode,
    2767                 :             :                    bool isexpression,
    2768                 :             :                    bool valid_sql,
    2769                 :             :                    int *startloc,
    2770                 :             :                    int *endtoken,
    2771                 :             :                    YYSTYPE *yylvalp, YYLTYPE *yyllocp,
    2772                 :             :                    yyscan_t yyscanner)
    2773                 :             : {
    2774                 :             :     int         tok;
    2775                 :             :     StringInfoData ds;
    2776                 :             :     IdentifierLookup save_IdentifierLookup;
    2777                 :       24642 :     int         startlocation = -1;
    2778                 :       24642 :     int         endlocation = -1;
    2779                 :       24642 :     int         parenlevel = 0;
    2780                 :             :     PLpgSQL_expr *expr;
    2781                 :             : 
    2782                 :       24642 :     initStringInfo(&ds);
    2783                 :             : 
    2784                 :             :     /* special lookup mode for identifiers within the SQL text */
    2785                 :       24642 :     save_IdentifierLookup = plpgsql_IdentifierLookup;
    2786                 :       24642 :     plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_EXPR;
    2787                 :             : 
    2788                 :             :     for (;;)
    2789                 :             :     {
    2790                 :      105337 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    2791         [ +  + ]:      105337 :         if (startlocation < 0)   /* remember loc of first token */
    2792                 :       24642 :             startlocation = *yyllocp;
    2793                 :             :         /* until2 and until3 are zero if unused; don't let them match EOF */
    2794   [ +  +  +  + ]:      105337 :         if (tok == until && parenlevel == 0)
    2795                 :       20115 :             break;
    2796   [ +  +  +  +  :       85222 :         if (until2 != 0 && tok == until2 && parenlevel == 0)
                   +  - ]
    2797                 :        4177 :             break;
    2798   [ +  +  +  +  :       81045 :         if (until3 != 0 && tok == until3 && parenlevel == 0)
                   +  - ]
    2799                 :         348 :             break;
    2800   [ +  +  +  + ]:       80697 :         if (tok == '(' || tok == '[')
    2801                 :        6283 :             parenlevel++;
    2802   [ +  +  +  + ]:       74414 :         else if (tok == ')' || tok == ']')
    2803                 :             :         {
    2804                 :        6283 :             parenlevel--;
    2805         [ -  + ]:        6283 :             if (parenlevel < 0)
    2806                 :           0 :                 yyerror(yyllocp, NULL, yyscanner, "mismatched parentheses");
    2807                 :             :         }
    2808                 :             : 
    2809                 :             :         /*
    2810                 :             :          * End of function definition is an error, and we don't expect to hit
    2811                 :             :          * a semicolon either (unless it's the until symbol, in which case we
    2812                 :             :          * should have fallen out above).
    2813                 :             :          */
    2814   [ +  +  -  + ]:       80697 :         if (tok == 0 || tok == ';')
    2815                 :             :         {
    2816         [ -  + ]:           2 :             if (parenlevel != 0)
    2817                 :           0 :                 yyerror(yyllocp, NULL, yyscanner, "mismatched parentheses");
    2818         [ +  + ]:           2 :             if (isexpression)
    2819         [ +  - ]:           1 :                 ereport(ERROR,
    2820                 :             :                         (errcode(ERRCODE_SYNTAX_ERROR),
    2821                 :             :                          errmsg("missing \"%s\" at end of SQL expression",
    2822                 :             :                                 expected),
    2823                 :             :                          parser_errposition(*yyllocp)));
    2824                 :             :             else
    2825         [ +  - ]:           1 :                 ereport(ERROR,
    2826                 :             :                         (errcode(ERRCODE_SYNTAX_ERROR),
    2827                 :             :                          errmsg("missing \"%s\" at end of SQL statement",
    2828                 :             :                                 expected),
    2829                 :             :                          parser_errposition(*yyllocp)));
    2830                 :             :         }
    2831                 :             :         /* Remember end+1 location of last accepted token */
    2832                 :       80695 :         endlocation = *yyllocp + plpgsql_token_length(yyscanner);
    2833                 :             :     }
    2834                 :             : 
    2835                 :       24640 :     plpgsql_IdentifierLookup = save_IdentifierLookup;
    2836                 :             : 
    2837         [ +  + ]:       24640 :     if (startloc)
    2838                 :        1507 :         *startloc = startlocation;
    2839         [ +  + ]:       24640 :     if (endtoken)
    2840                 :       11540 :         *endtoken = tok;
    2841                 :             : 
    2842                 :             :     /* give helpful complaint about empty input */
    2843         [ +  + ]:       24640 :     if (startlocation >= endlocation)
    2844                 :             :     {
    2845         [ +  - ]:           4 :         if (isexpression)
    2846                 :           4 :             yyerror(yyllocp, NULL, yyscanner, "missing expression");
    2847                 :             :         else
    2848                 :           0 :             yyerror(yyllocp, NULL, yyscanner, "missing SQL statement");
    2849                 :             :     }
    2850                 :             : 
    2851                 :             :     /*
    2852                 :             :      * We save only the text from startlocation to endlocation-1.  This
    2853                 :             :      * suppresses the "until" token as well as any whitespace or comments
    2854                 :             :      * following the last accepted token.  (We used to strip such trailing
    2855                 :             :      * whitespace by hand, but that causes problems if there's a "-- comment"
    2856                 :             :      * in front of said whitespace.)
    2857                 :             :      */
    2858                 :       24636 :     plpgsql_append_source_text(&ds, startlocation, endlocation, yyscanner);
    2859                 :             : 
    2860                 :       24636 :     expr = make_plpgsql_expr(ds.data, parsemode);
    2861                 :       24636 :     pfree(ds.data);
    2862                 :             : 
    2863         [ +  + ]:       24636 :     if (valid_sql)
    2864                 :       23129 :         check_sql_expr(expr->query, expr->parseMode, startlocation, yyscanner);
    2865                 :             : 
    2866                 :       24636 :     return expr;
    2867                 :             : }
    2868                 :             : 
    2869                 :             : /*
    2870                 :             :  * Read a datatype declaration, consuming the current lookahead token if any.
    2871                 :             :  * Returns a PLpgSQL_type struct.
    2872                 :             :  */
    2873                 :             : static PLpgSQL_type *
    2874                 :        3699 : read_datatype(int tok, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    2875                 :             : {
    2876                 :             :     StringInfoData ds;
    2877                 :             :     char       *type_name;
    2878                 :             :     int         startlocation;
    2879                 :        3699 :     PLpgSQL_type *result = NULL;
    2880                 :        3699 :     int         parenlevel = 0;
    2881                 :             : 
    2882                 :             :     /* Should only be called while parsing DECLARE sections */
    2883                 :             :     Assert(plpgsql_IdentifierLookup == IDENTIFIER_LOOKUP_DECLARE);
    2884                 :             : 
    2885                 :             :     /* Often there will be a lookahead token, but if not, get one */
    2886         [ +  + ]:        3699 :     if (tok == YYEMPTY)
    2887                 :         112 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    2888                 :             : 
    2889                 :             :     /* The current token is the start of what we'll pass to parse_datatype */
    2890                 :        3699 :     startlocation = *yyllocp;
    2891                 :             : 
    2892                 :             :     /*
    2893                 :             :      * If we have a simple or composite identifier, check for %TYPE and
    2894                 :             :      * %ROWTYPE constructs.
    2895                 :             :      */
    2896         [ +  + ]:        3699 :     if (tok == T_WORD)
    2897                 :             :     {
    2898                 :        3656 :         char       *dtname = yylvalp->word.ident;
    2899                 :             : 
    2900                 :        3656 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    2901         [ +  + ]:        3656 :         if (tok == '%')
    2902                 :             :         {
    2903                 :          63 :             tok = yylex(yylvalp, yyllocp, yyscanner);
    2904         [ +  + ]:          63 :             if (tok_is_keyword(tok, yylvalp,
    2905                 :             :                                K_TYPE, "type"))
    2906                 :          25 :                 result = plpgsql_parse_wordtype(dtname);
    2907         [ +  - ]:          38 :             else if (tok_is_keyword(tok, yylvalp,
    2908                 :             :                                     K_ROWTYPE, "rowtype"))
    2909                 :          38 :                 result = plpgsql_parse_wordrowtype(dtname);
    2910                 :             :         }
    2911                 :             :     }
    2912         [ +  + ]:          43 :     else if (plpgsql_token_is_unreserved_keyword(tok))
    2913                 :             :     {
    2914                 :           2 :         char       *dtname = pstrdup(yylvalp->keyword);
    2915                 :             : 
    2916                 :           2 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    2917         [ +  - ]:           2 :         if (tok == '%')
    2918                 :             :         {
    2919                 :           2 :             tok = yylex(yylvalp, yyllocp, yyscanner);
    2920         [ +  + ]:           2 :             if (tok_is_keyword(tok, yylvalp,
    2921                 :             :                                K_TYPE, "type"))
    2922                 :           1 :                 result = plpgsql_parse_wordtype(dtname);
    2923         [ +  - ]:           1 :             else if (tok_is_keyword(tok, yylvalp,
    2924                 :             :                                     K_ROWTYPE, "rowtype"))
    2925                 :           1 :                 result = plpgsql_parse_wordrowtype(dtname);
    2926                 :             :         }
    2927                 :             :     }
    2928         [ +  - ]:          41 :     else if (tok == T_CWORD)
    2929                 :             :     {
    2930                 :          41 :         List       *dtnames = yylvalp->cword.idents;
    2931                 :             : 
    2932                 :          41 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    2933         [ +  + ]:          41 :         if (tok == '%')
    2934                 :             :         {
    2935                 :          31 :             tok = yylex(yylvalp, yyllocp, yyscanner);
    2936         [ +  + ]:          31 :             if (tok_is_keyword(tok, yylvalp,
    2937                 :             :                                K_TYPE, "type"))
    2938                 :          22 :                 result = plpgsql_parse_cwordtype(dtnames);
    2939         [ +  - ]:           9 :             else if (tok_is_keyword(tok, yylvalp,
    2940                 :             :                                     K_ROWTYPE, "rowtype"))
    2941                 :           9 :                 result = plpgsql_parse_cwordrowtype(dtnames);
    2942                 :             :         }
    2943                 :             :     }
    2944                 :             : 
    2945                 :             :     /*
    2946                 :             :      * If we recognized a %TYPE or %ROWTYPE construct, see if it is followed
    2947                 :             :      * by array decoration: [ ARRAY ] [ '[' [ iconst ] ']' [ ... ] ]
    2948                 :             :      *
    2949                 :             :      * Like the core parser, we ignore the specific numbers and sizes of
    2950                 :             :      * dimensions; arrays of different dimensionality are still the same type
    2951                 :             :      * in Postgres.
    2952                 :             :      */
    2953         [ +  + ]:        3688 :     if (result)
    2954                 :             :     {
    2955                 :          85 :         bool        is_array = false;
    2956                 :             : 
    2957                 :          85 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    2958         [ +  + ]:          85 :         if (tok_is_keyword(tok, yylvalp,
    2959                 :             :                            K_ARRAY, "array"))
    2960                 :             :         {
    2961                 :           4 :             is_array = true;
    2962                 :           4 :             tok = yylex(yylvalp, yyllocp, yyscanner);
    2963                 :             :         }
    2964         [ +  + ]:         101 :         while (tok == '[')
    2965                 :             :         {
    2966                 :          16 :             is_array = true;
    2967                 :          16 :             tok = yylex(yylvalp, yyllocp, yyscanner);
    2968         [ +  + ]:          16 :             if (tok == ICONST)
    2969                 :           6 :                 tok = yylex(yylvalp, yyllocp, yyscanner);
    2970         [ -  + ]:          16 :             if (tok != ']')
    2971                 :           0 :                 yyerror(yyllocp, NULL, yyscanner, "syntax error, expected \"]\"");
    2972                 :          16 :             tok = yylex(yylvalp, yyllocp, yyscanner);
    2973                 :             :         }
    2974                 :          85 :         plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    2975                 :             : 
    2976         [ +  + ]:          85 :         if (is_array)
    2977                 :          14 :             result = plpgsql_build_datatype_arrayof(result);
    2978                 :             : 
    2979                 :          84 :         return result;
    2980                 :             :     }
    2981                 :             : 
    2982                 :             :     /*
    2983                 :             :      * Not %TYPE or %ROWTYPE, so scan to the end of the datatype declaration,
    2984                 :             :      * which could include typmod or array decoration.  We are not very picky
    2985                 :             :      * here, instead relying on parse_datatype to complain about garbage.  But
    2986                 :             :      * we must count parens to handle typmods within cursor_arg correctly.
    2987                 :             :      */
    2988         [ +  + ]:        4190 :     while (tok != ';')
    2989                 :             :     {
    2990         [ -  + ]:        1215 :         if (tok == 0)
    2991                 :             :         {
    2992         [ #  # ]:           0 :             if (parenlevel != 0)
    2993                 :           0 :                 yyerror(yyllocp, NULL, yyscanner, "mismatched parentheses");
    2994                 :             :             else
    2995                 :           0 :                 yyerror(yyllocp, NULL, yyscanner, "incomplete data type declaration");
    2996                 :             :         }
    2997                 :             :         /* Possible followers for datatype in a declaration */
    2998   [ +  +  +  +  :        1215 :         if (tok == K_COLLATE || tok == K_NOT ||
                   +  + ]
    2999   [ +  +  +  - ]:        1096 :             tok == '=' || tok == COLON_EQUALS || tok == K_DEFAULT)
    3000                 :             :             break;
    3001                 :             :         /* Possible followers for datatype in a cursor_arg list */
    3002   [ +  +  +  +  :         683 :         if ((tok == ',' || tok == ')') && parenlevel == 0)
                   +  + ]
    3003                 :          96 :             break;
    3004         [ +  + ]:         587 :         if (tok == '(')
    3005                 :          76 :             parenlevel++;
    3006         [ +  + ]:         511 :         else if (tok == ')')
    3007                 :          76 :             parenlevel--;
    3008                 :             : 
    3009                 :         587 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    3010                 :             :     }
    3011                 :             : 
    3012                 :             :     /* set up ds to contain complete typename text */
    3013                 :        3603 :     initStringInfo(&ds);
    3014                 :        3603 :     plpgsql_append_source_text(&ds, startlocation, *yyllocp, yyscanner);
    3015                 :        3603 :     type_name = ds.data;
    3016                 :             : 
    3017         [ -  + ]:        3603 :     if (type_name[0] == '\0')
    3018                 :           0 :         yyerror(yyllocp, NULL, yyscanner, "missing data type declaration");
    3019                 :             : 
    3020                 :        3603 :     result = parse_datatype(type_name, startlocation, yyscanner);
    3021                 :             : 
    3022                 :        3602 :     pfree(ds.data);
    3023                 :             : 
    3024                 :        3602 :     plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    3025                 :             : 
    3026                 :        3602 :     return result;
    3027                 :             : }
    3028                 :             : 
    3029                 :             : /*
    3030                 :             :  * Read a generic SQL statement.  We have already read its first token;
    3031                 :             :  * firsttoken is that token's code and location its starting location.
    3032                 :             :  * If firsttoken == T_WORD, pass its yylval value as "word", else pass NULL.
    3033                 :             :  */
    3034                 :             : static PLpgSQL_stmt *
    3035                 :        2574 : make_execsql_stmt(int firsttoken, int location, PLword *word, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    3036                 :             : {
    3037                 :             :     StringInfoData ds;
    3038                 :             :     IdentifierLookup save_IdentifierLookup;
    3039                 :             :     PLpgSQL_stmt_execsql *execsql;
    3040                 :             :     PLpgSQL_expr *expr;
    3041                 :        2574 :     PLpgSQL_variable *target = NULL;
    3042                 :             :     int         tok;
    3043                 :             :     int         prev_tok;
    3044                 :        2574 :     bool        have_into = false;
    3045                 :        2574 :     bool        have_strict = false;
    3046                 :        2574 :     int         into_start_loc = -1;
    3047                 :        2574 :     int         into_end_loc = -1;
    3048                 :        2574 :     int         paren_depth = 0;
    3049                 :        2574 :     int         begin_depth = 0;
    3050                 :        2574 :     bool        in_routine_definition = false;
    3051                 :        2574 :     int         token_count = 0;
    3052                 :             :     char        tokens[4];      /* records the first few tokens */
    3053                 :             : 
    3054                 :        2574 :     initStringInfo(&ds);
    3055                 :             : 
    3056                 :        2574 :     memset(tokens, 0, sizeof(tokens));
    3057                 :             : 
    3058                 :             :     /* special lookup mode for identifiers within the SQL text */
    3059                 :        2574 :     save_IdentifierLookup = plpgsql_IdentifierLookup;
    3060                 :        2574 :     plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_EXPR;
    3061                 :             : 
    3062                 :             :     /*
    3063                 :             :      * Scan to the end of the SQL command.  Identify any INTO-variables clause
    3064                 :             :      * lurking within it, and parse that via read_into_target().
    3065                 :             :      *
    3066                 :             :      * The end of the statement is defined by a semicolon ... except that
    3067                 :             :      * semicolons within parentheses or BEGIN/END blocks don't terminate a
    3068                 :             :      * statement.  We follow psql's lead in not recognizing BEGIN/END except
    3069                 :             :      * after CREATE [OR REPLACE] {FUNCTION|PROCEDURE}.  END can also appear
    3070                 :             :      * within a CASE construct, so we treat CASE/END like BEGIN/END.
    3071                 :             :      *
    3072                 :             :      * Because INTO is sometimes used in the main SQL grammar, we have to be
    3073                 :             :      * careful not to take any such usage of INTO as a PL/pgSQL INTO clause.
    3074                 :             :      * There are currently three such cases:
    3075                 :             :      *
    3076                 :             :      * 1. SELECT ... INTO.  We don't care, we just override that with the
    3077                 :             :      * PL/pgSQL definition.
    3078                 :             :      *
    3079                 :             :      * 2. INSERT INTO.  This is relatively easy to recognize since the words
    3080                 :             :      * must appear adjacently; but we can't assume INSERT starts the command,
    3081                 :             :      * because it can appear in CREATE RULE or WITH.  Unfortunately, INSERT is
    3082                 :             :      * *not* fully reserved, so that means there is a chance of a false match;
    3083                 :             :      * but it's not very likely.
    3084                 :             :      *
    3085                 :             :      * 3. IMPORT FOREIGN SCHEMA ... INTO.  This is not allowed in CREATE RULE
    3086                 :             :      * or WITH, so we just check for IMPORT as the command's first token. (If
    3087                 :             :      * IMPORT FOREIGN SCHEMA returned data someone might wish to capture with
    3088                 :             :      * an INTO-variables clause, we'd have to work much harder here.)
    3089                 :             :      *
    3090                 :             :      * Fortunately, INTO is a fully reserved word in the main grammar, so at
    3091                 :             :      * least we need not worry about it appearing as an identifier.
    3092                 :             :      *
    3093                 :             :      * Any future additional uses of INTO in the main grammar will doubtless
    3094                 :             :      * break this logic again ... beware!
    3095                 :             :      */
    3096                 :        2574 :     tok = firsttoken;
    3097   [ +  +  +  + ]:        2574 :     if (tok == T_WORD && strcmp(word->ident, "create") == 0)
    3098                 :          55 :         tokens[token_count] = 'c';
    3099                 :        2574 :     token_count++;
    3100                 :             : 
    3101                 :             :     for (;;)
    3102                 :             :     {
    3103                 :       30594 :         prev_tok = tok;
    3104                 :       30594 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    3105   [ +  +  +  + ]:       30594 :         if (have_into && into_end_loc < 0)
    3106                 :         699 :             into_end_loc = *yyllocp;    /* token after the INTO part */
    3107                 :             :         /* Detect CREATE [OR REPLACE] {FUNCTION|PROCEDURE} */
    3108   [ +  +  +  + ]:       30594 :         if (tokens[0] == 'c' && token_count < sizeof(tokens))
    3109                 :             :         {
    3110         [ +  + ]:         165 :             if (tok == K_OR)
    3111                 :           2 :                 tokens[token_count] = 'o';
    3112         [ +  + ]:         163 :             else if (tok == T_WORD &&
    3113         [ +  + ]:         128 :                      strcmp(yylvalp->word.ident, "replace") == 0)
    3114                 :           2 :                 tokens[token_count] = 'r';
    3115         [ +  + ]:         161 :             else if (tok == T_WORD &&
    3116         [ +  + ]:         126 :                      strcmp(yylvalp->word.ident, "function") == 0)
    3117                 :           3 :                 tokens[token_count] = 'f';
    3118         [ +  + ]:         158 :             else if (tok == T_WORD &&
    3119         [ +  + ]:         123 :                      strcmp(yylvalp->word.ident, "procedure") == 0)
    3120                 :           1 :                 tokens[token_count] = 'f';  /* treat same as "function" */
    3121         [ +  + ]:         165 :             if (tokens[1] == 'f' ||
    3122   [ +  +  +  +  :         159 :                 (tokens[1] == 'o' && tokens[2] == 'r' && tokens[3] == 'f'))
                   +  + ]
    3123                 :           8 :                 in_routine_definition = true;
    3124                 :         165 :             token_count++;
    3125                 :             :         }
    3126                 :             :         /* Track paren nesting (needed for CREATE RULE syntax) */
    3127         [ +  + ]:       30594 :         if (tok == '(')
    3128                 :        1457 :             paren_depth++;
    3129   [ +  +  +  - ]:       29137 :         else if (tok == ')' && paren_depth > 0)
    3130                 :        1457 :             paren_depth--;
    3131                 :             :         /* We need track BEGIN/END nesting only in a routine definition */
    3132   [ +  +  +  + ]:       30594 :         if (in_routine_definition && paren_depth == 0)
    3133                 :             :         {
    3134   [ +  +  -  + ]:          46 :             if (tok == K_BEGIN || tok == K_CASE)
    3135                 :           2 :                 begin_depth++;
    3136   [ +  +  +  - ]:          44 :             else if (tok == K_END && begin_depth > 0)
    3137                 :           2 :                 begin_depth--;
    3138                 :             :         }
    3139                 :             :         /* Command-ending semicolon? */
    3140   [ +  +  +  -  :       30594 :         if (tok == ';' && paren_depth == 0 && begin_depth == 0)
                   +  + ]
    3141                 :        2574 :             break;
    3142         [ -  + ]:       28020 :         if (tok == 0)
    3143                 :           0 :             yyerror(yyllocp, NULL, yyscanner, "unexpected end of function definition");
    3144         [ +  + ]:       28020 :         if (tok == K_INTO)
    3145                 :             :         {
    3146         [ +  + ]:        1325 :             if (prev_tok == K_INSERT)
    3147                 :         590 :                 continue;       /* INSERT INTO is not an INTO-target */
    3148         [ +  + ]:         735 :             if (prev_tok == K_MERGE)
    3149                 :          36 :                 continue;       /* MERGE INTO is not an INTO-target */
    3150         [ -  + ]:         699 :             if (firsttoken == K_IMPORT)
    3151                 :           0 :                 continue;       /* IMPORT ... INTO is not an INTO-target */
    3152         [ -  + ]:         699 :             if (have_into)
    3153                 :           0 :                 yyerror(yyllocp, NULL, yyscanner, "INTO specified more than once");
    3154                 :         699 :             have_into = true;
    3155                 :         699 :             into_start_loc = *yyllocp;
    3156                 :         699 :             plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
    3157                 :         699 :             read_into_target(&target, &have_strict, yylvalp, yyllocp, yyscanner);
    3158                 :         699 :             plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_EXPR;
    3159                 :             :         }
    3160                 :             :     }
    3161                 :             : 
    3162                 :        2574 :     plpgsql_IdentifierLookup = save_IdentifierLookup;
    3163                 :             : 
    3164         [ +  + ]:        2574 :     if (have_into)
    3165                 :             :     {
    3166                 :             :         /*
    3167                 :             :          * Insert an appropriate number of spaces corresponding to the INTO
    3168                 :             :          * text, so that locations within the redacted SQL statement still
    3169                 :             :          * line up with those in the original source text.
    3170                 :             :          */
    3171                 :         699 :         plpgsql_append_source_text(&ds, location, into_start_loc, yyscanner);
    3172                 :         699 :         appendStringInfoSpaces(&ds, into_end_loc - into_start_loc);
    3173                 :         699 :         plpgsql_append_source_text(&ds, into_end_loc, *yyllocp, yyscanner);
    3174                 :             :     }
    3175                 :             :     else
    3176                 :        1875 :         plpgsql_append_source_text(&ds, location, *yyllocp, yyscanner);
    3177                 :             : 
    3178                 :             :     /* trim any trailing whitespace, for neatness */
    3179   [ +  -  +  + ]:        4972 :     while (ds.len > 0 && scanner_isspace(ds.data[ds.len - 1]))
    3180                 :        2398 :         ds.data[--ds.len] = '\0';
    3181                 :             : 
    3182                 :        2574 :     expr = make_plpgsql_expr(ds.data, RAW_PARSE_DEFAULT);
    3183                 :        2574 :     pfree(ds.data);
    3184                 :             : 
    3185                 :        2574 :     check_sql_expr(expr->query, expr->parseMode, location, yyscanner);
    3186                 :             : 
    3187                 :        2570 :     execsql = palloc0_object(PLpgSQL_stmt_execsql);
    3188                 :        2570 :     execsql->cmd_type = PLPGSQL_STMT_EXECSQL;
    3189                 :        2570 :     execsql->lineno = plpgsql_location_to_lineno(location, yyscanner);
    3190                 :        2570 :     execsql->stmtid = ++plpgsql_curr_compile->nstatements;
    3191                 :        2570 :     execsql->sqlstmt = expr;
    3192                 :        2570 :     execsql->into = have_into;
    3193                 :        2570 :     execsql->strict = have_strict;
    3194                 :        2570 :     execsql->target = target;
    3195                 :             : 
    3196                 :        2570 :     return (PLpgSQL_stmt *) execsql;
    3197                 :             : }
    3198                 :             : 
    3199                 :             : 
    3200                 :             : /*
    3201                 :             :  * Read FETCH or MOVE direction clause (everything through FROM/IN).
    3202                 :             :  */
    3203                 :             : static PLpgSQL_stmt_fetch *
    3204                 :          93 : read_fetch_direction(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    3205                 :             : {
    3206                 :             :     PLpgSQL_stmt_fetch *fetch;
    3207                 :             :     int         tok;
    3208                 :          93 :     bool        check_FROM = true;
    3209                 :             : 
    3210                 :             :     /*
    3211                 :             :      * We create the PLpgSQL_stmt_fetch struct here, but only fill in the
    3212                 :             :      * fields arising from the optional direction clause
    3213                 :             :      */
    3214                 :          93 :     fetch = (PLpgSQL_stmt_fetch *) palloc0_object(PLpgSQL_stmt_fetch);
    3215                 :          93 :     fetch->cmd_type = PLPGSQL_STMT_FETCH;
    3216                 :          93 :     fetch->stmtid = ++plpgsql_curr_compile->nstatements;
    3217                 :             :     /* set direction defaults: */
    3218                 :          93 :     fetch->direction = FETCH_FORWARD;
    3219                 :          93 :     fetch->how_many = 1;
    3220                 :          93 :     fetch->expr = NULL;
    3221                 :          93 :     fetch->returns_multiple_rows = false;
    3222                 :             : 
    3223                 :          93 :     tok = yylex(yylvalp, yyllocp, yyscanner);
    3224         [ -  + ]:          93 :     if (tok == 0)
    3225                 :           0 :         yyerror(yyllocp, NULL, yyscanner, "unexpected end of function definition");
    3226                 :             : 
    3227         [ +  + ]:          93 :     if (tok_is_keyword(tok, yylvalp,
    3228                 :             :                        K_NEXT, "next"))
    3229                 :             :     {
    3230                 :             :         /* use defaults */
    3231                 :             :     }
    3232         [ +  + ]:          85 :     else if (tok_is_keyword(tok, yylvalp,
    3233                 :             :                             K_PRIOR, "prior"))
    3234                 :             :     {
    3235                 :          12 :         fetch->direction = FETCH_BACKWARD;
    3236                 :             :     }
    3237         [ -  + ]:          73 :     else if (tok_is_keyword(tok, yylvalp,
    3238                 :             :                             K_FIRST, "first"))
    3239                 :             :     {
    3240                 :           0 :         fetch->direction = FETCH_ABSOLUTE;
    3241                 :             :     }
    3242         [ +  + ]:          73 :     else if (tok_is_keyword(tok, yylvalp,
    3243                 :             :                             K_LAST, "last"))
    3244                 :             :     {
    3245                 :          20 :         fetch->direction = FETCH_ABSOLUTE;
    3246                 :          20 :         fetch->how_many = -1;
    3247                 :             :     }
    3248         [ -  + ]:          53 :     else if (tok_is_keyword(tok, yylvalp,
    3249                 :             :                             K_ABSOLUTE, "absolute"))
    3250                 :             :     {
    3251                 :           0 :         fetch->direction = FETCH_ABSOLUTE;
    3252                 :           0 :         fetch->expr = read_sql_expression2(K_FROM, K_IN,
    3253                 :             :                                            "FROM or IN",
    3254                 :             :                                            NULL, yylvalp, yyllocp, yyscanner);
    3255                 :           0 :         check_FROM = false;
    3256                 :             :     }
    3257         [ +  + ]:          53 :     else if (tok_is_keyword(tok, yylvalp,
    3258                 :             :                             K_RELATIVE, "relative"))
    3259                 :             :     {
    3260                 :          12 :         fetch->direction = FETCH_RELATIVE;
    3261                 :          12 :         fetch->expr = read_sql_expression2(K_FROM, K_IN,
    3262                 :             :                                            "FROM or IN",
    3263                 :             :                                            NULL, yylvalp, yyllocp, yyscanner);
    3264                 :          12 :         check_FROM = false;
    3265                 :             :     }
    3266         [ -  + ]:          41 :     else if (tok_is_keyword(tok, yylvalp,
    3267                 :             :                             K_ALL, "all"))
    3268                 :             :     {
    3269                 :           0 :         fetch->how_many = FETCH_ALL;
    3270                 :           0 :         fetch->returns_multiple_rows = true;
    3271                 :             :     }
    3272         [ +  + ]:          41 :     else if (tok_is_keyword(tok, yylvalp,
    3273                 :             :                             K_FORWARD, "forward"))
    3274                 :             :     {
    3275                 :           4 :         complete_direction(fetch, &check_FROM, yylvalp, yyllocp, yyscanner);
    3276                 :             :     }
    3277         [ +  + ]:          37 :     else if (tok_is_keyword(tok, yylvalp,
    3278                 :             :                             K_BACKWARD, "backward"))
    3279                 :             :     {
    3280                 :           8 :         fetch->direction = FETCH_BACKWARD;
    3281                 :           8 :         complete_direction(fetch, &check_FROM, yylvalp, yyllocp, yyscanner);
    3282                 :             :     }
    3283   [ +  -  -  + ]:          29 :     else if (tok == K_FROM || tok == K_IN)
    3284                 :             :     {
    3285                 :             :         /* empty direction */
    3286                 :           0 :         check_FROM = false;
    3287                 :             :     }
    3288         [ +  - ]:          29 :     else if (tok == T_DATUM)
    3289                 :             :     {
    3290                 :             :         /* Assume there's no direction clause and tok is a cursor name */
    3291                 :          29 :         plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    3292                 :          29 :         check_FROM = false;
    3293                 :             :     }
    3294                 :             :     else
    3295                 :             :     {
    3296                 :             :         /*
    3297                 :             :          * Assume it's a count expression with no preceding keyword. Note: we
    3298                 :             :          * allow this syntax because core SQL does, but it's ambiguous with
    3299                 :             :          * the case of an omitted direction clause; for instance, "MOVE n IN
    3300                 :             :          * c" will fail if n is a variable, because the preceding else-arm
    3301                 :             :          * will trigger.  Perhaps this can be improved someday, but it hardly
    3302                 :             :          * seems worth a lot of work.
    3303                 :             :          */
    3304                 :           0 :         plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    3305                 :           0 :         fetch->expr = read_sql_expression2(K_FROM, K_IN,
    3306                 :             :                                            "FROM or IN",
    3307                 :             :                                            NULL, yylvalp, yyllocp, yyscanner);
    3308                 :           0 :         fetch->returns_multiple_rows = true;
    3309                 :           0 :         check_FROM = false;
    3310                 :             :     }
    3311                 :             : 
    3312                 :             :     /* check FROM or IN keyword after direction's specification */
    3313         [ +  + ]:          93 :     if (check_FROM)
    3314                 :             :     {
    3315                 :          44 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    3316   [ +  +  -  + ]:          44 :         if (tok != K_FROM && tok != K_IN)
    3317                 :           0 :             yyerror(yyllocp, NULL, yyscanner, "expected FROM or IN");
    3318                 :             :     }
    3319                 :             : 
    3320                 :          93 :     return fetch;
    3321                 :             : }
    3322                 :             : 
    3323                 :             : /*
    3324                 :             :  * Process remainder of FETCH/MOVE direction after FORWARD or BACKWARD.
    3325                 :             :  * Allows these cases:
    3326                 :             :  *   FORWARD expr,  FORWARD ALL,  FORWARD
    3327                 :             :  *   BACKWARD expr, BACKWARD ALL, BACKWARD
    3328                 :             :  */
    3329                 :             : static void
    3330                 :          12 : complete_direction(PLpgSQL_stmt_fetch *fetch, bool *check_FROM, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    3331                 :             : {
    3332                 :             :     int         tok;
    3333                 :             : 
    3334                 :          12 :     tok = yylex(yylvalp, yyllocp, yyscanner);
    3335         [ -  + ]:          12 :     if (tok == 0)
    3336                 :           0 :         yyerror(yyllocp, NULL, yyscanner, "unexpected end of function definition");
    3337                 :             : 
    3338   [ +  +  -  + ]:          12 :     if (tok == K_FROM || tok == K_IN)
    3339                 :             :     {
    3340                 :           4 :         *check_FROM = false;
    3341                 :           4 :         return;
    3342                 :             :     }
    3343                 :             : 
    3344         [ +  + ]:           8 :     if (tok == K_ALL)
    3345                 :             :     {
    3346                 :           4 :         fetch->how_many = FETCH_ALL;
    3347                 :           4 :         fetch->returns_multiple_rows = true;
    3348                 :           4 :         *check_FROM = true;
    3349                 :           4 :         return;
    3350                 :             :     }
    3351                 :             : 
    3352                 :           4 :     plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    3353                 :           4 :     fetch->expr = read_sql_expression2(K_FROM, K_IN,
    3354                 :             :                                        "FROM or IN",
    3355                 :             :                                        NULL, yylvalp, yyllocp, yyscanner);
    3356                 :           4 :     fetch->returns_multiple_rows = true;
    3357                 :           4 :     *check_FROM = false;
    3358                 :             : }
    3359                 :             : 
    3360                 :             : 
    3361                 :             : static PLpgSQL_stmt *
    3362                 :        5510 : make_return_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    3363                 :             : {
    3364                 :             :     PLpgSQL_stmt_return *new;
    3365                 :             : 
    3366                 :        5510 :     new = palloc0_object(PLpgSQL_stmt_return);
    3367                 :        5510 :     new->cmd_type = PLPGSQL_STMT_RETURN;
    3368                 :        5510 :     new->lineno = plpgsql_location_to_lineno(location, yyscanner);
    3369                 :        5510 :     new->stmtid = ++plpgsql_curr_compile->nstatements;
    3370                 :        5510 :     new->expr = NULL;
    3371                 :        5510 :     new->retvarno = -1;
    3372                 :             : 
    3373         [ +  + ]:        5510 :     if (plpgsql_curr_compile->fn_retset)
    3374                 :             :     {
    3375         [ -  + ]:          24 :         if (yylex(yylvalp, yyllocp, yyscanner) != ';')
    3376         [ #  # ]:           0 :             ereport(ERROR,
    3377                 :             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    3378                 :             :                      errmsg("RETURN cannot have a parameter in function returning set"),
    3379                 :             :                      errhint("Use RETURN NEXT or RETURN QUERY."),
    3380                 :             :                      parser_errposition(*yyllocp)));
    3381                 :             :     }
    3382         [ +  + ]:        5486 :     else if (plpgsql_curr_compile->fn_rettype == VOIDOID)
    3383                 :             :     {
    3384         [ +  + ]:          63 :         if (yylex(yylvalp, yyllocp, yyscanner) != ';')
    3385                 :             :         {
    3386         [ +  + ]:           9 :             if (plpgsql_curr_compile->fn_prokind == PROKIND_PROCEDURE)
    3387         [ +  - ]:           1 :                 ereport(ERROR,
    3388                 :             :                         (errcode(ERRCODE_SYNTAX_ERROR),
    3389                 :             :                          errmsg("RETURN cannot have a parameter in a procedure"),
    3390                 :             :                          parser_errposition(*yyllocp)));
    3391                 :             :             else
    3392         [ +  - ]:           8 :                 ereport(ERROR,
    3393                 :             :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
    3394                 :             :                          errmsg("RETURN cannot have a parameter in function returning void"),
    3395                 :             :                          parser_errposition(*yyllocp)));
    3396                 :             :         }
    3397                 :             :     }
    3398         [ +  + ]:        5423 :     else if (plpgsql_curr_compile->out_param_varno >= 0)
    3399                 :             :     {
    3400         [ +  + ]:          36 :         if (yylex(yylvalp, yyllocp, yyscanner) != ';')
    3401         [ +  - ]:           4 :             ereport(ERROR,
    3402                 :             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    3403                 :             :                      errmsg("RETURN cannot have a parameter in function with OUT parameters"),
    3404                 :             :                      parser_errposition(*yyllocp)));
    3405                 :          32 :         new->retvarno = plpgsql_curr_compile->out_param_varno;
    3406                 :             :     }
    3407                 :             :     else
    3408                 :             :     {
    3409                 :             :         /*
    3410                 :             :          * We want to special-case simple variable references for efficiency.
    3411                 :             :          * So peek ahead to see if that's what we have.
    3412                 :             :          */
    3413                 :        5387 :         int         tok = yylex(yylvalp, yyllocp, yyscanner);
    3414                 :             : 
    3415   [ +  +  +  + ]:        5387 :         if (tok == T_DATUM && plpgsql_peek(yyscanner) == ';' &&
    3416         [ +  + ]:        3094 :             (yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_VAR ||
    3417         [ +  - ]:        2465 :              yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_PROMISE ||
    3418         [ +  - ]:        2465 :              yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_ROW ||
    3419         [ +  + ]:        2465 :              yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_REC))
    3420                 :             :         {
    3421                 :        3037 :             new->retvarno = yylvalp->wdatum.datum->dno;
    3422                 :             :             /* eat the semicolon token that we only peeked at above */
    3423                 :        3037 :             tok = yylex(yylvalp, yyllocp, yyscanner);
    3424                 :        3037 :             Assert(tok == ';');
    3425                 :             :         }
    3426                 :             :         else
    3427                 :             :         {
    3428                 :             :             /*
    3429                 :             :              * Not (just) a variable name, so treat as expression.
    3430                 :             :              *
    3431                 :             :              * Note that a well-formed expression is _required_ here; anything
    3432                 :             :              * else is a compile-time error.
    3433                 :             :              */
    3434                 :        2350 :             plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    3435                 :        2350 :             new->expr = read_sql_expression(';', ";", yylvalp, yyllocp, yyscanner);
    3436                 :             :         }
    3437                 :             :     }
    3438                 :             : 
    3439                 :        5493 :     return (PLpgSQL_stmt *) new;
    3440                 :             : }
    3441                 :             : 
    3442                 :             : 
    3443                 :             : static PLpgSQL_stmt *
    3444                 :         227 : make_return_next_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    3445                 :             : {
    3446                 :             :     PLpgSQL_stmt_return_next *new;
    3447                 :             : 
    3448         [ -  + ]:         227 :     if (!plpgsql_curr_compile->fn_retset)
    3449         [ #  # ]:           0 :         ereport(ERROR,
    3450                 :             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    3451                 :             :                  errmsg("cannot use RETURN NEXT in a non-SETOF function"),
    3452                 :             :                  parser_errposition(location)));
    3453                 :             : 
    3454                 :         227 :     new = palloc0_object(PLpgSQL_stmt_return_next);
    3455                 :         227 :     new->cmd_type = PLPGSQL_STMT_RETURN_NEXT;
    3456                 :         227 :     new->lineno = plpgsql_location_to_lineno(location, yyscanner);
    3457                 :         227 :     new->stmtid = ++plpgsql_curr_compile->nstatements;
    3458                 :         227 :     new->expr = NULL;
    3459                 :         227 :     new->retvarno = -1;
    3460                 :             : 
    3461         [ +  + ]:         227 :     if (plpgsql_curr_compile->out_param_varno >= 0)
    3462                 :             :     {
    3463         [ -  + ]:          48 :         if (yylex(yylvalp, yyllocp, yyscanner) != ';')
    3464         [ #  # ]:           0 :             ereport(ERROR,
    3465                 :             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    3466                 :             :                      errmsg("RETURN NEXT cannot have a parameter in function with OUT parameters"),
    3467                 :             :                      parser_errposition(*yyllocp)));
    3468                 :          48 :         new->retvarno = plpgsql_curr_compile->out_param_varno;
    3469                 :             :     }
    3470                 :             :     else
    3471                 :             :     {
    3472                 :             :         /*
    3473                 :             :          * We want to special-case simple variable references for efficiency.
    3474                 :             :          * So peek ahead to see if that's what we have.
    3475                 :             :          */
    3476                 :         179 :         int         tok = yylex(yylvalp, yyllocp, yyscanner);
    3477                 :             : 
    3478   [ +  +  +  + ]:         179 :         if (tok == T_DATUM && plpgsql_peek(yyscanner) == ';' &&
    3479         [ +  + ]:         156 :             (yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_VAR ||
    3480         [ +  - ]:          44 :              yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_PROMISE ||
    3481         [ +  - ]:          44 :              yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_ROW ||
    3482         [ +  - ]:          44 :              yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_REC))
    3483                 :             :         {
    3484                 :         156 :             new->retvarno = yylvalp->wdatum.datum->dno;
    3485                 :             :             /* eat the semicolon token that we only peeked at above */
    3486                 :         156 :             tok = yylex(yylvalp, yyllocp, yyscanner);
    3487                 :         156 :             Assert(tok == ';');
    3488                 :             :         }
    3489                 :             :         else
    3490                 :             :         {
    3491                 :             :             /*
    3492                 :             :              * Not (just) a variable name, so treat as expression.
    3493                 :             :              *
    3494                 :             :              * Note that a well-formed expression is _required_ here; anything
    3495                 :             :              * else is a compile-time error.
    3496                 :             :              */
    3497                 :          23 :             plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    3498                 :          23 :             new->expr = read_sql_expression(';', ";", yylvalp, yyllocp, yyscanner);
    3499                 :             :         }
    3500                 :             :     }
    3501                 :             : 
    3502                 :         227 :     return (PLpgSQL_stmt *) new;
    3503                 :             : }
    3504                 :             : 
    3505                 :             : 
    3506                 :             : static PLpgSQL_stmt *
    3507                 :         119 : make_return_query_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    3508                 :             : {
    3509                 :             :     PLpgSQL_stmt_return_query *new;
    3510                 :             :     int         tok;
    3511                 :             : 
    3512         [ -  + ]:         119 :     if (!plpgsql_curr_compile->fn_retset)
    3513         [ #  # ]:           0 :         ereport(ERROR,
    3514                 :             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    3515                 :             :                  errmsg("cannot use RETURN QUERY in a non-SETOF function"),
    3516                 :             :                  parser_errposition(location)));
    3517                 :             : 
    3518                 :         119 :     new = palloc0_object(PLpgSQL_stmt_return_query);
    3519                 :         119 :     new->cmd_type = PLPGSQL_STMT_RETURN_QUERY;
    3520                 :         119 :     new->lineno = plpgsql_location_to_lineno(location, yyscanner);
    3521                 :         119 :     new->stmtid = ++plpgsql_curr_compile->nstatements;
    3522                 :             : 
    3523                 :             :     /* check for RETURN QUERY EXECUTE */
    3524                 :         119 :     tok = yylex(yylvalp, yyllocp, yyscanner);
    3525         [ +  + ]:         119 :     if (!tok_is_keyword(tok, yylvalp, K_EXECUTE, "execute"))
    3526                 :             :     {
    3527                 :             :         /* ordinary static query */
    3528                 :          55 :         plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    3529                 :          55 :         new->query = read_sql_stmt(yylvalp, yyllocp, yyscanner);
    3530                 :             :     }
    3531                 :             :     else
    3532                 :             :     {
    3533                 :             :         /* dynamic SQL */
    3534                 :             :         int         term;
    3535                 :             : 
    3536                 :          64 :         new->dynquery = read_sql_expression2(';', K_USING, "; or USING",
    3537                 :             :                                              &term, yylvalp, yyllocp, yyscanner);
    3538         [ +  + ]:          64 :         if (term == K_USING)
    3539                 :             :         {
    3540                 :             :             do
    3541                 :             :             {
    3542                 :             :                 PLpgSQL_expr *expr;
    3543                 :             : 
    3544                 :          12 :                 expr = read_sql_expression2(',', ';', ", or ;", &term, yylvalp, yyllocp, yyscanner);
    3545                 :          12 :                 new->params = lappend(new->params, expr);
    3546         [ +  + ]:          12 :             } while (term == ',');
    3547                 :             :         }
    3548                 :             :     }
    3549                 :             : 
    3550                 :         119 :     return (PLpgSQL_stmt *) new;
    3551                 :             : }
    3552                 :             : 
    3553                 :             : 
    3554                 :             : /* convenience routine to fetch the name of a T_DATUM */
    3555                 :             : static char *
    3556                 :        1209 : NameOfDatum(PLwdatum *wdatum)
    3557                 :             : {
    3558         [ +  + ]:        1209 :     if (wdatum->ident)
    3559                 :        1149 :         return wdatum->ident;
    3560                 :             :     Assert(wdatum->idents != NIL);
    3561                 :          60 :     return NameListToString(wdatum->idents);
    3562                 :             : }
    3563                 :             : 
    3564                 :             : static void
    3565                 :        6846 : check_assignable(PLpgSQL_datum *datum, int location, yyscan_t yyscanner)
    3566                 :             : {
    3567   [ +  +  +  - ]:        6846 :     switch (datum->dtype)
    3568                 :             :     {
    3569                 :        6343 :         case PLPGSQL_DTYPE_VAR:
    3570                 :             :         case PLPGSQL_DTYPE_PROMISE:
    3571                 :             :         case PLPGSQL_DTYPE_REC:
    3572         [ +  + ]:        6343 :             if (((PLpgSQL_variable *) datum)->isconst)
    3573         [ +  - ]:           5 :                 ereport(ERROR,
    3574                 :             :                         (errcode(ERRCODE_ERROR_IN_ASSIGNMENT),
    3575                 :             :                          errmsg("variable \"%s\" is declared CONSTANT",
    3576                 :             :                                 ((PLpgSQL_variable *) datum)->refname),
    3577                 :             :                          parser_errposition(location)));
    3578                 :        6338 :             break;
    3579                 :          10 :         case PLPGSQL_DTYPE_ROW:
    3580                 :             :             /* always assignable; member vars were checked at compile time */
    3581                 :          10 :             break;
    3582                 :         493 :         case PLPGSQL_DTYPE_RECFIELD:
    3583                 :             :             /* assignable if parent record is */
    3584                 :         493 :             check_assignable(plpgsql_Datums[((PLpgSQL_recfield *) datum)->recparentno],
    3585                 :             :                              location, yyscanner);
    3586                 :         491 :             break;
    3587                 :           0 :         default:
    3588         [ #  # ]:           0 :             elog(ERROR, "unrecognized dtype: %d", datum->dtype);
    3589                 :             :             break;
    3590                 :             :     }
    3591                 :        6839 : }
    3592                 :             : 
    3593                 :             : /*
    3594                 :             :  * Read the argument of an INTO clause.  On entry, we have just read the
    3595                 :             :  * INTO keyword.
    3596                 :             :  */
    3597                 :             : static void
    3598                 :        1099 : read_into_target(PLpgSQL_variable **target, bool *strict, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    3599                 :             : {
    3600                 :             :     int         tok;
    3601                 :             : 
    3602                 :             :     /* Set default results */
    3603                 :        1099 :     *target = NULL;
    3604         [ +  + ]:        1099 :     if (strict)
    3605                 :        1018 :         *strict = false;
    3606                 :             : 
    3607                 :        1099 :     tok = yylex(yylvalp, yyllocp, yyscanner);
    3608   [ +  +  +  + ]:        1099 :     if (strict && tok_is_keyword(tok, yylvalp, K_STRICT, "strict"))
    3609                 :             :     {
    3610                 :          89 :         *strict = true;
    3611                 :          89 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    3612                 :             :     }
    3613                 :             : 
    3614                 :             :     /*
    3615                 :             :      * Currently, a row or record variable can be the single INTO target, but
    3616                 :             :      * not a member of a multi-target list.  So we throw error if there is a
    3617                 :             :      * comma after it, because that probably means the user tried to write a
    3618                 :             :      * multi-target list.  If this ever gets generalized, we should probably
    3619                 :             :      * refactor read_into_scalar_list so it handles all cases.
    3620                 :             :      */
    3621         [ +  - ]:        1099 :     switch (tok)
    3622                 :             :     {
    3623                 :        1099 :         case T_DATUM:
    3624         [ +  - ]:        1099 :             if (yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_ROW ||
    3625         [ +  + ]:        1099 :                 yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_REC)
    3626                 :             :             {
    3627                 :         515 :                 check_assignable(yylvalp->wdatum.datum, *yyllocp, yyscanner);
    3628                 :         515 :                 *target = (PLpgSQL_variable *) yylvalp->wdatum.datum;
    3629                 :             : 
    3630         [ -  + ]:         515 :                 if ((tok = yylex(yylvalp, yyllocp, yyscanner)) == ',')
    3631         [ #  # ]:           0 :                     ereport(ERROR,
    3632                 :             :                             (errcode(ERRCODE_SYNTAX_ERROR),
    3633                 :             :                              errmsg("record variable cannot be part of multiple-item INTO list"),
    3634                 :             :                              parser_errposition(*yyllocp)));
    3635                 :         515 :                 plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    3636                 :             :             }
    3637                 :             :             else
    3638                 :             :             {
    3639                 :         584 :                 *target = (PLpgSQL_variable *)
    3640                 :         584 :                     read_into_scalar_list(NameOfDatum(&(yylvalp->wdatum)),
    3641                 :             :                                           yylvalp->wdatum.datum, *yyllocp, yylvalp, yyllocp, yyscanner);
    3642                 :             :             }
    3643                 :        1099 :             break;
    3644                 :             : 
    3645                 :           0 :         default:
    3646                 :             :             /* just to give a better message than "syntax error" */
    3647                 :           0 :             current_token_is_not_variable(tok, yylvalp, yyllocp, yyscanner);
    3648                 :             :     }
    3649                 :        1099 : }
    3650                 :             : 
    3651                 :             : /*
    3652                 :             :  * Given the first datum and name in the INTO list, continue to read
    3653                 :             :  * comma-separated scalar variables until we run out. Then construct
    3654                 :             :  * and return a fake "row" variable that represents the list of
    3655                 :             :  * scalars.
    3656                 :             :  */
    3657                 :             : static PLpgSQL_row *
    3658                 :         596 : read_into_scalar_list(char *initial_name,
    3659                 :             :                       PLpgSQL_datum *initial_datum,
    3660                 :             :                       int initial_location,
    3661                 :             :                       YYSTYPE *yylvalp, YYLTYPE *yyllocp,
    3662                 :             :                       yyscan_t yyscanner)
    3663                 :             : {
    3664                 :             :     int         nfields;
    3665                 :             :     char       *fieldnames[1024];
    3666                 :             :     int         varnos[1024];
    3667                 :             :     PLpgSQL_row *row;
    3668                 :             :     int         tok;
    3669                 :             : 
    3670                 :         596 :     check_assignable(initial_datum, initial_location, yyscanner);
    3671                 :         594 :     fieldnames[0] = initial_name;
    3672                 :         594 :     varnos[0] = initial_datum->dno;
    3673                 :         594 :     nfields = 1;
    3674                 :             : 
    3675         [ +  + ]:         710 :     while ((tok = yylex(yylvalp, yyllocp, yyscanner)) == ',')
    3676                 :             :     {
    3677                 :             :         /* Check for array overflow */
    3678         [ -  + ]:         116 :         if (nfields >= 1024)
    3679         [ #  # ]:           0 :             ereport(ERROR,
    3680                 :             :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    3681                 :             :                      errmsg("too many INTO variables specified"),
    3682                 :             :                      parser_errposition(*yyllocp)));
    3683                 :             : 
    3684                 :         116 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    3685         [ +  - ]:         116 :         switch (tok)
    3686                 :             :         {
    3687                 :         116 :             case T_DATUM:
    3688                 :         116 :                 check_assignable(yylvalp->wdatum.datum, *yyllocp, yyscanner);
    3689         [ +  - ]:         116 :                 if (yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_ROW ||
    3690         [ -  + ]:         116 :                     yylvalp->wdatum.datum->dtype == PLPGSQL_DTYPE_REC)
    3691         [ #  # ]:           0 :                     ereport(ERROR,
    3692                 :             :                             (errcode(ERRCODE_SYNTAX_ERROR),
    3693                 :             :                              errmsg("\"%s\" is not a scalar variable",
    3694                 :             :                                     NameOfDatum(&(yylvalp->wdatum))),
    3695                 :             :                              parser_errposition(*yyllocp)));
    3696                 :         116 :                 fieldnames[nfields] = NameOfDatum(&(yylvalp->wdatum));
    3697                 :         116 :                 varnos[nfields++] = yylvalp->wdatum.datum->dno;
    3698                 :         116 :                 break;
    3699                 :             : 
    3700                 :           0 :             default:
    3701                 :             :                 /* just to give a better message than "syntax error" */
    3702                 :           0 :                 current_token_is_not_variable(tok, yylvalp, yyllocp, yyscanner);
    3703                 :             :         }
    3704                 :             :     }
    3705                 :             : 
    3706                 :             :     /*
    3707                 :             :      * We read an extra, non-comma token from yylex(), so push it back onto
    3708                 :             :      * the input stream
    3709                 :             :      */
    3710                 :         594 :     plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
    3711                 :             : 
    3712                 :         594 :     row = palloc0_object(PLpgSQL_row);
    3713                 :         594 :     row->dtype = PLPGSQL_DTYPE_ROW;
    3714                 :         594 :     row->refname = "(unnamed row)";
    3715                 :         594 :     row->lineno = plpgsql_location_to_lineno(initial_location, yyscanner);
    3716                 :         594 :     row->rowtupdesc = NULL;
    3717                 :         594 :     row->nfields = nfields;
    3718                 :         594 :     row->fieldnames = palloc_array(char *, nfields);
    3719                 :         594 :     row->varnos = palloc_array(int, nfields);
    3720         [ +  + ]:        1304 :     while (--nfields >= 0)
    3721                 :             :     {
    3722                 :         710 :         row->fieldnames[nfields] = fieldnames[nfields];
    3723                 :         710 :         row->varnos[nfields] = varnos[nfields];
    3724                 :             :     }
    3725                 :             : 
    3726                 :         594 :     plpgsql_adddatum((PLpgSQL_datum *) row);
    3727                 :             : 
    3728                 :         594 :     return row;
    3729                 :             : }
    3730                 :             : 
    3731                 :             : /*
    3732                 :             :  * Convert a single scalar into a "row" list.  This is exactly
    3733                 :             :  * like read_into_scalar_list except we never consume any input.
    3734                 :             :  *
    3735                 :             :  * Note: lineno could be computed from location, but since callers
    3736                 :             :  * have it at hand already, we may as well pass it in.
    3737                 :             :  */
    3738                 :             : static PLpgSQL_row *
    3739                 :         246 : make_scalar_list1(char *initial_name,
    3740                 :             :                   PLpgSQL_datum *initial_datum,
    3741                 :             :                   int lineno, int location, yyscan_t yyscanner)
    3742                 :             : {
    3743                 :             :     PLpgSQL_row *row;
    3744                 :             : 
    3745                 :         246 :     check_assignable(initial_datum, location, yyscanner);
    3746                 :             : 
    3747                 :         246 :     row = palloc0_object(PLpgSQL_row);
    3748                 :         246 :     row->dtype = PLPGSQL_DTYPE_ROW;
    3749                 :         246 :     row->refname = "(unnamed row)";
    3750                 :         246 :     row->lineno = lineno;
    3751                 :         246 :     row->rowtupdesc = NULL;
    3752                 :         246 :     row->nfields = 1;
    3753                 :         246 :     row->fieldnames = palloc_object(char *);
    3754                 :         246 :     row->varnos = palloc_object(int);
    3755                 :         246 :     row->fieldnames[0] = initial_name;
    3756                 :         246 :     row->varnos[0] = initial_datum->dno;
    3757                 :             : 
    3758                 :         246 :     plpgsql_adddatum((PLpgSQL_datum *) row);
    3759                 :             : 
    3760                 :         246 :     return row;
    3761                 :             : }
    3762                 :             : 
    3763                 :             : /*
    3764                 :             :  * When the PL/pgSQL parser expects to see a SQL statement, it is very
    3765                 :             :  * liberal in what it accepts; for example, we often assume an
    3766                 :             :  * unrecognized keyword is the beginning of a SQL statement. This
    3767                 :             :  * avoids the need to duplicate parts of the SQL grammar in the
    3768                 :             :  * PL/pgSQL grammar, but it means we can accept wildly malformed
    3769                 :             :  * input. To try and catch some of the more obviously invalid input,
    3770                 :             :  * we run the strings we expect to be SQL statements through the main
    3771                 :             :  * SQL parser.
    3772                 :             :  *
    3773                 :             :  * We only invoke the raw parser (not the analyzer); this doesn't do
    3774                 :             :  * any database access and does not check any semantic rules, it just
    3775                 :             :  * checks for basic syntactic correctness. We do this here, rather
    3776                 :             :  * than after parsing has finished, because a malformed SQL statement
    3777                 :             :  * may cause the PL/pgSQL parser to become confused about statement
    3778                 :             :  * borders. So it is best to bail out as early as we can.
    3779                 :             :  *
    3780                 :             :  * It is assumed that "stmt" represents a copy of the function source text
    3781                 :             :  * beginning at offset "location".  We use this assumption to transpose
    3782                 :             :  * any error cursor position back to the function source text.
    3783                 :             :  * If no error cursor is provided, we'll just point at "location".
    3784                 :             :  */
    3785                 :             : static void
    3786                 :       27210 : check_sql_expr(const char *stmt, RawParseMode parseMode, int location, yyscan_t yyscanner)
    3787                 :             : {
    3788                 :             :     sql_error_callback_arg cbarg;
    3789                 :             :     ErrorContextCallback syntax_errcontext;
    3790                 :             :     MemoryContext oldCxt;
    3791                 :             : 
    3792         [ +  + ]:       27210 :     if (!plpgsql_check_syntax)
    3793                 :       14057 :         return;
    3794                 :             : 
    3795                 :       13153 :     cbarg.location = location;
    3796                 :       13153 :     cbarg.yyscanner = yyscanner;
    3797                 :             : 
    3798                 :       13153 :     syntax_errcontext.callback = plpgsql_sql_error_callback;
    3799                 :       13153 :     syntax_errcontext.arg = &cbarg;
    3800                 :       13153 :     syntax_errcontext.previous = error_context_stack;
    3801                 :       13153 :     error_context_stack = &syntax_errcontext;
    3802                 :             : 
    3803                 :       13153 :     oldCxt = MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
    3804                 :       13153 :     (void) raw_parser(stmt, parseMode);
    3805                 :       13145 :     MemoryContextSwitchTo(oldCxt);
    3806                 :             : 
    3807                 :             :     /* Restore former ereport callback */
    3808                 :       13145 :     error_context_stack = syntax_errcontext.previous;
    3809                 :             : }
    3810                 :             : 
    3811                 :             : static void
    3812                 :           9 : plpgsql_sql_error_callback(void *arg)
    3813                 :             : {
    3814                 :           9 :     sql_error_callback_arg *cbarg = (sql_error_callback_arg *) arg;
    3815                 :           9 :     yyscan_t    yyscanner = cbarg->yyscanner;
    3816                 :             :     int         errpos;
    3817                 :             : 
    3818                 :             :     /*
    3819                 :             :      * First, set up internalerrposition to point to the start of the
    3820                 :             :      * statement text within the function text.  Note this converts location
    3821                 :             :      * (a byte offset) to a character number.
    3822                 :             :      */
    3823                 :           9 :     parser_errposition(cbarg->location);
    3824                 :             : 
    3825                 :             :     /*
    3826                 :             :      * If the core parser provided an error position, transpose it. Note we
    3827                 :             :      * are dealing with 1-based character numbers at this point.
    3828                 :             :      */
    3829                 :           9 :     errpos = geterrposition();
    3830         [ +  + ]:           9 :     if (errpos > 0)
    3831                 :             :     {
    3832                 :           8 :         int         myerrpos = getinternalerrposition();
    3833                 :             : 
    3834         [ +  - ]:           8 :         if (myerrpos > 0)        /* safety check */
    3835                 :           8 :             internalerrposition(myerrpos + errpos - 1);
    3836                 :             :     }
    3837                 :             : 
    3838                 :             :     /* In any case, flush errposition --- we want internalerrposition only */
    3839                 :           9 :     errposition(0);
    3840                 :           9 : }
    3841                 :             : 
    3842                 :             : /*
    3843                 :             :  * Parse a SQL datatype name and produce a PLpgSQL_type structure.
    3844                 :             :  *
    3845                 :             :  * The heavy lifting is done elsewhere.  Here we are only concerned
    3846                 :             :  * with setting up an errcontext link that will let us give an error
    3847                 :             :  * cursor pointing into the plpgsql function source, if necessary.
    3848                 :             :  * This is handled the same as in check_sql_expr(), and we likewise
    3849                 :             :  * expect that the given string is a copy from the source text.
    3850                 :             :  */
    3851                 :             : static PLpgSQL_type *
    3852                 :        3603 : parse_datatype(const char *string, int location, yyscan_t yyscanner)
    3853                 :             : {
    3854                 :             :     TypeName   *typeName;
    3855                 :             :     Oid         type_id;
    3856                 :             :     int32       typmod;
    3857                 :             :     sql_error_callback_arg cbarg;
    3858                 :             :     ErrorContextCallback syntax_errcontext;
    3859                 :             :     MemoryContext oldCxt;
    3860                 :             : 
    3861                 :        3603 :     cbarg.location = location;
    3862                 :        3603 :     cbarg.yyscanner = yyscanner;
    3863                 :             : 
    3864                 :        3603 :     syntax_errcontext.callback = plpgsql_sql_error_callback;
    3865                 :        3603 :     syntax_errcontext.arg = &cbarg;
    3866                 :        3603 :     syntax_errcontext.previous = error_context_stack;
    3867                 :        3603 :     error_context_stack = &syntax_errcontext;
    3868                 :             : 
    3869                 :             :     /*
    3870                 :             :      * Let the main parser try to parse it under standard SQL rules.  The
    3871                 :             :      * parser leaks memory, so run it in temp context.
    3872                 :             :      */
    3873                 :        3603 :     oldCxt = MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
    3874                 :        3603 :     typeName = typeStringToTypeName(string, NULL);
    3875                 :        3603 :     typenameTypeIdAndMod(NULL, typeName, &type_id, &typmod);
    3876                 :        3602 :     MemoryContextSwitchTo(oldCxt);
    3877                 :             : 
    3878                 :             :     /* Restore former ereport callback */
    3879                 :        3602 :     error_context_stack = syntax_errcontext.previous;
    3880                 :             : 
    3881                 :             :     /* Okay, build a PLpgSQL_type data structure for it */
    3882                 :        7204 :     return plpgsql_build_datatype(type_id, typmod,
    3883                 :        3602 :                                   plpgsql_curr_compile->fn_input_collation,
    3884                 :             :                                   typeName);
    3885                 :             : }
    3886                 :             : 
    3887                 :             : /*
    3888                 :             :  * Check block starting and ending labels match.
    3889                 :             :  */
    3890                 :             : static void
    3891                 :        7156 : check_labels(const char *start_label, const char *end_label, int end_location, yyscan_t yyscanner)
    3892                 :             : {
    3893         [ +  + ]:        7156 :     if (end_label)
    3894                 :             :     {
    3895         [ +  + ]:           5 :         if (!start_label)
    3896         [ +  - ]:           2 :             ereport(ERROR,
    3897                 :             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    3898                 :             :                      errmsg("end label \"%s\" specified for unlabeled block",
    3899                 :             :                             end_label),
    3900                 :             :                      parser_errposition(end_location)));
    3901                 :             : 
    3902         [ +  + ]:           3 :         if (strcmp(start_label, end_label) != 0)
    3903         [ +  - ]:           1 :             ereport(ERROR,
    3904                 :             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    3905                 :             :                      errmsg("end label \"%s\" differs from block's label \"%s\"",
    3906                 :             :                             end_label, start_label),
    3907                 :             :                      parser_errposition(end_location)));
    3908                 :             :     }
    3909                 :        7153 : }
    3910                 :             : 
    3911                 :             : /*
    3912                 :             :  * Read the arguments (if any) for a cursor, followed by the until token
    3913                 :             :  *
    3914                 :             :  * If cursor has no args, just swallow the until token and return NULL.
    3915                 :             :  * If it does have args, we expect to see "( arg [, arg ...] )" followed
    3916                 :             :  * by the until token, where arg may be a plain expression, or a named
    3917                 :             :  * parameter assignment of the form argname := expr. Consume all that and
    3918                 :             :  * return a SELECT query that evaluates the expression(s) (without the outer
    3919                 :             :  * parens).
    3920                 :             :  */
    3921                 :             : static PLpgSQL_expr *
    3922                 :          89 : read_cursor_args(PLpgSQL_var *cursor, int until, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    3923                 :             : {
    3924                 :             :     PLpgSQL_expr *expr;
    3925                 :             :     PLpgSQL_row *row;
    3926                 :             :     int         tok;
    3927                 :             :     int         argc;
    3928                 :             :     char      **argv;
    3929                 :             :     StringInfoData ds;
    3930                 :          89 :     bool        any_named = false;
    3931                 :             : 
    3932                 :          89 :     tok = yylex(yylvalp, yyllocp, yyscanner);
    3933         [ +  + ]:          89 :     if (cursor->cursor_explicit_argrow < 0)
    3934                 :             :     {
    3935                 :             :         /* No arguments expected */
    3936         [ -  + ]:          41 :         if (tok == '(')
    3937         [ #  # ]:           0 :             ereport(ERROR,
    3938                 :             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    3939                 :             :                      errmsg("cursor \"%s\" has no arguments",
    3940                 :             :                             cursor->refname),
    3941                 :             :                      parser_errposition(*yyllocp)));
    3942                 :             : 
    3943         [ -  + ]:          41 :         if (tok != until)
    3944                 :           0 :             yyerror(yyllocp, NULL, yyscanner, "syntax error");
    3945                 :             : 
    3946                 :          41 :         return NULL;
    3947                 :             :     }
    3948                 :             : 
    3949                 :             :     /* Else better provide arguments */
    3950         [ -  + ]:          48 :     if (tok != '(')
    3951         [ #  # ]:           0 :         ereport(ERROR,
    3952                 :             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    3953                 :             :                  errmsg("cursor \"%s\" has arguments",
    3954                 :             :                         cursor->refname),
    3955                 :             :                  parser_errposition(*yyllocp)));
    3956                 :             : 
    3957                 :             :     /*
    3958                 :             :      * Read the arguments, one by one.
    3959                 :             :      */
    3960                 :          48 :     row = (PLpgSQL_row *) plpgsql_Datums[cursor->cursor_explicit_argrow];
    3961                 :          48 :     argv = palloc0_array(char *, row->nfields);
    3962                 :             : 
    3963         [ +  + ]:         128 :     for (argc = 0; argc < row->nfields; argc++)
    3964                 :             :     {
    3965                 :             :         PLpgSQL_expr *item;
    3966                 :             :         int         endtoken;
    3967                 :             :         int         argpos;
    3968                 :             :         int         tok1,
    3969                 :             :                     tok2;
    3970                 :             :         int         arglocation;
    3971                 :             : 
    3972                 :             :         /*
    3973                 :             :          * Check if it's a named parameter: "param := value"
    3974                 :             :          * or "param => value"
    3975                 :             :          */
    3976                 :          96 :         plpgsql_peek2(&tok1, &tok2, &arglocation, NULL, yyscanner);
    3977   [ +  +  +  +  :          96 :         if (tok1 == IDENT && (tok2 == COLON_EQUALS || tok2 == EQUALS_GREATER))
                   +  - ]
    3978                 :          60 :         {
    3979                 :             :             char       *argname;
    3980                 :             :             IdentifierLookup save_IdentifierLookup;
    3981                 :             : 
    3982                 :             :             /* Read the argument name, ignoring any matching variable */
    3983                 :          60 :             save_IdentifierLookup = plpgsql_IdentifierLookup;
    3984                 :          60 :             plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
    3985                 :          60 :             yylex(yylvalp, yyllocp, yyscanner);
    3986                 :          60 :             argname = yylvalp->str;
    3987                 :          60 :             plpgsql_IdentifierLookup = save_IdentifierLookup;
    3988                 :             : 
    3989                 :             :             /* Match argument name to cursor arguments */
    3990         [ +  - ]:         100 :             for (argpos = 0; argpos < row->nfields; argpos++)
    3991                 :             :             {
    3992         [ +  + ]:         100 :                 if (strcmp(row->fieldnames[argpos], argname) == 0)
    3993                 :          60 :                     break;
    3994                 :             :             }
    3995         [ -  + ]:          60 :             if (argpos == row->nfields)
    3996         [ #  # ]:           0 :                 ereport(ERROR,
    3997                 :             :                         (errcode(ERRCODE_SYNTAX_ERROR),
    3998                 :             :                          errmsg("cursor \"%s\" has no argument named \"%s\"",
    3999                 :             :                                 cursor->refname, argname),
    4000                 :             :                          parser_errposition(*yyllocp)));
    4001                 :             : 
    4002                 :             :             /*
    4003                 :             :              * Eat the ":=" or "=>".  We already peeked, so the error should
    4004                 :             :              * never happen.
    4005                 :             :              */
    4006                 :          60 :             tok2 = yylex(yylvalp, yyllocp, yyscanner);
    4007   [ +  +  -  + ]:          60 :             if (tok2 != COLON_EQUALS && tok2 != EQUALS_GREATER)
    4008                 :           0 :                 yyerror(yyllocp, NULL, yyscanner, "syntax error");
    4009                 :             : 
    4010                 :          60 :             any_named = true;
    4011                 :             :         }
    4012                 :             :         else
    4013                 :          36 :             argpos = argc;
    4014                 :             : 
    4015         [ +  + ]:          96 :         if (argv[argpos] != NULL)
    4016         [ +  - ]:          12 :             ereport(ERROR,
    4017                 :             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    4018                 :             :                      errmsg("value for parameter \"%s\" of cursor \"%s\" specified more than once",
    4019                 :             :                             row->fieldnames[argpos], cursor->refname),
    4020                 :             :                      parser_errposition(arglocation)));
    4021                 :             : 
    4022                 :             :         /*
    4023                 :             :          * Read the value expression. To provide the user with meaningful
    4024                 :             :          * parse error positions, we check the syntax immediately, instead of
    4025                 :             :          * checking the final expression that may have the arguments
    4026                 :             :          * reordered.
    4027                 :             :          */
    4028                 :          84 :         item = read_sql_construct(',', ')', 0,
    4029                 :             :                                   ",\" or \")",
    4030                 :             :                                   RAW_PARSE_PLPGSQL_EXPR,
    4031                 :             :                                   true, true,
    4032                 :             :                                   NULL, &endtoken,
    4033                 :             :                                   yylvalp, yyllocp, yyscanner);
    4034                 :             : 
    4035                 :          84 :         argv[argpos] = item->query;
    4036                 :             : 
    4037   [ +  +  +  + ]:          84 :         if (endtoken == ')' && !(argc == row->nfields - 1))
    4038         [ +  - ]:           4 :             ereport(ERROR,
    4039                 :             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    4040                 :             :                      errmsg("not enough arguments for cursor \"%s\"",
    4041                 :             :                             cursor->refname),
    4042                 :             :                      parser_errposition(*yyllocp)));
    4043                 :             : 
    4044   [ +  +  -  + ]:          80 :         if (endtoken == ',' && (argc == row->nfields - 1))
    4045         [ #  # ]:           0 :             ereport(ERROR,
    4046                 :             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    4047                 :             :                      errmsg("too many arguments for cursor \"%s\"",
    4048                 :             :                             cursor->refname),
    4049                 :             :                      parser_errposition(*yyllocp)));
    4050                 :             :     }
    4051                 :             : 
    4052                 :             :     /* Make positional argument list */
    4053                 :          32 :     initStringInfo(&ds);
    4054         [ +  + ]:         100 :     for (argc = 0; argc < row->nfields; argc++)
    4055                 :             :     {
    4056                 :             :         Assert(argv[argc] != NULL);
    4057                 :             : 
    4058                 :             :         /*
    4059                 :             :          * Because named notation allows permutated argument lists, include
    4060                 :             :          * the parameter name for meaningful runtime errors.
    4061                 :             :          */
    4062                 :          68 :         appendStringInfoString(&ds, argv[argc]);
    4063         [ +  + ]:          68 :         if (any_named)
    4064                 :          44 :             appendStringInfo(&ds, " AS %s",
    4065                 :          44 :                              quote_identifier(row->fieldnames[argc]));
    4066         [ +  + ]:          68 :         if (argc < row->nfields - 1)
    4067                 :          36 :             appendStringInfoString(&ds, ", ");
    4068                 :             :     }
    4069                 :             : 
    4070                 :          32 :     expr = make_plpgsql_expr(ds.data, RAW_PARSE_PLPGSQL_EXPR);
    4071                 :          32 :     pfree(ds.data);
    4072                 :             : 
    4073                 :             :     /* Next we'd better find the until token */
    4074                 :          32 :     tok = yylex(yylvalp, yyllocp, yyscanner);
    4075         [ -  + ]:          32 :     if (tok != until)
    4076                 :           0 :         yyerror(yyllocp, NULL, yyscanner, "syntax error");
    4077                 :             : 
    4078                 :          32 :     return expr;
    4079                 :             : }
    4080                 :             : 
    4081                 :             : /*
    4082                 :             :  * Parse RAISE ... USING options
    4083                 :             :  */
    4084                 :             : static List *
    4085                 :         106 : read_raise_options(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
    4086                 :             : {
    4087                 :         106 :     List       *result = NIL;
    4088                 :             : 
    4089                 :             :     for (;;)
    4090                 :          62 :     {
    4091                 :             :         PLpgSQL_raise_option *opt;
    4092                 :             :         int         tok;
    4093                 :             : 
    4094         [ -  + ]:         168 :         if ((tok = yylex(yylvalp, yyllocp, yyscanner)) == 0)
    4095                 :           0 :             yyerror(yyllocp, NULL, yyscanner, "unexpected end of function definition");
    4096                 :             : 
    4097                 :         168 :         opt = palloc_object(PLpgSQL_raise_option);
    4098                 :             : 
    4099         [ +  + ]:         168 :         if (tok_is_keyword(tok, yylvalp,
    4100                 :             :                            K_ERRCODE, "errcode"))
    4101                 :          32 :             opt->opt_type = PLPGSQL_RAISEOPTION_ERRCODE;
    4102         [ +  + ]:         136 :         else if (tok_is_keyword(tok, yylvalp,
    4103                 :             :                                 K_MESSAGE, "message"))
    4104                 :          72 :             opt->opt_type = PLPGSQL_RAISEOPTION_MESSAGE;
    4105         [ +  + ]:          64 :         else if (tok_is_keyword(tok, yylvalp,
    4106                 :             :                                 K_DETAIL, "detail"))
    4107                 :          30 :             opt->opt_type = PLPGSQL_RAISEOPTION_DETAIL;
    4108         [ +  + ]:          34 :         else if (tok_is_keyword(tok, yylvalp,
    4109                 :             :                                 K_HINT, "hint"))
    4110                 :           9 :             opt->opt_type = PLPGSQL_RAISEOPTION_HINT;
    4111         [ +  + ]:          25 :         else if (tok_is_keyword(tok, yylvalp,
    4112                 :             :                                 K_COLUMN, "column"))
    4113                 :           5 :             opt->opt_type = PLPGSQL_RAISEOPTION_COLUMN;
    4114         [ +  + ]:          20 :         else if (tok_is_keyword(tok, yylvalp,
    4115                 :             :                                 K_CONSTRAINT, "constraint"))
    4116                 :           5 :             opt->opt_type = PLPGSQL_RAISEOPTION_CONSTRAINT;
    4117         [ +  + ]:          15 :         else if (tok_is_keyword(tok, yylvalp,
    4118                 :             :                                 K_DATATYPE, "datatype"))
    4119                 :           5 :             opt->opt_type = PLPGSQL_RAISEOPTION_DATATYPE;
    4120         [ +  + ]:          10 :         else if (tok_is_keyword(tok, yylvalp,
    4121                 :             :                                 K_TABLE, "table"))
    4122                 :           5 :             opt->opt_type = PLPGSQL_RAISEOPTION_TABLE;
    4123         [ +  - ]:           5 :         else if (tok_is_keyword(tok, yylvalp,
    4124                 :             :                                 K_SCHEMA, "schema"))
    4125                 :           5 :             opt->opt_type = PLPGSQL_RAISEOPTION_SCHEMA;
    4126                 :             :         else
    4127                 :           0 :             yyerror(yyllocp, NULL, yyscanner, "unrecognized RAISE statement option");
    4128                 :             : 
    4129                 :         168 :         tok = yylex(yylvalp, yyllocp, yyscanner);
    4130   [ -  +  -  - ]:         168 :         if (tok != '=' && tok != COLON_EQUALS)
    4131                 :           0 :             yyerror(yyllocp, NULL, yyscanner, "syntax error, expected \"=\"");
    4132                 :             : 
    4133                 :         168 :         opt->expr = read_sql_expression2(',', ';', ", or ;", &tok, yylvalp, yyllocp, yyscanner);
    4134                 :             : 
    4135                 :         168 :         result = lappend(result, opt);
    4136                 :             : 
    4137         [ +  + ]:         168 :         if (tok == ';')
    4138                 :         106 :             break;
    4139                 :             :     }
    4140                 :             : 
    4141                 :         106 :     return result;
    4142                 :             : }
    4143                 :             : 
    4144                 :             : /*
    4145                 :             :  * Check that the number of parameter placeholders in the message matches the
    4146                 :             :  * number of parameters passed to it, if a message was given.
    4147                 :             :  */
    4148                 :             : static void
    4149                 :        4295 : check_raise_parameters(PLpgSQL_stmt_raise *stmt)
    4150                 :             : {
    4151                 :             :     char       *cp;
    4152                 :        4295 :     int         expected_nparams = 0;
    4153                 :             : 
    4154         [ +  + ]:        4295 :     if (stmt->message == NULL)
    4155                 :         124 :         return;
    4156                 :             : 
    4157         [ +  + ]:       86448 :     for (cp = stmt->message; *cp; cp++)
    4158                 :             :     {
    4159         [ +  + ]:       82277 :         if (cp[0] == '%')
    4160                 :             :         {
    4161                 :             :             /* ignore literal % characters */
    4162         [ +  + ]:        8751 :             if (cp[1] == '%')
    4163                 :           4 :                 cp++;
    4164                 :             :             else
    4165                 :        8747 :                 expected_nparams++;
    4166                 :             :         }
    4167                 :             :     }
    4168                 :             : 
    4169         [ +  + ]:        4171 :     if (expected_nparams < list_length(stmt->params))
    4170         [ +  - ]:           4 :         ereport(ERROR,
    4171                 :             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    4172                 :             :                  errmsg("too many parameters specified for RAISE")));
    4173         [ +  + ]:        4167 :     if (expected_nparams > list_length(stmt->params))
    4174         [ +  - ]:           4 :         ereport(ERROR,
    4175                 :             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    4176                 :             :                  errmsg("too few parameters specified for RAISE")));
    4177                 :             : }
    4178                 :             : 
    4179                 :             : /*
    4180                 :             :  * Fix up CASE statement
    4181                 :             :  */
    4182                 :             : static PLpgSQL_stmt *
    4183                 :          11 : make_case(int location, PLpgSQL_expr *t_expr,
    4184                 :             :           List *case_when_list, List *else_stmts, yyscan_t yyscanner)
    4185                 :             : {
    4186                 :             :     PLpgSQL_stmt_case *new;
    4187                 :             : 
    4188                 :          11 :     new = palloc_object(PLpgSQL_stmt_case);
    4189                 :          11 :     new->cmd_type = PLPGSQL_STMT_CASE;
    4190                 :          11 :     new->lineno = plpgsql_location_to_lineno(location, yyscanner);
    4191                 :          11 :     new->stmtid = ++plpgsql_curr_compile->nstatements;
    4192                 :          11 :     new->t_expr = t_expr;
    4193                 :          11 :     new->t_varno = 0;
    4194                 :          11 :     new->case_when_list = case_when_list;
    4195                 :          11 :     new->have_else = (else_stmts != NIL);
    4196                 :             :     /* Get rid of list-with-NULL hack */
    4197   [ +  +  -  + ]:          11 :     if (list_length(else_stmts) == 1 && linitial(else_stmts) == NULL)
    4198                 :           0 :         new->else_stmts = NIL;
    4199                 :             :     else
    4200                 :          11 :         new->else_stmts = else_stmts;
    4201                 :             : 
    4202                 :             :     /*
    4203                 :             :      * When test expression is present, we create a var for it and then
    4204                 :             :      * convert all the WHEN expressions to "VAR IN (original_expression)".
    4205                 :             :      * This is a bit klugy, but okay since we haven't yet done more than read
    4206                 :             :      * the expressions as text.  (Note that previous parsing won't have
    4207                 :             :      * complained if the WHEN ... THEN expression contained multiple
    4208                 :             :      * comma-separated values.)
    4209                 :             :      */
    4210         [ +  + ]:          11 :     if (t_expr)
    4211                 :             :     {
    4212                 :             :         char        varname[32];
    4213                 :             :         PLpgSQL_var *t_var;
    4214                 :             :         ListCell   *l;
    4215                 :             : 
    4216                 :             :         /* use a name unlikely to collide with any user names */
    4217                 :          10 :         snprintf(varname, sizeof(varname), "__Case__Variable_%d__",
    4218                 :             :                  plpgsql_nDatums);
    4219                 :             : 
    4220                 :             :         /*
    4221                 :             :          * We don't yet know the result datatype of t_expr.  Build the
    4222                 :             :          * variable as if it were INT4; we'll fix this at runtime if needed.
    4223                 :             :          */
    4224                 :             :         t_var = (PLpgSQL_var *)
    4225                 :          10 :             plpgsql_build_variable(varname, new->lineno,
    4226                 :             :                                    plpgsql_build_datatype(INT4OID,
    4227                 :             :                                                           -1,
    4228                 :             :                                                           InvalidOid,
    4229                 :             :                                                           NULL),
    4230                 :             :                                    true);
    4231                 :          10 :         new->t_varno = t_var->dno;
    4232                 :             : 
    4233   [ +  -  +  +  :          32 :         foreach(l, case_when_list)
                   +  + ]
    4234                 :             :         {
    4235                 :          22 :             PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
    4236                 :          22 :             PLpgSQL_expr *expr = cwt->expr;
    4237                 :             :             StringInfoData ds;
    4238                 :             : 
    4239                 :             :             /* We expect to have expressions not statements */
    4240                 :             :             Assert(expr->parseMode == RAW_PARSE_PLPGSQL_EXPR);
    4241                 :             : 
    4242                 :             :             /* Do the string hacking */
    4243                 :          22 :             initStringInfo(&ds);
    4244                 :             : 
    4245                 :          22 :             appendStringInfo(&ds, "\"%s\" IN (%s)",
    4246                 :             :                              varname, expr->query);
    4247                 :             : 
    4248                 :          22 :             pfree(expr->query);
    4249                 :          22 :             expr->query = pstrdup(ds.data);
    4250                 :             :             /* Adjust expr's namespace to include the case variable */
    4251                 :          22 :             expr->ns = plpgsql_ns_top();
    4252                 :             : 
    4253                 :          22 :             pfree(ds.data);
    4254                 :             :         }
    4255                 :             :     }
    4256                 :             : 
    4257                 :          11 :     return (PLpgSQL_stmt *) new;
    4258                 :             : }
        

Generated by: LCOV version 2.0-1