1 /* A Bison parser, made by GNU Bison 3.3.2.  */
2 
3 /* Bison implementation for Yacc-like parsers in C
4 
5    Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2019 Free Software Foundation,
6    Inc.
7 
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 3 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20 
21 /* As a special exception, you may create a larger work that contains
22    part or all of the Bison parser skeleton and distribute that work
23    under terms of your choice, so long as that work isn't itself a
24    parser generator using the skeleton or a modified version thereof
25    as a parser skeleton.  Alternatively, if you modify or redistribute
26    the parser skeleton itself, you may (at your option) remove this
27    special exception, which will cause the skeleton and the resulting
28    Bison output files to be licensed under the GNU General Public
29    License without this special exception.
30 
31    This special exception was added by the Free Software Foundation in
32    version 2.2 of Bison.  */
33 
34 /* C LALR(1) parser skeleton written by Richard Stallman, by
35    simplifying the original so-called "semantic" parser.  */
36 
37 /* All symbols defined below should begin with yy or YY, to avoid
38    infringing on user name space.  This should be done even for local
39    variables, as they might otherwise be expanded by user macros.
40    There are some unavoidable exceptions within include files to
41    define necessary library symbols; they are noted "INFRINGES ON
42    USER NAME SPACE" below.  */
43 
44 /* Undocumented macros, especially those whose name start with YY_,
45    are private implementation details.  Do not rely on them.  */
46 
47 /* Identify Bison output.  */
48 #define YYBISON 1
49 
50 /* Bison version.  */
51 #define YYBISON_VERSION "3.3.2"
52 
53 /* Skeleton name.  */
54 #define YYSKELETON_NAME "yacc.c"
55 
56 /* Pure parsers.  */
57 #define YYPURE 0
58 
59 /* Push parsers.  */
60 #define YYPUSH 0
61 
62 /* Pull parsers.  */
63 #define YYPULL 1
64 
65 
66 /* Substitute the variable and function names.  */
67 #define yyparse         plpgsql_yyparse
68 #define yylex           plpgsql_yylex
69 #define yyerror         plpgsql_yyerror
70 #define yydebug         plpgsql_yydebug
71 #define yynerrs         plpgsql_yynerrs
72 
73 #define yylval          plpgsql_yylval
74 #define yychar          plpgsql_yychar
75 #define yylloc          plpgsql_yylloc
76 
77 /* First part of user prologue.  */
78 #line 1 "pl_gram.y" /* yacc.c:337  */
79 
80 /*-------------------------------------------------------------------------
81  *
82  * pl_gram.y			- Parser for the PL/pgSQL procedural language
83  *
84  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
85  * Portions Copyright (c) 1994, Regents of the University of California
86  *
87  *
88  * IDENTIFICATION
89  *	  src/pl/plpgsql/src/pl_gram.y
90  *
91  *-------------------------------------------------------------------------
92  */
93 
94 #include "postgres.h"
95 
96 #include "catalog/namespace.h"
97 #include "catalog/pg_proc.h"
98 #include "catalog/pg_type.h"
99 #include "parser/parser.h"
100 #include "parser/parse_type.h"
101 #include "parser/scanner.h"
102 #include "parser/scansup.h"
103 #include "utils/builtins.h"
104 
105 #include "plpgsql.h"
106 
107 
108 /* Location tracking support --- simpler than bison's default */
109 #define YYLLOC_DEFAULT(Current, Rhs, N) \
110 	do { \
111 		if (N) \
112 			(Current) = (Rhs)[1]; \
113 		else \
114 			(Current) = (Rhs)[0]; \
115 	} while (0)
116 
117 /*
118  * Bison doesn't allocate anything that needs to live across parser calls,
119  * so we can easily have it use palloc instead of malloc.  This prevents
120  * memory leaks if we error out during parsing.  Note this only works with
121  * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
122  * if possible, so there's not really much problem anyhow, at least if
123  * you're building with gcc.
124  */
125 #define YYMALLOC palloc
126 #define YYFREE   pfree
127 
128 
129 typedef struct
130 {
131 	int			location;
132 	int			leaderlen;
133 } sql_error_callback_arg;
134 
135 #define parser_errposition(pos)  plpgsql_scanner_errposition(pos)
136 
137 union YYSTYPE;					/* need forward reference for tok_is_keyword */
138 
139 static	bool			tok_is_keyword(int token, union YYSTYPE *lval,
140 									   int kw_token, const char *kw_str);
141 static	void			word_is_not_variable(PLword *word, int location);
142 static	void			cword_is_not_variable(PLcword *cword, int location);
143 static	void			current_token_is_not_variable(int tok);
144 static	PLpgSQL_expr	*read_sql_construct(int until,
145 											int until2,
146 											int until3,
147 											const char *expected,
148 											const char *sqlstart,
149 											bool isexpression,
150 											bool valid_sql,
151 											bool trim,
152 											int *startloc,
153 											int *endtoken);
154 static	PLpgSQL_expr	*read_sql_expression(int until,
155 											 const char *expected);
156 static	PLpgSQL_expr	*read_sql_expression2(int until, int until2,
157 											  const char *expected,
158 											  int *endtoken);
159 static	PLpgSQL_expr	*read_sql_stmt(const char *sqlstart);
160 static	PLpgSQL_type	*read_datatype(int tok);
161 static	PLpgSQL_stmt	*make_execsql_stmt(int firsttoken, int location);
162 static	PLpgSQL_stmt_fetch *read_fetch_direction(void);
163 static	void			 complete_direction(PLpgSQL_stmt_fetch *fetch,
164 											bool *check_FROM);
165 static	PLpgSQL_stmt	*make_return_stmt(int location);
166 static	PLpgSQL_stmt	*make_return_next_stmt(int location);
167 static	PLpgSQL_stmt	*make_return_query_stmt(int location);
168 static  PLpgSQL_stmt	*make_case(int location, PLpgSQL_expr *t_expr,
169 								   List *case_when_list, List *else_stmts);
170 static	char			*NameOfDatum(PLwdatum *wdatum);
171 static	void			 check_assignable(PLpgSQL_datum *datum, int location);
172 static	void			 read_into_target(PLpgSQL_variable **target,
173 										  bool *strict);
174 static	PLpgSQL_row		*read_into_scalar_list(char *initial_name,
175 											   PLpgSQL_datum *initial_datum,
176 											   int initial_location);
177 static	PLpgSQL_row		*make_scalar_list1(char *initial_name,
178 										   PLpgSQL_datum *initial_datum,
179 										   int lineno, int location);
180 static	void			 check_sql_expr(const char *stmt, int location,
181 										int leaderlen);
182 static	void			 plpgsql_sql_error_callback(void *arg);
183 static	PLpgSQL_type	*parse_datatype(const char *string, int location);
184 static	void			 check_labels(const char *start_label,
185 									  const char *end_label,
186 									  int end_location);
187 static	PLpgSQL_expr	*read_cursor_args(PLpgSQL_var *cursor,
188 										  int until, const char *expected);
189 static	List			*read_raise_options(void);
190 static	void			check_raise_parameters(PLpgSQL_stmt_raise *stmt);
191 
192 
193 #line 194 "pl_gram.c" /* yacc.c:337  */
194 # ifndef YY_NULLPTR
195 #  if defined __cplusplus
196 #   if 201103L <= __cplusplus
197 #    define YY_NULLPTR nullptr
198 #   else
199 #    define YY_NULLPTR 0
200 #   endif
201 #  else
202 #   define YY_NULLPTR ((void*)0)
203 #  endif
204 # endif
205 
206 /* Enabling verbose error messages.  */
207 #ifdef YYERROR_VERBOSE
208 # undef YYERROR_VERBOSE
209 # define YYERROR_VERBOSE 1
210 #else
211 # define YYERROR_VERBOSE 0
212 #endif
213 
214 /* In a future release of Bison, this section will be replaced
215    by #include "pl_gram.h".  */
216 #ifndef YY_PLPGSQL_YY_PL_GRAM_H_INCLUDED
217 # define YY_PLPGSQL_YY_PL_GRAM_H_INCLUDED
218 /* Debug traces.  */
219 #ifndef YYDEBUG
220 # define YYDEBUG 0
221 #endif
222 #if YYDEBUG
223 extern int plpgsql_yydebug;
224 #endif
225 
226 /* Token type.  */
227 #ifndef YYTOKENTYPE
228 # define YYTOKENTYPE
229   enum yytokentype
230   {
231     IDENT = 258,
232     FCONST = 259,
233     SCONST = 260,
234     BCONST = 261,
235     XCONST = 262,
236     Op = 263,
237     ICONST = 264,
238     PARAM = 265,
239     TYPECAST = 266,
240     DOT_DOT = 267,
241     COLON_EQUALS = 268,
242     EQUALS_GREATER = 269,
243     LESS_EQUALS = 270,
244     GREATER_EQUALS = 271,
245     NOT_EQUALS = 272,
246     T_WORD = 273,
247     T_CWORD = 274,
248     T_DATUM = 275,
249     LESS_LESS = 276,
250     GREATER_GREATER = 277,
251     K_ABSOLUTE = 278,
252     K_ALIAS = 279,
253     K_ALL = 280,
254     K_ARRAY = 281,
255     K_ASSERT = 282,
256     K_BACKWARD = 283,
257     K_BEGIN = 284,
258     K_BY = 285,
259     K_CALL = 286,
260     K_CASE = 287,
261     K_CLOSE = 288,
262     K_COLLATE = 289,
263     K_COLUMN = 290,
264     K_COLUMN_NAME = 291,
265     K_COMMIT = 292,
266     K_CONSTANT = 293,
267     K_CONSTRAINT = 294,
268     K_CONSTRAINT_NAME = 295,
269     K_CONTINUE = 296,
270     K_CURRENT = 297,
271     K_CURSOR = 298,
272     K_DATATYPE = 299,
273     K_DEBUG = 300,
274     K_DECLARE = 301,
275     K_DEFAULT = 302,
276     K_DETAIL = 303,
277     K_DIAGNOSTICS = 304,
278     K_DO = 305,
279     K_DUMP = 306,
280     K_ELSE = 307,
281     K_ELSIF = 308,
282     K_END = 309,
283     K_ERRCODE = 310,
284     K_ERROR = 311,
285     K_EXCEPTION = 312,
286     K_EXECUTE = 313,
287     K_EXIT = 314,
288     K_FETCH = 315,
289     K_FIRST = 316,
290     K_FOR = 317,
291     K_FOREACH = 318,
292     K_FORWARD = 319,
293     K_FROM = 320,
294     K_GET = 321,
295     K_HINT = 322,
296     K_IF = 323,
297     K_IMPORT = 324,
298     K_IN = 325,
299     K_INFO = 326,
300     K_INSERT = 327,
301     K_INTO = 328,
302     K_IS = 329,
303     K_LAST = 330,
304     K_LOG = 331,
305     K_LOOP = 332,
306     K_MESSAGE = 333,
307     K_MESSAGE_TEXT = 334,
308     K_MOVE = 335,
309     K_NEXT = 336,
310     K_NO = 337,
311     K_NOT = 338,
312     K_NOTICE = 339,
313     K_NULL = 340,
314     K_OPEN = 341,
315     K_OPTION = 342,
316     K_OR = 343,
317     K_PERFORM = 344,
318     K_PG_CONTEXT = 345,
319     K_PG_DATATYPE_NAME = 346,
320     K_PG_EXCEPTION_CONTEXT = 347,
321     K_PG_EXCEPTION_DETAIL = 348,
322     K_PG_EXCEPTION_HINT = 349,
323     K_PRINT_STRICT_PARAMS = 350,
324     K_PRIOR = 351,
325     K_QUERY = 352,
326     K_RAISE = 353,
327     K_RELATIVE = 354,
328     K_RESET = 355,
329     K_RESULT_OID = 356,
330     K_RETURN = 357,
331     K_RETURNED_SQLSTATE = 358,
332     K_REVERSE = 359,
333     K_ROLLBACK = 360,
334     K_ROW_COUNT = 361,
335     K_ROWTYPE = 362,
336     K_SCHEMA = 363,
337     K_SCHEMA_NAME = 364,
338     K_SCROLL = 365,
339     K_SET = 366,
340     K_SLICE = 367,
341     K_SQLSTATE = 368,
342     K_STACKED = 369,
343     K_STRICT = 370,
344     K_TABLE = 371,
345     K_TABLE_NAME = 372,
346     K_THEN = 373,
347     K_TO = 374,
348     K_TYPE = 375,
349     K_USE_COLUMN = 376,
350     K_USE_VARIABLE = 377,
351     K_USING = 378,
352     K_VARIABLE_CONFLICT = 379,
353     K_WARNING = 380,
354     K_WHEN = 381,
355     K_WHILE = 382
356   };
357 #endif
358 
359 /* Value type.  */
360 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
361 
362 union YYSTYPE
363 {
364 #line 120 "pl_gram.y" /* yacc.c:352  */
365 
366 		core_YYSTYPE			core_yystype;
367 		/* these fields must match core_YYSTYPE: */
368 		int						ival;
369 		char					*str;
370 		const char				*keyword;
371 
372 		PLword					word;
373 		PLcword					cword;
374 		PLwdatum				wdatum;
375 		bool					boolean;
376 		Oid						oid;
377 		struct
378 		{
379 			char *name;
380 			int  lineno;
381 		}						varname;
382 		struct
383 		{
384 			char *name;
385 			int  lineno;
386 			PLpgSQL_datum   *scalar;
387 			PLpgSQL_datum   *row;
388 		}						forvariable;
389 		struct
390 		{
391 			char *label;
392 			int  n_initvars;
393 			int  *initvarnos;
394 		}						declhdr;
395 		struct
396 		{
397 			List *stmts;
398 			char *end_label;
399 			int   end_label_location;
400 		}						loop_body;
401 		List					*list;
402 		PLpgSQL_type			*dtype;
403 		PLpgSQL_datum			*datum;
404 		PLpgSQL_var				*var;
405 		PLpgSQL_expr			*expr;
406 		PLpgSQL_stmt			*stmt;
407 		PLpgSQL_condition		*condition;
408 		PLpgSQL_exception		*exception;
409 		PLpgSQL_exception_block	*exception_block;
410 		PLpgSQL_nsitem			*nsitem;
411 		PLpgSQL_diag_item		*diagitem;
412 		PLpgSQL_stmt_fetch		*fetch;
413 		PLpgSQL_case_when		*casewhen;
414 
415 #line 416 "pl_gram.c" /* yacc.c:352  */
416 };
417 
418 typedef union YYSTYPE YYSTYPE;
419 # define YYSTYPE_IS_TRIVIAL 1
420 # define YYSTYPE_IS_DECLARED 1
421 #endif
422 
423 /* Location type.  */
424 #if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
425 typedef struct YYLTYPE YYLTYPE;
426 struct YYLTYPE
427 {
428   int first_line;
429   int first_column;
430   int last_line;
431   int last_column;
432 };
433 # define YYLTYPE_IS_DECLARED 1
434 # define YYLTYPE_IS_TRIVIAL 1
435 #endif
436 
437 
438 extern YYSTYPE plpgsql_yylval;
439 extern YYLTYPE plpgsql_yylloc;
440 int plpgsql_yyparse (void);
441 
442 #endif /* !YY_PLPGSQL_YY_PL_GRAM_H_INCLUDED  */
443 
444 
445 
446 #ifdef short
447 # undef short
448 #endif
449 
450 #ifdef YYTYPE_UINT8
451 typedef YYTYPE_UINT8 yytype_uint8;
452 #else
453 typedef unsigned char yytype_uint8;
454 #endif
455 
456 #ifdef YYTYPE_INT8
457 typedef YYTYPE_INT8 yytype_int8;
458 #else
459 typedef signed char yytype_int8;
460 #endif
461 
462 #ifdef YYTYPE_UINT16
463 typedef YYTYPE_UINT16 yytype_uint16;
464 #else
465 typedef unsigned short yytype_uint16;
466 #endif
467 
468 #ifdef YYTYPE_INT16
469 typedef YYTYPE_INT16 yytype_int16;
470 #else
471 typedef short yytype_int16;
472 #endif
473 
474 #ifndef YYSIZE_T
475 # ifdef __SIZE_TYPE__
476 #  define YYSIZE_T __SIZE_TYPE__
477 # elif defined size_t
478 #  define YYSIZE_T size_t
479 # elif ! defined YYSIZE_T
480 #  include <stddef.h> /* INFRINGES ON USER NAME SPACE */
481 #  define YYSIZE_T size_t
482 # else
483 #  define YYSIZE_T unsigned
484 # endif
485 #endif
486 
487 #define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
488 
489 #ifndef YY_
490 # if defined YYENABLE_NLS && YYENABLE_NLS
491 #  if ENABLE_NLS
492 #   include <libintl.h> /* INFRINGES ON USER NAME SPACE */
493 #   define YY_(Msgid) dgettext ("bison-runtime", Msgid)
494 #  endif
495 # endif
496 # ifndef YY_
497 #  define YY_(Msgid) Msgid
498 # endif
499 #endif
500 
501 #ifndef YY_ATTRIBUTE
502 # if (defined __GNUC__                                               \
503       && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)))  \
504      || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C
505 #  define YY_ATTRIBUTE(Spec) __attribute__(Spec)
506 # else
507 #  define YY_ATTRIBUTE(Spec) /* empty */
508 # endif
509 #endif
510 
511 #ifndef YY_ATTRIBUTE_PURE
512 # define YY_ATTRIBUTE_PURE   YY_ATTRIBUTE ((__pure__))
513 #endif
514 
515 #ifndef YY_ATTRIBUTE_UNUSED
516 # define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__))
517 #endif
518 
519 /* Suppress unused-variable warnings by "using" E.  */
520 #if ! defined lint || defined __GNUC__
521 # define YYUSE(E) ((void) (E))
522 #else
523 # define YYUSE(E) /* empty */
524 #endif
525 
526 #if defined __GNUC__ && ! defined __ICC && 407 <= __GNUC__ * 100 + __GNUC_MINOR__
527 /* Suppress an incorrect diagnostic about yylval being uninitialized.  */
528 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
529     _Pragma ("GCC diagnostic push") \
530     _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\
531     _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
532 # define YY_IGNORE_MAYBE_UNINITIALIZED_END \
533     _Pragma ("GCC diagnostic pop")
534 #else
535 # define YY_INITIAL_VALUE(Value) Value
536 #endif
537 #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
538 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
539 # define YY_IGNORE_MAYBE_UNINITIALIZED_END
540 #endif
541 #ifndef YY_INITIAL_VALUE
542 # define YY_INITIAL_VALUE(Value) /* Nothing. */
543 #endif
544 
545 
546 #if ! defined yyoverflow || YYERROR_VERBOSE
547 
548 /* The parser invokes alloca or malloc; define the necessary symbols.  */
549 
550 # ifdef YYSTACK_USE_ALLOCA
551 #  if YYSTACK_USE_ALLOCA
552 #   ifdef __GNUC__
553 #    define YYSTACK_ALLOC __builtin_alloca
554 #   elif defined __BUILTIN_VA_ARG_INCR
555 #    include <alloca.h> /* INFRINGES ON USER NAME SPACE */
556 #   elif defined _AIX
557 #    define YYSTACK_ALLOC __alloca
558 #   elif defined _MSC_VER
559 #    include <malloc.h> /* INFRINGES ON USER NAME SPACE */
560 #    define alloca _alloca
561 #   else
562 #    define YYSTACK_ALLOC alloca
563 #    if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS
564 #     include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
565       /* Use EXIT_SUCCESS as a witness for stdlib.h.  */
566 #     ifndef EXIT_SUCCESS
567 #      define EXIT_SUCCESS 0
568 #     endif
569 #    endif
570 #   endif
571 #  endif
572 # endif
573 
574 # ifdef YYSTACK_ALLOC
575    /* Pacify GCC's 'empty if-body' warning.  */
576 #  define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
577 #  ifndef YYSTACK_ALLOC_MAXIMUM
578     /* The OS might guarantee only one guard page at the bottom of the stack,
579        and a page size can be as small as 4096 bytes.  So we cannot safely
580        invoke alloca (N) if N exceeds 4096.  Use a slightly smaller number
581        to allow for a few compiler-allocated temporary stack slots.  */
582 #   define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
583 #  endif
584 # else
585 #  define YYSTACK_ALLOC YYMALLOC
586 #  define YYSTACK_FREE YYFREE
587 #  ifndef YYSTACK_ALLOC_MAXIMUM
588 #   define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
589 #  endif
590 #  if (defined __cplusplus && ! defined EXIT_SUCCESS \
591        && ! ((defined YYMALLOC || defined malloc) \
592              && (defined YYFREE || defined free)))
593 #   include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
594 #   ifndef EXIT_SUCCESS
595 #    define EXIT_SUCCESS 0
596 #   endif
597 #  endif
598 #  ifndef YYMALLOC
599 #   define YYMALLOC malloc
600 #   if ! defined malloc && ! defined EXIT_SUCCESS
601 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
602 #   endif
603 #  endif
604 #  ifndef YYFREE
605 #   define YYFREE free
606 #   if ! defined free && ! defined EXIT_SUCCESS
607 void free (void *); /* INFRINGES ON USER NAME SPACE */
608 #   endif
609 #  endif
610 # endif
611 #endif /* ! defined yyoverflow || YYERROR_VERBOSE */
612 
613 
614 #if (! defined yyoverflow \
615      && (! defined __cplusplus \
616          || (defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL \
617              && defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
618 
619 /* A type that is properly aligned for any stack member.  */
620 union yyalloc
621 {
622   yytype_int16 yyss_alloc;
623   YYSTYPE yyvs_alloc;
624   YYLTYPE yyls_alloc;
625 };
626 
627 /* The size of the maximum gap between one aligned stack and the next.  */
628 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
629 
630 /* The size of an array large to enough to hold all stacks, each with
631    N elements.  */
632 # define YYSTACK_BYTES(N) \
633      ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE) + sizeof (YYLTYPE)) \
634       + 2 * YYSTACK_GAP_MAXIMUM)
635 
636 # define YYCOPY_NEEDED 1
637 
638 /* Relocate STACK from its old location to the new one.  The
639    local variables YYSIZE and YYSTACKSIZE give the old and new number of
640    elements in the stack, and YYPTR gives the new location of the
641    stack.  Advance YYPTR to a properly aligned location for the next
642    stack.  */
643 # define YYSTACK_RELOCATE(Stack_alloc, Stack)                           \
644     do                                                                  \
645       {                                                                 \
646         YYSIZE_T yynewbytes;                                            \
647         YYCOPY (&yyptr->Stack_alloc, Stack, yysize);                    \
648         Stack = &yyptr->Stack_alloc;                                    \
649         yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
650         yyptr += yynewbytes / sizeof (*yyptr);                          \
651       }                                                                 \
652     while (0)
653 
654 #endif
655 
656 #if defined YYCOPY_NEEDED && YYCOPY_NEEDED
657 /* Copy COUNT objects from SRC to DST.  The source and destination do
658    not overlap.  */
659 # ifndef YYCOPY
660 #  if defined __GNUC__ && 1 < __GNUC__
661 #   define YYCOPY(Dst, Src, Count) \
662       __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src)))
663 #  else
664 #   define YYCOPY(Dst, Src, Count)              \
665       do                                        \
666         {                                       \
667           YYSIZE_T yyi;                         \
668           for (yyi = 0; yyi < (Count); yyi++)   \
669             (Dst)[yyi] = (Src)[yyi];            \
670         }                                       \
671       while (0)
672 #  endif
673 # endif
674 #endif /* !YYCOPY_NEEDED */
675 
676 /* YYFINAL -- State number of the termination state.  */
677 #define YYFINAL  3
678 /* YYLAST -- Last index in YYTABLE.  */
679 #define YYLAST   1385
680 
681 /* YYNTOKENS -- Number of terminals.  */
682 #define YYNTOKENS  135
683 /* YYNNTS -- Number of nonterminals.  */
684 #define YYNNTS  89
685 /* YYNRULES -- Number of rules.  */
686 #define YYNRULES  254
687 /* YYNSTATES -- Number of states.  */
688 #define YYNSTATES  335
689 
690 #define YYUNDEFTOK  2
691 #define YYMAXUTOK   382
692 
693 /* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM
694    as returned by yylex, with out-of-bounds checking.  */
695 #define YYTRANSLATE(YYX)                                                \
696   ((unsigned) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
697 
698 /* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM
699    as returned by yylex.  */
700 static const yytype_uint8 yytranslate[] =
701 {
702        0,     2,     2,     2,     2,     2,     2,     2,     2,     2,
703        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
704        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
705        2,     2,     2,     2,     2,   128,     2,     2,     2,     2,
706      130,   131,     2,     2,   132,     2,     2,     2,     2,     2,
707        2,     2,     2,     2,     2,     2,     2,     2,     2,   129,
708        2,   133,     2,     2,     2,     2,     2,     2,     2,     2,
709        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
710        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
711        2,   134,     2,     2,     2,     2,     2,     2,     2,     2,
712        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
713        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
714        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
715        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
716        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
717        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
718        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
719        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
720        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
721        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
722        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
723        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
724        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
725        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
726        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
727        2,     2,     2,     2,     2,     2,     1,     2,     3,     4,
728        5,     6,     7,     8,     9,    10,    11,    12,    13,    14,
729       15,    16,    17,    18,    19,    20,    21,    22,    23,    24,
730       25,    26,    27,    28,    29,    30,    31,    32,    33,    34,
731       35,    36,    37,    38,    39,    40,    41,    42,    43,    44,
732       45,    46,    47,    48,    49,    50,    51,    52,    53,    54,
733       55,    56,    57,    58,    59,    60,    61,    62,    63,    64,
734       65,    66,    67,    68,    69,    70,    71,    72,    73,    74,
735       75,    76,    77,    78,    79,    80,    81,    82,    83,    84,
736       85,    86,    87,    88,    89,    90,    91,    92,    93,    94,
737       95,    96,    97,    98,    99,   100,   101,   102,   103,   104,
738      105,   106,   107,   108,   109,   110,   111,   112,   113,   114,
739      115,   116,   117,   118,   119,   120,   121,   122,   123,   124,
740      125,   126,   127
741 };
742 
743 #if YYDEBUG
744   /* YYRLINE[YYN] -- Source line where rule number YYN was defined.  */
745 static const yytype_uint16 yyrline[] =
746 {
747        0,   360,   360,   366,   367,   370,   374,   383,   387,   391,
748      397,   401,   406,   407,   410,   432,   440,   447,   456,   468,
749      469,   472,   473,   477,   490,   528,   534,   533,   586,   589,
750      593,   600,   606,   609,   640,   644,   650,   658,   659,   661,
751      676,   691,   719,   747,   778,   779,   784,   795,   796,   801,
752      806,   813,   814,   818,   820,   826,   827,   835,   836,   840,
753      841,   851,   853,   855,   857,   859,   861,   863,   865,   867,
754      869,   871,   873,   875,   877,   879,   881,   883,   885,   887,
755      889,   891,   893,   895,   897,   899,   903,   916,   929,   945,
756      959,  1022,  1025,  1029,  1035,  1039,  1045,  1058,  1105,  1116,
757     1121,  1129,  1134,  1151,  1168,  1171,  1185,  1188,  1194,  1201,
758     1215,  1219,  1225,  1237,  1240,  1255,  1272,  1290,  1324,  1582,
759     1608,  1622,  1629,  1667,  1670,  1676,  1728,  1732,  1738,  1764,
760     1908,  1931,  1949,  1953,  1957,  1967,  1979,  2042,  2119,  2149,
761     2162,  2167,  2180,  2187,  2199,  2211,  2222,  2236,  2258,  2263,
762     2271,  2273,  2272,  2314,  2318,  2324,  2337,  2346,  2352,  2389,
763     2393,  2397,  2401,  2405,  2409,  2417,  2421,  2429,  2432,  2439,
764     2441,  2448,  2452,  2456,  2465,  2466,  2467,  2468,  2469,  2470,
765     2471,  2472,  2473,  2474,  2475,  2476,  2477,  2478,  2479,  2480,
766     2481,  2482,  2483,  2484,  2485,  2486,  2487,  2488,  2489,  2490,
767     2491,  2492,  2493,  2494,  2495,  2496,  2497,  2498,  2499,  2500,
768     2501,  2502,  2503,  2504,  2505,  2506,  2507,  2508,  2509,  2510,
769     2511,  2512,  2513,  2514,  2515,  2516,  2517,  2518,  2519,  2520,
770     2521,  2522,  2523,  2524,  2525,  2526,  2527,  2528,  2529,  2530,
771     2531,  2532,  2533,  2534,  2535,  2536,  2537,  2538,  2539,  2540,
772     2541,  2542,  2543,  2544,  2545
773 };
774 #endif
775 
776 #if YYDEBUG || YYERROR_VERBOSE || 0
777 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
778    First, the terminals, then, starting at YYNTOKENS, nonterminals.  */
779 static const char *const yytname[] =
780 {
781   "$end", "error", "$undefined", "IDENT", "FCONST", "SCONST", "BCONST",
782   "XCONST", "Op", "ICONST", "PARAM", "TYPECAST", "DOT_DOT", "COLON_EQUALS",
783   "EQUALS_GREATER", "LESS_EQUALS", "GREATER_EQUALS", "NOT_EQUALS",
784   "T_WORD", "T_CWORD", "T_DATUM", "LESS_LESS", "GREATER_GREATER",
785   "K_ABSOLUTE", "K_ALIAS", "K_ALL", "K_ARRAY", "K_ASSERT", "K_BACKWARD",
786   "K_BEGIN", "K_BY", "K_CALL", "K_CASE", "K_CLOSE", "K_COLLATE",
787   "K_COLUMN", "K_COLUMN_NAME", "K_COMMIT", "K_CONSTANT", "K_CONSTRAINT",
788   "K_CONSTRAINT_NAME", "K_CONTINUE", "K_CURRENT", "K_CURSOR", "K_DATATYPE",
789   "K_DEBUG", "K_DECLARE", "K_DEFAULT", "K_DETAIL", "K_DIAGNOSTICS", "K_DO",
790   "K_DUMP", "K_ELSE", "K_ELSIF", "K_END", "K_ERRCODE", "K_ERROR",
791   "K_EXCEPTION", "K_EXECUTE", "K_EXIT", "K_FETCH", "K_FIRST", "K_FOR",
792   "K_FOREACH", "K_FORWARD", "K_FROM", "K_GET", "K_HINT", "K_IF",
793   "K_IMPORT", "K_IN", "K_INFO", "K_INSERT", "K_INTO", "K_IS", "K_LAST",
794   "K_LOG", "K_LOOP", "K_MESSAGE", "K_MESSAGE_TEXT", "K_MOVE", "K_NEXT",
795   "K_NO", "K_NOT", "K_NOTICE", "K_NULL", "K_OPEN", "K_OPTION", "K_OR",
796   "K_PERFORM", "K_PG_CONTEXT", "K_PG_DATATYPE_NAME",
797   "K_PG_EXCEPTION_CONTEXT", "K_PG_EXCEPTION_DETAIL", "K_PG_EXCEPTION_HINT",
798   "K_PRINT_STRICT_PARAMS", "K_PRIOR", "K_QUERY", "K_RAISE", "K_RELATIVE",
799   "K_RESET", "K_RESULT_OID", "K_RETURN", "K_RETURNED_SQLSTATE",
800   "K_REVERSE", "K_ROLLBACK", "K_ROW_COUNT", "K_ROWTYPE", "K_SCHEMA",
801   "K_SCHEMA_NAME", "K_SCROLL", "K_SET", "K_SLICE", "K_SQLSTATE",
802   "K_STACKED", "K_STRICT", "K_TABLE", "K_TABLE_NAME", "K_THEN", "K_TO",
803   "K_TYPE", "K_USE_COLUMN", "K_USE_VARIABLE", "K_USING",
804   "K_VARIABLE_CONFLICT", "K_WARNING", "K_WHEN", "K_WHILE", "'#'", "';'",
805   "'('", "')'", "','", "'='", "'['", "$accept", "pl_function",
806   "comp_options", "comp_option", "option_value", "opt_semi", "pl_block",
807   "decl_sect", "decl_start", "decl_stmts", "decl_stmt", "decl_statement",
808   "$@1", "opt_scrollable", "decl_cursor_query", "decl_cursor_args",
809   "decl_cursor_arglist", "decl_cursor_arg", "decl_is_for",
810   "decl_aliasitem", "decl_varname", "decl_const", "decl_datatype",
811   "decl_collate", "decl_notnull", "decl_defval", "decl_defkey",
812   "assign_operator", "proc_sect", "proc_stmt", "stmt_perform", "stmt_call",
813   "stmt_assign", "stmt_getdiag", "getdiag_area_opt", "getdiag_list",
814   "getdiag_list_item", "getdiag_item", "getdiag_target", "assign_var",
815   "stmt_if", "stmt_elsifs", "stmt_else", "stmt_case",
816   "opt_expr_until_when", "case_when_list", "case_when", "opt_case_else",
817   "stmt_loop", "stmt_while", "stmt_for", "for_control", "for_variable",
818   "stmt_foreach_a", "foreach_slice", "stmt_exit", "exit_type",
819   "stmt_return", "stmt_raise", "stmt_assert", "loop_body", "stmt_execsql",
820   "stmt_dynexecute", "stmt_open", "stmt_fetch", "stmt_move",
821   "opt_fetch_direction", "stmt_close", "stmt_null", "stmt_commit",
822   "stmt_rollback", "stmt_set", "cursor_variable", "exception_sect", "@2",
823   "proc_exceptions", "proc_exception", "proc_conditions", "proc_condition",
824   "expr_until_semi", "expr_until_rightbracket", "expr_until_then",
825   "expr_until_loop", "opt_block_label", "opt_loop_label", "opt_label",
826   "opt_exitcond", "any_identifier", "unreserved_keyword", YY_NULLPTR
827 };
828 #endif
829 
830 # ifdef YYPRINT
831 /* YYTOKNUM[NUM] -- (External) token number corresponding to the
832    (internal) symbol number NUM (which must be that of a token).  */
833 static const yytype_uint16 yytoknum[] =
834 {
835        0,   256,   257,   258,   259,   260,   261,   262,   263,   264,
836      265,   266,   267,   268,   269,   270,   271,   272,   273,   274,
837      275,   276,   277,   278,   279,   280,   281,   282,   283,   284,
838      285,   286,   287,   288,   289,   290,   291,   292,   293,   294,
839      295,   296,   297,   298,   299,   300,   301,   302,   303,   304,
840      305,   306,   307,   308,   309,   310,   311,   312,   313,   314,
841      315,   316,   317,   318,   319,   320,   321,   322,   323,   324,
842      325,   326,   327,   328,   329,   330,   331,   332,   333,   334,
843      335,   336,   337,   338,   339,   340,   341,   342,   343,   344,
844      345,   346,   347,   348,   349,   350,   351,   352,   353,   354,
845      355,   356,   357,   358,   359,   360,   361,   362,   363,   364,
846      365,   366,   367,   368,   369,   370,   371,   372,   373,   374,
847      375,   376,   377,   378,   379,   380,   381,   382,    35,    59,
848       40,    41,    44,    61,    91
849 };
850 # endif
851 
852 #define YYPACT_NINF -252
853 
854 #define yypact_value_is_default(Yystate) \
855   (!!((Yystate) == (-252)))
856 
857 #define YYTABLE_NINF -165
858 
859 #define yytable_value_is_error(Yytable_value) \
860   0
861 
862   /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
863      STATE-NUM.  */
864 static const yytype_int16 yypact[] =
865 {
866     -252,    36,   -13,  -252,   364,   -54,  -252,   -79,    32,    23,
867     -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
868     -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
869     -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
870     -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
871     -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
872     -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
873     -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
874     -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
875     -252,  -252,  -252,    56,  -252,    38,   685,   -34,  -252,  -252,
876     -252,  -252,   256,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
877     -252,  1074,  -252,   364,  -252,   256,  -252,  -252,     4,  -252,
878     -252,  -252,  -252,   364,  -252,  -252,  -252,    57,   -32,  -252,
879     -252,  -252,  -252,  -252,  -252,   -33,  -252,  -252,  -252,  -252,
880      -31,    57,  -252,  -252,  -252,  -252,   -29,  -252,   -28,  -252,
881     -252,  -252,  -252,  -252,   -10,  -252,  -252,  -252,  -252,  -252,
882     -252,  -252,   364,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
883     -252,  -252,  -252,  -252,  -252,  -252,    49,   -45,    82,  -252,
884       44,  -252,    -2,  -252,    70,  -252,   105,     2,  -252,  -252,
885     -252,     0,  -252,     6,    57,  -252,  -252,    81,  -252,    57,
886     -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,   -71,
887     -252,   364,    92,    92,  -252,  -252,  -252,   472,  -252,  -252,
888       97,     5,  -252,   -42,  -252,  -252,   364,     6,  -252,    60,
889       98,   898,     7,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
890     -252,  -252,  -252,    64,    25,  1162,  -252,  -252,  -252,  -252,
891        9,  -252,    10,   580,    52,  -252,  -252,  -252,    85,   -50,
892     -252,  -252,  -252,  -252,  -252,  -252,   -73,  -252,    -7,     8,
893       12,  -252,  -252,  -252,  -252,   132,    73,    67,  -252,  -252,
894      790,   -17,  -252,  -252,  -252,    61,    -8,    -6,  1186,   113,
895      364,  -252,  -252,    98,  -252,  -252,  -252,    93,  -252,   122,
896      364,   -60,  -252,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
897     -252,  -252,  -252,    20,  -252,   146,  -252,  -252,  1274,  -252,
898       83,  -252,    21,  -252,   790,  -252,  -252,  -252,   986,    24,
899     -252,  -252,  -252,  -252,  -252
900 };
901 
902   /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
903      Performed when YYTABLE does not specify something else to do.  Zero
904      means the default is an error.  */
905 static const yytype_uint8 yydefact[] =
906 {
907        3,     0,   163,     1,     0,     0,     4,    12,     0,    15,
908      171,   173,   174,   175,   176,   177,   178,   179,   180,   181,
909      182,   183,   184,   185,   186,   187,   188,   189,   190,   191,
910      192,   193,   194,   195,   196,   197,   198,   199,   200,   201,
911      202,   203,   204,   205,   206,   207,   208,   209,   210,   211,
912      212,   213,   214,   215,   216,   217,   218,   219,   220,   221,
913      222,   223,   224,   225,   226,   227,   228,   229,   230,   231,
914      232,   233,   234,   235,   236,   237,   238,   239,   240,   241,
915      242,   243,   244,   245,   246,   247,   248,   249,   250,   251,
916      252,   253,   254,     0,   172,     0,     0,     0,    13,     2,
917       59,    18,    16,   164,     5,    10,     6,    11,     7,     9,
918        8,   165,    42,     0,    22,    17,    20,    21,    44,    43,
919      134,   135,   101,     0,   130,    87,   109,     0,     0,   127,
920       88,   151,   136,   126,   140,    91,   161,   132,   133,   140,
921        0,     0,   159,   129,   146,   128,     0,   145,     0,    60,
922       75,    76,    62,    77,     0,    63,    64,    65,    66,    67,
923       68,    69,   167,    70,    71,    72,    73,    74,    78,    79,
924       80,    81,    82,    83,    84,    85,     0,     0,     0,    19,
925        0,    45,     0,    30,     0,    46,     0,     0,   148,   149,
926      147,     0,   143,     0,     0,    92,    93,     0,    59,     0,
927      142,   137,    86,   144,    61,    58,    57,   160,   159,     0,
928      168,   167,     0,     0,    59,   162,    23,     0,    29,    26,
929       47,   166,   161,   113,   111,   141,     0,   152,   154,     0,
930        0,   165,     0,   102,    89,   159,   169,   125,    14,   120,
931      121,   119,    59,     0,   123,   165,   115,    59,    39,    41,
932        0,    40,    32,     0,    51,    59,    59,   110,     0,     0,
933      157,   158,   153,   138,    99,   100,     0,    95,     0,    98,
934      106,   139,   170,   117,   118,     0,     0,     0,   116,    25,
935        0,     0,    48,    50,    49,     0,     0,   165,   165,     0,
936        0,    59,    90,     0,    97,    59,   161,     0,   124,     0,
937      167,     0,    34,    46,    38,    37,    31,    52,    56,    53,
938       24,    54,    55,     0,   156,   165,    94,    96,   165,    59,
939        0,   162,     0,    33,     0,    36,    27,   108,   165,     0,
940       59,   131,    35,   103,   122
941 };
942 
943   /* YYPGOTO[NTERM-NUM].  */
944 static const yytype_int16 yypgoto[] =
945 {
946     -252,  -252,  -252,  -252,  -252,  -252,   150,  -252,  -252,  -252,
947       39,  -252,  -252,  -252,  -252,  -252,  -252,  -169,  -252,  -252,
948     -251,  -252,  -147,  -252,  -252,  -252,  -252,  -219,   -96,  -252,
949     -252,  -252,  -252,  -252,  -252,  -252,  -136,  -252,  -252,  -200,
950     -252,  -252,  -252,  -252,  -252,  -252,   -62,  -252,  -252,  -252,
951     -252,  -252,   -51,  -252,  -252,  -252,  -252,  -252,  -252,  -252,
952     -223,  -252,  -252,  -252,  -252,  -252,    29,  -252,  -252,  -252,
953     -252,  -252,  -104,  -252,  -252,  -252,   -64,  -252,  -121,  -192,
954     -252,  -211,  -151,  -252,  -252,  -209,  -252,    -4,   -95
955 };
956 
957   /* YYDEFGOTO[NTERM-NUM].  */
958 static const yytype_int16 yydefgoto[] =
959 {
960       -1,     1,     2,     6,   106,    99,   148,     8,   102,   115,
961      116,   117,   252,   184,   326,   281,   301,   302,   306,   250,
962      118,   185,   220,   254,   286,   310,   311,   208,   245,   149,
963      150,   151,   152,   153,   197,   266,   267,   317,   268,   154,
964      155,   270,   297,   156,   187,   223,   224,   258,   157,   158,
965      159,   242,   243,   160,   276,   161,   162,   163,   164,   165,
966      246,   166,   167,   168,   169,   170,   194,   171,   172,   173,
967      174,   175,   191,   176,   193,   227,   228,   259,   260,   202,
968      233,   198,   247,     9,   177,   209,   237,   210,    94
969 };
970 
971   /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM.  If
972      positive, shift that token.  If negative, reduce the rule whose
973      number is the opposite.  If YYTABLE_NINF, syntax error.  */
974 static const yytype_int16 yytable[] =
975 {
976       93,   107,   238,   205,   111,   205,   205,   119,     4,   195,
977      256,   255,   120,   121,   122,   123,   234,   212,   213,   273,
978      119,   124,   108,  -163,   278,   125,   126,   127,   180,   303,
979      269,   128,   214,    95,  -164,   129,     3,   201,   290,   308,
980     -163,    96,   181,   272,   130,   304,  -112,   -28,  -112,   294,
981       98,  -164,   132,   133,   134,   235,   292,   305,   236,   293,
982      135,   100,   136,   137,   295,   296,   138,   312,   291,   101,
983       97,   323,   324,   303,   139,   188,   189,   190,   103,   140,
984      141,   196,   215,   142,   222,   319,   182,   109,   110,   104,
985      229,   322,   143,   269,   144,   232,   145,   192,   200,   146,
986      203,   204,   231,   211,   216,   147,   217,   334,   218,   178,
987      239,   240,   241,   219,   183,     5,   264,   265,   122,   186,
988     -112,   309,   251,   206,   207,   206,   206,   221,   222,   225,
989      230,   253,   226,   263,   274,   285,   271,   275,   279,   289,
990      280,   298,   207,   299,   300,   313,   307,   320,   321,   327,
991      331,   329,     7,   333,   179,   332,   325,   316,   284,   287,
992      288,   257,   244,   262,   120,   121,   122,   123,   199,   314,
993      330,     0,     0,   124,     0,  -163,     0,   125,   126,   127,
994        0,     0,     0,   128,     0,   119,     0,   129,     0,     0,
995        0,     0,  -163,     0,     0,   315,   130,     0,     0,   318,
996     -155,     0,     0,     0,   132,   133,   134,     0,     0,     0,
997        0,     0,   135,     0,   136,   137,     0,     0,   138,     0,
998        0,     0,   261,   328,     0,     0,   139,     0,     0,   119,
999        0,   140,   141,     0,     0,   142,     0,     0,     0,     0,
1000        0,     0,     0,     0,   143,     0,   144,     0,   145,     0,
1001        0,   146,     0,     0,     0,     0,     0,   147,     0,     0,
1002        0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
1003        0,     0,  -155,     0,   112,     0,     0,   113,     0,    12,
1004       13,     0,    14,    15,    16,     0,   261,    17,     0,    18,
1005       19,    20,    21,    22,    23,    24,    25,    26,    27,    28,
1006       29,    30,   114,    31,    32,    33,    34,    35,     0,    36,
1007        0,    37,    38,    39,     0,    40,    41,    42,     0,     0,
1008       43,     0,    44,    45,     0,    46,     0,    47,    48,     0,
1009       49,    50,    51,     0,    52,    53,    54,    55,    56,     0,
1010       57,     0,    58,    59,     0,    60,    61,    62,    63,    64,
1011       65,    66,    67,    68,    69,    70,    71,    72,    73,    74,
1012       75,    76,    77,    78,    79,    80,    81,    82,    83,    84,
1013       85,     0,    86,    87,     0,     0,    88,    89,    90,     0,
1014       91,    92,    10,     0,    11,     0,     0,    12,    13,     0,
1015       14,    15,    16,     0,     0,    17,     0,    18,    19,    20,
1016       21,    22,    23,    24,    25,    26,    27,    28,    29,    30,
1017        0,    31,    32,    33,    34,    35,     0,    36,     0,    37,
1018       38,    39,     0,    40,    41,    42,     0,     0,    43,     0,
1019       44,    45,     0,    46,     0,    47,    48,     0,    49,    50,
1020       51,     0,    52,    53,    54,    55,    56,     0,    57,     0,
1021       58,    59,     0,    60,    61,    62,    63,    64,    65,    66,
1022       67,    68,    69,    70,    71,    72,    73,    74,    75,    76,
1023       77,    78,    79,    80,    81,    82,    83,    84,    85,     0,
1024       86,    87,     0,     0,    88,    89,    90,     0,    91,    92,
1025      248,   249,     0,     0,     0,    12,    13,     0,    14,    15,
1026       16,     0,     0,    17,     0,    18,    19,    20,    21,    22,
1027       23,    24,    25,    26,    27,    28,    29,    30,     0,    31,
1028       32,    33,    34,    35,     0,    36,     0,    37,    38,    39,
1029        0,    40,    41,    42,     0,     0,    43,     0,    44,    45,
1030        0,    46,     0,    47,    48,     0,    49,    50,    51,     0,
1031       52,    53,    54,    55,    56,     0,    57,     0,    58,    59,
1032        0,    60,    61,    62,    63,    64,    65,    66,    67,    68,
1033       69,    70,    71,    72,    73,    74,    75,    76,    77,    78,
1034       79,    80,    81,    82,    83,    84,    85,     0,    86,    87,
1035        0,     0,    88,    89,    90,     0,    91,    92,   282,   283,
1036        0,     0,     0,    12,    13,     0,    14,    15,    16,     0,
1037        0,    17,     0,    18,    19,    20,    21,    22,    23,    24,
1038       25,    26,    27,    28,    29,    30,     0,    31,    32,    33,
1039       34,    35,     0,    36,     0,    37,    38,    39,     0,    40,
1040       41,    42,     0,     0,    43,     0,    44,    45,     0,    46,
1041        0,    47,    48,     0,    49,    50,    51,     0,    52,    53,
1042       54,    55,    56,     0,    57,     0,    58,    59,     0,    60,
1043       61,    62,    63,    64,    65,    66,    67,    68,    69,    70,
1044       71,    72,    73,    74,    75,    76,    77,    78,    79,    80,
1045       81,    82,    83,    84,    85,     0,    86,    87,     0,     0,
1046       88,    89,    90,   105,    91,    92,     0,     0,    12,    13,
1047        0,    14,    15,    16,     0,     0,    17,     0,    18,    19,
1048       20,    21,    22,    23,    24,    25,    26,    27,    28,    29,
1049       30,     0,    31,    32,    33,    34,    35,     0,    36,     0,
1050       37,    38,    39,     0,    40,    41,    42,     0,     0,    43,
1051        0,    44,    45,     0,    46,     0,    47,    48,     0,    49,
1052       50,    51,     0,    52,    53,    54,    55,    56,     0,    57,
1053        0,    58,    59,     0,    60,    61,    62,    63,    64,    65,
1054       66,    67,    68,    69,    70,    71,    72,    73,    74,    75,
1055       76,    77,    78,    79,    80,    81,    82,    83,    84,    85,
1056        0,    86,    87,     0,     0,    88,    89,    90,   112,    91,
1057       92,     0,     0,    12,    13,     0,    14,    15,    16,     0,
1058        0,    17,     0,    18,    19,    20,    21,    22,    23,    24,
1059       25,    26,    27,    28,    29,    30,     0,    31,    32,    33,
1060       34,    35,     0,    36,     0,    37,    38,    39,     0,    40,
1061       41,    42,     0,     0,    43,     0,    44,    45,     0,    46,
1062        0,    47,    48,     0,    49,    50,    51,     0,    52,    53,
1063       54,    55,    56,     0,    57,     0,    58,    59,     0,    60,
1064       61,    62,    63,    64,    65,    66,    67,    68,    69,    70,
1065       71,    72,    73,    74,    75,    76,    77,    78,    79,    80,
1066       81,    82,    83,    84,    85,     0,    86,    87,     0,     0,
1067       88,    89,    90,     0,    91,    92,   120,   121,   122,   123,
1068        0,     0,     0,     0,     0,   124,     0,  -163,     0,   125,
1069      126,   127,     0,     0,     0,   128,     0,     0,     0,   129,
1070        0,     0,     0,     0,  -163,     0,     0,     0,   130,     0,
1071     -104,  -104,  -104,     0,     0,     0,   132,   133,   134,     0,
1072        0,     0,     0,     0,   135,     0,   136,   137,     0,     0,
1073      138,     0,     0,     0,     0,     0,     0,     0,   139,     0,
1074        0,     0,     0,   140,   141,     0,     0,   142,     0,     0,
1075        0,     0,     0,     0,     0,     0,   143,     0,   144,     0,
1076      145,     0,     0,   146,   120,   121,   122,   123,     0,   147,
1077        0,     0,     0,   124,     0,  -163,     0,   125,   126,   127,
1078        0,     0,     0,   128,     0,     0,     0,   129,     0,     0,
1079        0,     0,  -163,     0,     0,     0,   130,     0,  -105,  -105,
1080     -105,     0,     0,     0,   132,   133,   134,     0,     0,     0,
1081        0,     0,   135,     0,   136,   137,     0,     0,   138,     0,
1082        0,     0,     0,     0,     0,     0,   139,     0,     0,     0,
1083        0,   140,   141,     0,     0,   142,     0,     0,     0,     0,
1084        0,     0,     0,     0,   143,     0,   144,     0,   145,     0,
1085        0,   146,   120,   121,   122,   123,     0,   147,     0,     0,
1086        0,   124,     0,  -163,     0,   125,   126,   127,     0,     0,
1087        0,   128,     0,     0,     0,   129,     0,     0,     0,     0,
1088     -163,     0,     0,     0,   130,     0,     0,     0,  -150,     0,
1089        0,   131,   132,   133,   134,     0,     0,     0,     0,     0,
1090      135,     0,   136,   137,     0,     0,   138,     0,     0,     0,
1091        0,     0,     0,     0,   139,     0,     0,     0,     0,   140,
1092      141,     0,     0,   142,     0,     0,     0,     0,     0,     0,
1093        0,     0,   143,     0,   144,     0,   145,     0,     0,   146,
1094      120,   121,   122,   123,     0,   147,     0,     0,     0,   124,
1095        0,  -163,     0,   125,   126,   127,     0,     0,     0,   128,
1096        0,     0,     0,   129,   120,   121,   122,   123,  -163,     0,
1097        0,     0,   130,   124,     0,  -163,   277,   125,   126,   127,
1098      132,   133,   134,   128,     0,     0,     0,   129,   135,     0,
1099      136,   137,  -163,     0,   138,     0,   130,     0,     0,     0,
1100     -114,     0,   139,     0,   132,   133,   134,   140,   141,     0,
1101        0,   142,   135,     0,   136,   137,     0,     0,   138,     0,
1102      143,     0,   144,     0,   145,     0,   139,   146,     0,     0,
1103        0,   140,   141,   147,     0,   142,     0,     0,     0,     0,
1104        0,     0,     0,     0,   143,     0,   144,     0,   145,     0,
1105        0,   146,   120,   121,   122,   123,     0,   147,     0,     0,
1106        0,   124,     0,  -163,     0,   125,   126,   127,     0,     0,
1107        0,   128,     0,     0,     0,   129,     0,     0,     0,     0,
1108     -163,     0,     0,     0,   130,     0,     0,     0,  -107,     0,
1109        0,     0,   132,   133,   134,     0,     0,     0,     0,     0,
1110      135,     0,   136,   137,     0,     0,   138,     0,     0,     0,
1111        0,     0,     0,     0,   139,     0,     0,     0,     0,   140,
1112      141,     0,     0,   142,     0,     0,     0,     0,     0,     0,
1113        0,     0,   143,     0,   144,     0,   145,     0,     0,   146,
1114        0,     0,     0,     0,     0,   147
1115 };
1116 
1117 static const yytype_int16 yycheck[] =
1118 {
1119        4,    96,   211,    13,   100,    13,    13,   102,    21,    42,
1120       52,   222,    18,    19,    20,    21,   208,    62,    63,   242,
1121      115,    27,    56,    29,   247,    31,    32,    33,    24,   280,
1122      230,    37,    77,    87,    29,    41,     0,   141,    88,    47,
1123       46,    95,    38,   235,    50,    62,    52,    43,    54,   268,
1124      129,    46,    58,    59,    60,   126,   129,    74,   129,   132,
1125       66,    29,    68,    69,    52,    53,    72,   286,   118,    46,
1126      124,   131,   132,   324,    80,    18,    19,    20,    22,    85,
1127       86,   114,   127,    89,   126,   296,    82,   121,   122,    51,
1128      194,   300,    98,   293,   100,   199,   102,   129,   129,   105,
1129      129,   129,   198,    54,    22,   111,    62,   330,   110,   113,
1130       18,    19,    20,    43,   110,   128,    18,    19,    20,   123,
1131      126,   129,   217,   133,   134,   133,   133,    22,   126,   129,
1132       49,    34,   126,    73,    70,    83,   129,   112,   129,    54,
1133      130,     9,   134,    70,    77,    32,    85,    54,    26,   129,
1134      129,    68,     2,   129,   115,   324,   303,   293,   253,   255,
1135      256,   223,   213,   227,    18,    19,    20,    21,   139,   290,
1136      321,    -1,    -1,    27,    -1,    29,    -1,    31,    32,    33,
1137       -1,    -1,    -1,    37,    -1,   280,    -1,    41,    -1,    -1,
1138       -1,    -1,    46,    -1,    -1,   291,    50,    -1,    -1,   295,
1139       54,    -1,    -1,    -1,    58,    59,    60,    -1,    -1,    -1,
1140       -1,    -1,    66,    -1,    68,    69,    -1,    -1,    72,    -1,
1141       -1,    -1,   226,   319,    -1,    -1,    80,    -1,    -1,   324,
1142       -1,    85,    86,    -1,    -1,    89,    -1,    -1,    -1,    -1,
1143       -1,    -1,    -1,    -1,    98,    -1,   100,    -1,   102,    -1,
1144       -1,   105,    -1,    -1,    -1,    -1,    -1,   111,    -1,    -1,
1145       -1,    -1,    -1,    -1,    -1,    -1,    -1,    -1,    -1,    -1,
1146       -1,    -1,   126,    -1,    18,    -1,    -1,    21,    -1,    23,
1147       24,    -1,    26,    27,    28,    -1,   290,    31,    -1,    33,
1148       34,    35,    36,    37,    38,    39,    40,    41,    42,    43,
1149       44,    45,    46,    47,    48,    49,    50,    51,    -1,    53,
1150       -1,    55,    56,    57,    -1,    59,    60,    61,    -1,    -1,
1151       64,    -1,    66,    67,    -1,    69,    -1,    71,    72,    -1,
1152       74,    75,    76,    -1,    78,    79,    80,    81,    82,    -1,
1153       84,    -1,    86,    87,    -1,    89,    90,    91,    92,    93,
1154       94,    95,    96,    97,    98,    99,   100,   101,   102,   103,
1155      104,   105,   106,   107,   108,   109,   110,   111,   112,   113,
1156      114,    -1,   116,   117,    -1,    -1,   120,   121,   122,    -1,
1157      124,   125,    18,    -1,    20,    -1,    -1,    23,    24,    -1,
1158       26,    27,    28,    -1,    -1,    31,    -1,    33,    34,    35,
1159       36,    37,    38,    39,    40,    41,    42,    43,    44,    45,
1160       -1,    47,    48,    49,    50,    51,    -1,    53,    -1,    55,
1161       56,    57,    -1,    59,    60,    61,    -1,    -1,    64,    -1,
1162       66,    67,    -1,    69,    -1,    71,    72,    -1,    74,    75,
1163       76,    -1,    78,    79,    80,    81,    82,    -1,    84,    -1,
1164       86,    87,    -1,    89,    90,    91,    92,    93,    94,    95,
1165       96,    97,    98,    99,   100,   101,   102,   103,   104,   105,
1166      106,   107,   108,   109,   110,   111,   112,   113,   114,    -1,
1167      116,   117,    -1,    -1,   120,   121,   122,    -1,   124,   125,
1168       18,    19,    -1,    -1,    -1,    23,    24,    -1,    26,    27,
1169       28,    -1,    -1,    31,    -1,    33,    34,    35,    36,    37,
1170       38,    39,    40,    41,    42,    43,    44,    45,    -1,    47,
1171       48,    49,    50,    51,    -1,    53,    -1,    55,    56,    57,
1172       -1,    59,    60,    61,    -1,    -1,    64,    -1,    66,    67,
1173       -1,    69,    -1,    71,    72,    -1,    74,    75,    76,    -1,
1174       78,    79,    80,    81,    82,    -1,    84,    -1,    86,    87,
1175       -1,    89,    90,    91,    92,    93,    94,    95,    96,    97,
1176       98,    99,   100,   101,   102,   103,   104,   105,   106,   107,
1177      108,   109,   110,   111,   112,   113,   114,    -1,   116,   117,
1178       -1,    -1,   120,   121,   122,    -1,   124,   125,    18,    19,
1179       -1,    -1,    -1,    23,    24,    -1,    26,    27,    28,    -1,
1180       -1,    31,    -1,    33,    34,    35,    36,    37,    38,    39,
1181       40,    41,    42,    43,    44,    45,    -1,    47,    48,    49,
1182       50,    51,    -1,    53,    -1,    55,    56,    57,    -1,    59,
1183       60,    61,    -1,    -1,    64,    -1,    66,    67,    -1,    69,
1184       -1,    71,    72,    -1,    74,    75,    76,    -1,    78,    79,
1185       80,    81,    82,    -1,    84,    -1,    86,    87,    -1,    89,
1186       90,    91,    92,    93,    94,    95,    96,    97,    98,    99,
1187      100,   101,   102,   103,   104,   105,   106,   107,   108,   109,
1188      110,   111,   112,   113,   114,    -1,   116,   117,    -1,    -1,
1189      120,   121,   122,    18,   124,   125,    -1,    -1,    23,    24,
1190       -1,    26,    27,    28,    -1,    -1,    31,    -1,    33,    34,
1191       35,    36,    37,    38,    39,    40,    41,    42,    43,    44,
1192       45,    -1,    47,    48,    49,    50,    51,    -1,    53,    -1,
1193       55,    56,    57,    -1,    59,    60,    61,    -1,    -1,    64,
1194       -1,    66,    67,    -1,    69,    -1,    71,    72,    -1,    74,
1195       75,    76,    -1,    78,    79,    80,    81,    82,    -1,    84,
1196       -1,    86,    87,    -1,    89,    90,    91,    92,    93,    94,
1197       95,    96,    97,    98,    99,   100,   101,   102,   103,   104,
1198      105,   106,   107,   108,   109,   110,   111,   112,   113,   114,
1199       -1,   116,   117,    -1,    -1,   120,   121,   122,    18,   124,
1200      125,    -1,    -1,    23,    24,    -1,    26,    27,    28,    -1,
1201       -1,    31,    -1,    33,    34,    35,    36,    37,    38,    39,
1202       40,    41,    42,    43,    44,    45,    -1,    47,    48,    49,
1203       50,    51,    -1,    53,    -1,    55,    56,    57,    -1,    59,
1204       60,    61,    -1,    -1,    64,    -1,    66,    67,    -1,    69,
1205       -1,    71,    72,    -1,    74,    75,    76,    -1,    78,    79,
1206       80,    81,    82,    -1,    84,    -1,    86,    87,    -1,    89,
1207       90,    91,    92,    93,    94,    95,    96,    97,    98,    99,
1208      100,   101,   102,   103,   104,   105,   106,   107,   108,   109,
1209      110,   111,   112,   113,   114,    -1,   116,   117,    -1,    -1,
1210      120,   121,   122,    -1,   124,   125,    18,    19,    20,    21,
1211       -1,    -1,    -1,    -1,    -1,    27,    -1,    29,    -1,    31,
1212       32,    33,    -1,    -1,    -1,    37,    -1,    -1,    -1,    41,
1213       -1,    -1,    -1,    -1,    46,    -1,    -1,    -1,    50,    -1,
1214       52,    53,    54,    -1,    -1,    -1,    58,    59,    60,    -1,
1215       -1,    -1,    -1,    -1,    66,    -1,    68,    69,    -1,    -1,
1216       72,    -1,    -1,    -1,    -1,    -1,    -1,    -1,    80,    -1,
1217       -1,    -1,    -1,    85,    86,    -1,    -1,    89,    -1,    -1,
1218       -1,    -1,    -1,    -1,    -1,    -1,    98,    -1,   100,    -1,
1219      102,    -1,    -1,   105,    18,    19,    20,    21,    -1,   111,
1220       -1,    -1,    -1,    27,    -1,    29,    -1,    31,    32,    33,
1221       -1,    -1,    -1,    37,    -1,    -1,    -1,    41,    -1,    -1,
1222       -1,    -1,    46,    -1,    -1,    -1,    50,    -1,    52,    53,
1223       54,    -1,    -1,    -1,    58,    59,    60,    -1,    -1,    -1,
1224       -1,    -1,    66,    -1,    68,    69,    -1,    -1,    72,    -1,
1225       -1,    -1,    -1,    -1,    -1,    -1,    80,    -1,    -1,    -1,
1226       -1,    85,    86,    -1,    -1,    89,    -1,    -1,    -1,    -1,
1227       -1,    -1,    -1,    -1,    98,    -1,   100,    -1,   102,    -1,
1228       -1,   105,    18,    19,    20,    21,    -1,   111,    -1,    -1,
1229       -1,    27,    -1,    29,    -1,    31,    32,    33,    -1,    -1,
1230       -1,    37,    -1,    -1,    -1,    41,    -1,    -1,    -1,    -1,
1231       46,    -1,    -1,    -1,    50,    -1,    -1,    -1,    54,    -1,
1232       -1,    57,    58,    59,    60,    -1,    -1,    -1,    -1,    -1,
1233       66,    -1,    68,    69,    -1,    -1,    72,    -1,    -1,    -1,
1234       -1,    -1,    -1,    -1,    80,    -1,    -1,    -1,    -1,    85,
1235       86,    -1,    -1,    89,    -1,    -1,    -1,    -1,    -1,    -1,
1236       -1,    -1,    98,    -1,   100,    -1,   102,    -1,    -1,   105,
1237       18,    19,    20,    21,    -1,   111,    -1,    -1,    -1,    27,
1238       -1,    29,    -1,    31,    32,    33,    -1,    -1,    -1,    37,
1239       -1,    -1,    -1,    41,    18,    19,    20,    21,    46,    -1,
1240       -1,    -1,    50,    27,    -1,    29,    54,    31,    32,    33,
1241       58,    59,    60,    37,    -1,    -1,    -1,    41,    66,    -1,
1242       68,    69,    46,    -1,    72,    -1,    50,    -1,    -1,    -1,
1243       54,    -1,    80,    -1,    58,    59,    60,    85,    86,    -1,
1244       -1,    89,    66,    -1,    68,    69,    -1,    -1,    72,    -1,
1245       98,    -1,   100,    -1,   102,    -1,    80,   105,    -1,    -1,
1246       -1,    85,    86,   111,    -1,    89,    -1,    -1,    -1,    -1,
1247       -1,    -1,    -1,    -1,    98,    -1,   100,    -1,   102,    -1,
1248       -1,   105,    18,    19,    20,    21,    -1,   111,    -1,    -1,
1249       -1,    27,    -1,    29,    -1,    31,    32,    33,    -1,    -1,
1250       -1,    37,    -1,    -1,    -1,    41,    -1,    -1,    -1,    -1,
1251       46,    -1,    -1,    -1,    50,    -1,    -1,    -1,    54,    -1,
1252       -1,    -1,    58,    59,    60,    -1,    -1,    -1,    -1,    -1,
1253       66,    -1,    68,    69,    -1,    -1,    72,    -1,    -1,    -1,
1254       -1,    -1,    -1,    -1,    80,    -1,    -1,    -1,    -1,    85,
1255       86,    -1,    -1,    89,    -1,    -1,    -1,    -1,    -1,    -1,
1256       -1,    -1,    98,    -1,   100,    -1,   102,    -1,    -1,   105,
1257       -1,    -1,    -1,    -1,    -1,   111
1258 };
1259 
1260   /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
1261      symbol of state STATE-NUM.  */
1262 static const yytype_uint8 yystos[] =
1263 {
1264        0,   136,   137,     0,    21,   128,   138,   141,   142,   218,
1265       18,    20,    23,    24,    26,    27,    28,    31,    33,    34,
1266       35,    36,    37,    38,    39,    40,    41,    42,    43,    44,
1267       45,    47,    48,    49,    50,    51,    53,    55,    56,    57,
1268       59,    60,    61,    64,    66,    67,    69,    71,    72,    74,
1269       75,    76,    78,    79,    80,    81,    82,    84,    86,    87,
1270       89,    90,    91,    92,    93,    94,    95,    96,    97,    98,
1271       99,   100,   101,   102,   103,   104,   105,   106,   107,   108,
1272      109,   110,   111,   112,   113,   114,   116,   117,   120,   121,
1273      122,   124,   125,   222,   223,    87,    95,   124,   129,   140,
1274       29,    46,   143,    22,    51,    18,   139,   223,    56,   121,
1275      122,   163,    18,    21,    46,   144,   145,   146,   155,   223,
1276       18,    19,    20,    21,    27,    31,    32,    33,    37,    41,
1277       50,    57,    58,    59,    60,    66,    68,    69,    72,    80,
1278       85,    86,    89,    98,   100,   102,   105,   111,   141,   164,
1279      165,   166,   167,   168,   174,   175,   178,   183,   184,   185,
1280      188,   190,   191,   192,   193,   194,   196,   197,   198,   199,
1281      200,   202,   203,   204,   205,   206,   208,   219,   222,   145,
1282       24,    38,    82,   110,   148,   156,   222,   179,    18,    19,
1283       20,   207,   129,   209,   201,    42,   114,   169,   216,   201,
1284      129,   207,   214,   129,   129,    13,   133,   134,   162,   220,
1285      222,    54,    62,    63,    77,   127,    22,    62,   110,    43,
1286      157,    22,   126,   180,   181,   129,   126,   210,   211,   207,
1287       49,   163,   207,   215,   214,   126,   129,   221,   220,    18,
1288       19,    20,   186,   187,   187,   163,   195,   217,    18,    19,
1289      154,   223,   147,    34,   158,   216,    52,   181,   182,   212,
1290      213,   222,   211,    73,    18,    19,   170,   171,   173,   174,
1291      176,   129,   214,   195,    70,   112,   189,    54,   195,   129,
1292      130,   150,    18,    19,   223,    83,   159,   163,   163,    54,
1293       88,   118,   129,   132,   162,    52,    53,   177,     9,    70,
1294       77,   151,   152,   155,    62,    74,   153,    85,    47,   129,
1295      160,   161,   162,    32,   213,   163,   171,   172,   163,   216,
1296       54,    26,   220,   131,   132,   157,   149,   129,   163,    68,
1297      217,   129,   152,   129,   195
1298 };
1299 
1300   /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
1301 static const yytype_uint8 yyr1[] =
1302 {
1303        0,   135,   136,   137,   137,   138,   138,   138,   138,   138,
1304      139,   139,   140,   140,   141,   142,   142,   142,   143,   144,
1305      144,   145,   145,   145,   146,   146,   147,   146,   148,   148,
1306      148,   149,   150,   150,   151,   151,   152,   153,   153,   154,
1307      154,   154,   155,   155,   156,   156,   157,   158,   158,   158,
1308      158,   159,   159,   160,   160,   161,   161,   162,   162,   163,
1309      163,   164,   164,   164,   164,   164,   164,   164,   164,   164,
1310      164,   164,   164,   164,   164,   164,   164,   164,   164,   164,
1311      164,   164,   164,   164,   164,   164,   165,   166,   166,   167,
1312      168,   169,   169,   169,   170,   170,   171,   172,   173,   173,
1313      173,   174,   174,   175,   176,   176,   177,   177,   178,   179,
1314      180,   180,   181,   182,   182,   183,   184,   185,   186,   187,
1315      187,   187,   188,   189,   189,   190,   191,   191,   192,   193,
1316      194,   195,   196,   196,   196,   196,   197,   198,   199,   200,
1317      201,   202,   203,   204,   205,   206,   206,   207,   207,   207,
1318      208,   209,   208,   210,   210,   211,   212,   212,   213,   214,
1319      215,   216,   217,   218,   218,   219,   219,   220,   220,   221,
1320      221,   222,   222,   222,   223,   223,   223,   223,   223,   223,
1321      223,   223,   223,   223,   223,   223,   223,   223,   223,   223,
1322      223,   223,   223,   223,   223,   223,   223,   223,   223,   223,
1323      223,   223,   223,   223,   223,   223,   223,   223,   223,   223,
1324      223,   223,   223,   223,   223,   223,   223,   223,   223,   223,
1325      223,   223,   223,   223,   223,   223,   223,   223,   223,   223,
1326      223,   223,   223,   223,   223,   223,   223,   223,   223,   223,
1327      223,   223,   223,   223,   223,   223,   223,   223,   223,   223,
1328      223,   223,   223,   223,   223
1329 };
1330 
1331   /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.  */
1332 static const yytype_uint8 yyr2[] =
1333 {
1334        0,     2,     3,     0,     2,     3,     3,     3,     3,     3,
1335        1,     1,     0,     1,     6,     1,     2,     3,     1,     2,
1336        1,     1,     1,     3,     6,     5,     0,     7,     0,     2,
1337        1,     0,     0,     3,     1,     3,     2,     1,     1,     1,
1338        1,     1,     1,     1,     0,     1,     0,     0,     2,     2,
1339        2,     0,     2,     1,     1,     1,     1,     1,     1,     0,
1340        2,     2,     1,     1,     1,     1,     1,     1,     1,     1,
1341        1,     1,     1,     1,     1,     1,     1,     1,     1,     1,
1342        1,     1,     1,     1,     1,     1,     2,     1,     1,     3,
1343        5,     0,     1,     1,     3,     1,     3,     0,     1,     1,
1344        1,     1,     3,     8,     0,     4,     0,     2,     7,     0,
1345        2,     1,     3,     0,     2,     3,     4,     4,     2,     1,
1346        1,     1,     8,     0,     2,     3,     1,     1,     1,     1,
1347        1,     5,     1,     1,     1,     1,     1,     2,     4,     4,
1348        0,     3,     2,     2,     2,     1,     1,     1,     1,     1,
1349        0,     0,     3,     2,     1,     4,     3,     1,     1,     0,
1350        0,     0,     0,     0,     3,     0,     3,     0,     1,     1,
1351        2,     1,     1,     1,     1,     1,     1,     1,     1,     1,
1352        1,     1,     1,     1,     1,     1,     1,     1,     1,     1,
1353        1,     1,     1,     1,     1,     1,     1,     1,     1,     1,
1354        1,     1,     1,     1,     1,     1,     1,     1,     1,     1,
1355        1,     1,     1,     1,     1,     1,     1,     1,     1,     1,
1356        1,     1,     1,     1,     1,     1,     1,     1,     1,     1,
1357        1,     1,     1,     1,     1,     1,     1,     1,     1,     1,
1358        1,     1,     1,     1,     1,     1,     1,     1,     1,     1,
1359        1,     1,     1,     1,     1
1360 };
1361 
1362 
1363 #define yyerrok         (yyerrstatus = 0)
1364 #define yyclearin       (yychar = YYEMPTY)
1365 #define YYEMPTY         (-2)
1366 #define YYEOF           0
1367 
1368 #define YYACCEPT        goto yyacceptlab
1369 #define YYABORT         goto yyabortlab
1370 #define YYERROR         goto yyerrorlab
1371 
1372 
1373 #define YYRECOVERING()  (!!yyerrstatus)
1374 
1375 #define YYBACKUP(Token, Value)                                    \
1376   do                                                              \
1377     if (yychar == YYEMPTY)                                        \
1378       {                                                           \
1379         yychar = (Token);                                         \
1380         yylval = (Value);                                         \
1381         YYPOPSTACK (yylen);                                       \
1382         yystate = *yyssp;                                         \
1383         goto yybackup;                                            \
1384       }                                                           \
1385     else                                                          \
1386       {                                                           \
1387         yyerror (YY_("syntax error: cannot back up")); \
1388         YYERROR;                                                  \
1389       }                                                           \
1390   while (0)
1391 
1392 /* Error token number */
1393 #define YYTERROR        1
1394 #define YYERRCODE       256
1395 
1396 
1397 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
1398    If N is 0, then set CURRENT to the empty location which ends
1399    the previous symbol: RHS[0] (always defined).  */
1400 
1401 #ifndef YYLLOC_DEFAULT
1402 # define YYLLOC_DEFAULT(Current, Rhs, N)                                \
1403     do                                                                  \
1404       if (N)                                                            \
1405         {                                                               \
1406           (Current).first_line   = YYRHSLOC (Rhs, 1).first_line;        \
1407           (Current).first_column = YYRHSLOC (Rhs, 1).first_column;      \
1408           (Current).last_line    = YYRHSLOC (Rhs, N).last_line;         \
1409           (Current).last_column  = YYRHSLOC (Rhs, N).last_column;       \
1410         }                                                               \
1411       else                                                              \
1412         {                                                               \
1413           (Current).first_line   = (Current).last_line   =              \
1414             YYRHSLOC (Rhs, 0).last_line;                                \
1415           (Current).first_column = (Current).last_column =              \
1416             YYRHSLOC (Rhs, 0).last_column;                              \
1417         }                                                               \
1418     while (0)
1419 #endif
1420 
1421 #define YYRHSLOC(Rhs, K) ((Rhs)[K])
1422 
1423 
1424 /* Enable debugging if requested.  */
1425 #if YYDEBUG
1426 
1427 # ifndef YYFPRINTF
1428 #  include <stdio.h> /* INFRINGES ON USER NAME SPACE */
1429 #  define YYFPRINTF fprintf
1430 # endif
1431 
1432 # define YYDPRINTF(Args)                        \
1433 do {                                            \
1434   if (yydebug)                                  \
1435     YYFPRINTF Args;                             \
1436 } while (0)
1437 
1438 
1439 /* YY_LOCATION_PRINT -- Print the location on the stream.
1440    This macro was not mandated originally: define only if we know
1441    we won't break user code: when these are the locations we know.  */
1442 
1443 #ifndef YY_LOCATION_PRINT
1444 # if defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL
1445 
1446 /* Print *YYLOCP on YYO.  Private, do not rely on its existence. */
1447 
1448 YY_ATTRIBUTE_UNUSED
1449 static int
yy_location_print_(FILE * yyo,YYLTYPE const * const yylocp)1450 yy_location_print_ (FILE *yyo, YYLTYPE const * const yylocp)
1451 {
1452   int res = 0;
1453   int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
1454   if (0 <= yylocp->first_line)
1455     {
1456       res += YYFPRINTF (yyo, "%d", yylocp->first_line);
1457       if (0 <= yylocp->first_column)
1458         res += YYFPRINTF (yyo, ".%d", yylocp->first_column);
1459     }
1460   if (0 <= yylocp->last_line)
1461     {
1462       if (yylocp->first_line < yylocp->last_line)
1463         {
1464           res += YYFPRINTF (yyo, "-%d", yylocp->last_line);
1465           if (0 <= end_col)
1466             res += YYFPRINTF (yyo, ".%d", end_col);
1467         }
1468       else if (0 <= end_col && yylocp->first_column < end_col)
1469         res += YYFPRINTF (yyo, "-%d", end_col);
1470     }
1471   return res;
1472  }
1473 
1474 #  define YY_LOCATION_PRINT(File, Loc)          \
1475   yy_location_print_ (File, &(Loc))
1476 
1477 # else
1478 #  define YY_LOCATION_PRINT(File, Loc) ((void) 0)
1479 # endif
1480 #endif
1481 
1482 
1483 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)                    \
1484 do {                                                                      \
1485   if (yydebug)                                                            \
1486     {                                                                     \
1487       YYFPRINTF (stderr, "%s ", Title);                                   \
1488       yy_symbol_print (stderr,                                            \
1489                   Type, Value, Location); \
1490       YYFPRINTF (stderr, "\n");                                           \
1491     }                                                                     \
1492 } while (0)
1493 
1494 
1495 /*-----------------------------------.
1496 | Print this symbol's value on YYO.  |
1497 `-----------------------------------*/
1498 
1499 static void
yy_symbol_value_print(FILE * yyo,int yytype,YYSTYPE const * const yyvaluep,YYLTYPE const * const yylocationp)1500 yy_symbol_value_print (FILE *yyo, int yytype, YYSTYPE const * const yyvaluep, YYLTYPE const * const yylocationp)
1501 {
1502   FILE *yyoutput = yyo;
1503   YYUSE (yyoutput);
1504   YYUSE (yylocationp);
1505   if (!yyvaluep)
1506     return;
1507 # ifdef YYPRINT
1508   if (yytype < YYNTOKENS)
1509     YYPRINT (yyo, yytoknum[yytype], *yyvaluep);
1510 # endif
1511   YYUSE (yytype);
1512 }
1513 
1514 
1515 /*---------------------------.
1516 | Print this symbol on YYO.  |
1517 `---------------------------*/
1518 
1519 static void
yy_symbol_print(FILE * yyo,int yytype,YYSTYPE const * const yyvaluep,YYLTYPE const * const yylocationp)1520 yy_symbol_print (FILE *yyo, int yytype, YYSTYPE const * const yyvaluep, YYLTYPE const * const yylocationp)
1521 {
1522   YYFPRINTF (yyo, "%s %s (",
1523              yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]);
1524 
1525   YY_LOCATION_PRINT (yyo, *yylocationp);
1526   YYFPRINTF (yyo, ": ");
1527   yy_symbol_value_print (yyo, yytype, yyvaluep, yylocationp);
1528   YYFPRINTF (yyo, ")");
1529 }
1530 
1531 /*------------------------------------------------------------------.
1532 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
1533 | TOP (included).                                                   |
1534 `------------------------------------------------------------------*/
1535 
1536 static void
yy_stack_print(yytype_int16 * yybottom,yytype_int16 * yytop)1537 yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop)
1538 {
1539   YYFPRINTF (stderr, "Stack now");
1540   for (; yybottom <= yytop; yybottom++)
1541     {
1542       int yybot = *yybottom;
1543       YYFPRINTF (stderr, " %d", yybot);
1544     }
1545   YYFPRINTF (stderr, "\n");
1546 }
1547 
1548 # define YY_STACK_PRINT(Bottom, Top)                            \
1549 do {                                                            \
1550   if (yydebug)                                                  \
1551     yy_stack_print ((Bottom), (Top));                           \
1552 } while (0)
1553 
1554 
1555 /*------------------------------------------------.
1556 | Report that the YYRULE is going to be reduced.  |
1557 `------------------------------------------------*/
1558 
1559 static void
yy_reduce_print(yytype_int16 * yyssp,YYSTYPE * yyvsp,YYLTYPE * yylsp,int yyrule)1560 yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, YYLTYPE *yylsp, int yyrule)
1561 {
1562   unsigned long yylno = yyrline[yyrule];
1563   int yynrhs = yyr2[yyrule];
1564   int yyi;
1565   YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
1566              yyrule - 1, yylno);
1567   /* The symbols being reduced.  */
1568   for (yyi = 0; yyi < yynrhs; yyi++)
1569     {
1570       YYFPRINTF (stderr, "   $%d = ", yyi + 1);
1571       yy_symbol_print (stderr,
1572                        yystos[yyssp[yyi + 1 - yynrhs]],
1573                        &yyvsp[(yyi + 1) - (yynrhs)]
1574                        , &(yylsp[(yyi + 1) - (yynrhs)])                       );
1575       YYFPRINTF (stderr, "\n");
1576     }
1577 }
1578 
1579 # define YY_REDUCE_PRINT(Rule)          \
1580 do {                                    \
1581   if (yydebug)                          \
1582     yy_reduce_print (yyssp, yyvsp, yylsp, Rule); \
1583 } while (0)
1584 
1585 /* Nonzero means print parse trace.  It is left uninitialized so that
1586    multiple parsers can coexist.  */
1587 int yydebug;
1588 #else /* !YYDEBUG */
1589 # define YYDPRINTF(Args)
1590 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
1591 # define YY_STACK_PRINT(Bottom, Top)
1592 # define YY_REDUCE_PRINT(Rule)
1593 #endif /* !YYDEBUG */
1594 
1595 
1596 /* YYINITDEPTH -- initial size of the parser's stacks.  */
1597 #ifndef YYINITDEPTH
1598 # define YYINITDEPTH 200
1599 #endif
1600 
1601 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
1602    if the built-in stack extension method is used).
1603 
1604    Do not make this value too large; the results are undefined if
1605    YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
1606    evaluated with infinite-precision integer arithmetic.  */
1607 
1608 #ifndef YYMAXDEPTH
1609 # define YYMAXDEPTH 10000
1610 #endif
1611 
1612 
1613 #if YYERROR_VERBOSE
1614 
1615 # ifndef yystrlen
1616 #  if defined __GLIBC__ && defined _STRING_H
1617 #   define yystrlen strlen
1618 #  else
1619 /* Return the length of YYSTR.  */
1620 static YYSIZE_T
yystrlen(const char * yystr)1621 yystrlen (const char *yystr)
1622 {
1623   YYSIZE_T yylen;
1624   for (yylen = 0; yystr[yylen]; yylen++)
1625     continue;
1626   return yylen;
1627 }
1628 #  endif
1629 # endif
1630 
1631 # ifndef yystpcpy
1632 #  if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
1633 #   define yystpcpy stpcpy
1634 #  else
1635 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
1636    YYDEST.  */
1637 static char *
yystpcpy(char * yydest,const char * yysrc)1638 yystpcpy (char *yydest, const char *yysrc)
1639 {
1640   char *yyd = yydest;
1641   const char *yys = yysrc;
1642 
1643   while ((*yyd++ = *yys++) != '\0')
1644     continue;
1645 
1646   return yyd - 1;
1647 }
1648 #  endif
1649 # endif
1650 
1651 # ifndef yytnamerr
1652 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1653    quotes and backslashes, so that it's suitable for yyerror.  The
1654    heuristic is that double-quoting is unnecessary unless the string
1655    contains an apostrophe, a comma, or backslash (other than
1656    backslash-backslash).  YYSTR is taken from yytname.  If YYRES is
1657    null, do not copy; instead, return the length of what the result
1658    would have been.  */
1659 static YYSIZE_T
yytnamerr(char * yyres,const char * yystr)1660 yytnamerr (char *yyres, const char *yystr)
1661 {
1662   if (*yystr == '"')
1663     {
1664       YYSIZE_T yyn = 0;
1665       char const *yyp = yystr;
1666 
1667       for (;;)
1668         switch (*++yyp)
1669           {
1670           case '\'':
1671           case ',':
1672             goto do_not_strip_quotes;
1673 
1674           case '\\':
1675             if (*++yyp != '\\')
1676               goto do_not_strip_quotes;
1677             else
1678               goto append;
1679 
1680           append:
1681           default:
1682             if (yyres)
1683               yyres[yyn] = *yyp;
1684             yyn++;
1685             break;
1686 
1687           case '"':
1688             if (yyres)
1689               yyres[yyn] = '\0';
1690             return yyn;
1691           }
1692     do_not_strip_quotes: ;
1693     }
1694 
1695   if (! yyres)
1696     return yystrlen (yystr);
1697 
1698   return (YYSIZE_T) (yystpcpy (yyres, yystr) - yyres);
1699 }
1700 # endif
1701 
1702 /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message
1703    about the unexpected token YYTOKEN for the state stack whose top is
1704    YYSSP.
1705 
1706    Return 0 if *YYMSG was successfully written.  Return 1 if *YYMSG is
1707    not large enough to hold the message.  In that case, also set
1708    *YYMSG_ALLOC to the required number of bytes.  Return 2 if the
1709    required number of bytes is too large to store.  */
1710 static int
yysyntax_error(YYSIZE_T * yymsg_alloc,char ** yymsg,yytype_int16 * yyssp,int yytoken)1711 yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
1712                 yytype_int16 *yyssp, int yytoken)
1713 {
1714   YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]);
1715   YYSIZE_T yysize = yysize0;
1716   enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1717   /* Internationalized format string. */
1718   const char *yyformat = YY_NULLPTR;
1719   /* Arguments of yyformat. */
1720   char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1721   /* Number of reported tokens (one for the "unexpected", one per
1722      "expected"). */
1723   int yycount = 0;
1724 
1725   /* There are many possibilities here to consider:
1726      - If this state is a consistent state with a default action, then
1727        the only way this function was invoked is if the default action
1728        is an error action.  In that case, don't check for expected
1729        tokens because there are none.
1730      - The only way there can be no lookahead present (in yychar) is if
1731        this state is a consistent state with a default action.  Thus,
1732        detecting the absence of a lookahead is sufficient to determine
1733        that there is no unexpected or expected token to report.  In that
1734        case, just report a simple "syntax error".
1735      - Don't assume there isn't a lookahead just because this state is a
1736        consistent state with a default action.  There might have been a
1737        previous inconsistent state, consistent state with a non-default
1738        action, or user semantic action that manipulated yychar.
1739      - Of course, the expected token list depends on states to have
1740        correct lookahead information, and it depends on the parser not
1741        to perform extra reductions after fetching a lookahead from the
1742        scanner and before detecting a syntax error.  Thus, state merging
1743        (from LALR or IELR) and default reductions corrupt the expected
1744        token list.  However, the list is correct for canonical LR with
1745        one exception: it will still contain any token that will not be
1746        accepted due to an error action in a later state.
1747   */
1748   if (yytoken != YYEMPTY)
1749     {
1750       int yyn = yypact[*yyssp];
1751       yyarg[yycount++] = yytname[yytoken];
1752       if (!yypact_value_is_default (yyn))
1753         {
1754           /* Start YYX at -YYN if negative to avoid negative indexes in
1755              YYCHECK.  In other words, skip the first -YYN actions for
1756              this state because they are default actions.  */
1757           int yyxbegin = yyn < 0 ? -yyn : 0;
1758           /* Stay within bounds of both yycheck and yytname.  */
1759           int yychecklim = YYLAST - yyn + 1;
1760           int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
1761           int yyx;
1762 
1763           for (yyx = yyxbegin; yyx < yyxend; ++yyx)
1764             if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR
1765                 && !yytable_value_is_error (yytable[yyx + yyn]))
1766               {
1767                 if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1768                   {
1769                     yycount = 1;
1770                     yysize = yysize0;
1771                     break;
1772                   }
1773                 yyarg[yycount++] = yytname[yyx];
1774                 {
1775                   YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]);
1776                   if (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM)
1777                     yysize = yysize1;
1778                   else
1779                     return 2;
1780                 }
1781               }
1782         }
1783     }
1784 
1785   switch (yycount)
1786     {
1787 # define YYCASE_(N, S)                      \
1788       case N:                               \
1789         yyformat = S;                       \
1790       break
1791     default: /* Avoid compiler warnings. */
1792       YYCASE_(0, YY_("syntax error"));
1793       YYCASE_(1, YY_("syntax error, unexpected %s"));
1794       YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1795       YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1796       YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1797       YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1798 # undef YYCASE_
1799     }
1800 
1801   {
1802     YYSIZE_T yysize1 = yysize + yystrlen (yyformat);
1803     if (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM)
1804       yysize = yysize1;
1805     else
1806       return 2;
1807   }
1808 
1809   if (*yymsg_alloc < yysize)
1810     {
1811       *yymsg_alloc = 2 * yysize;
1812       if (! (yysize <= *yymsg_alloc
1813              && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM))
1814         *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM;
1815       return 1;
1816     }
1817 
1818   /* Avoid sprintf, as that infringes on the user's name space.
1819      Don't have undefined behavior even if the translation
1820      produced a string with the wrong number of "%s"s.  */
1821   {
1822     char *yyp = *yymsg;
1823     int yyi = 0;
1824     while ((*yyp = *yyformat) != '\0')
1825       if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
1826         {
1827           yyp += yytnamerr (yyp, yyarg[yyi++]);
1828           yyformat += 2;
1829         }
1830       else
1831         {
1832           yyp++;
1833           yyformat++;
1834         }
1835   }
1836   return 0;
1837 }
1838 #endif /* YYERROR_VERBOSE */
1839 
1840 /*-----------------------------------------------.
1841 | Release the memory associated to this symbol.  |
1842 `-----------------------------------------------*/
1843 
1844 static void
yydestruct(const char * yymsg,int yytype,YYSTYPE * yyvaluep,YYLTYPE * yylocationp)1845 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep, YYLTYPE *yylocationp)
1846 {
1847   YYUSE (yyvaluep);
1848   YYUSE (yylocationp);
1849   if (!yymsg)
1850     yymsg = "Deleting";
1851   YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1852 
1853   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1854   YYUSE (yytype);
1855   YY_IGNORE_MAYBE_UNINITIALIZED_END
1856 }
1857 
1858 
1859 
1860 
1861 /* The lookahead symbol.  */
1862 int yychar;
1863 
1864 /* The semantic value of the lookahead symbol.  */
1865 YYSTYPE yylval;
1866 /* Location data for the lookahead symbol.  */
1867 YYLTYPE yylloc
1868 # if defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL
1869   = { 1, 1, 1, 1 }
1870 # endif
1871 ;
1872 /* Number of syntax errors so far.  */
1873 int yynerrs;
1874 
1875 
1876 /*----------.
1877 | yyparse.  |
1878 `----------*/
1879 
1880 int
yyparse(void)1881 yyparse (void)
1882 {
1883     int yystate;
1884     /* Number of tokens to shift before error messages enabled.  */
1885     int yyerrstatus;
1886 
1887     /* The stacks and their tools:
1888        'yyss': related to states.
1889        'yyvs': related to semantic values.
1890        'yyls': related to locations.
1891 
1892        Refer to the stacks through separate pointers, to allow yyoverflow
1893        to reallocate them elsewhere.  */
1894 
1895     /* The state stack.  */
1896     yytype_int16 yyssa[YYINITDEPTH];
1897     yytype_int16 *yyss;
1898     yytype_int16 *yyssp;
1899 
1900     /* The semantic value stack.  */
1901     YYSTYPE yyvsa[YYINITDEPTH];
1902     YYSTYPE *yyvs;
1903     YYSTYPE *yyvsp;
1904 
1905     /* The location stack.  */
1906     YYLTYPE yylsa[YYINITDEPTH];
1907     YYLTYPE *yyls;
1908     YYLTYPE *yylsp;
1909 
1910     /* The locations where the error started and ended.  */
1911     YYLTYPE yyerror_range[3];
1912 
1913     YYSIZE_T yystacksize;
1914 
1915   int yyn;
1916   int yyresult;
1917   /* Lookahead token as an internal (translated) token number.  */
1918   int yytoken = 0;
1919   /* The variables used to return semantic value and location from the
1920      action routines.  */
1921   YYSTYPE yyval;
1922   YYLTYPE yyloc;
1923 
1924 #if YYERROR_VERBOSE
1925   /* Buffer for error messages, and its allocated size.  */
1926   char yymsgbuf[128];
1927   char *yymsg = yymsgbuf;
1928   YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
1929 #endif
1930 
1931 #define YYPOPSTACK(N)   (yyvsp -= (N), yyssp -= (N), yylsp -= (N))
1932 
1933   /* The number of symbols on the RHS of the reduced rule.
1934      Keep to zero when no symbol should be popped.  */
1935   int yylen = 0;
1936 
1937   yyssp = yyss = yyssa;
1938   yyvsp = yyvs = yyvsa;
1939   yylsp = yyls = yylsa;
1940   yystacksize = YYINITDEPTH;
1941 
1942   YYDPRINTF ((stderr, "Starting parse\n"));
1943 
1944   yystate = 0;
1945   yyerrstatus = 0;
1946   yynerrs = 0;
1947   yychar = YYEMPTY; /* Cause a token to be read.  */
1948   yylsp[0] = yylloc;
1949   goto yysetstate;
1950 
1951 
1952 /*------------------------------------------------------------.
1953 | yynewstate -- push a new state, which is found in yystate.  |
1954 `------------------------------------------------------------*/
1955 yynewstate:
1956   /* In all cases, when you get here, the value and location stacks
1957      have just been pushed.  So pushing a state here evens the stacks.  */
1958   yyssp++;
1959 
1960 
1961 /*--------------------------------------------------------------------.
1962 | yynewstate -- set current state (the top of the stack) to yystate.  |
1963 `--------------------------------------------------------------------*/
1964 yysetstate:
1965   *yyssp = (yytype_int16) yystate;
1966 
1967   if (yyss + yystacksize - 1 <= yyssp)
1968 #if !defined yyoverflow && !defined YYSTACK_RELOCATE
1969     goto yyexhaustedlab;
1970 #else
1971     {
1972       /* Get the current used size of the three stacks, in elements.  */
1973       YYSIZE_T yysize = (YYSIZE_T) (yyssp - yyss + 1);
1974 
1975 # if defined yyoverflow
1976       {
1977         /* Give user a chance to reallocate the stack.  Use copies of
1978            these so that the &'s don't force the real ones into
1979            memory.  */
1980         YYSTYPE *yyvs1 = yyvs;
1981         yytype_int16 *yyss1 = yyss;
1982         YYLTYPE *yyls1 = yyls;
1983 
1984         /* Each stack pointer address is followed by the size of the
1985            data in use in that stack, in bytes.  This used to be a
1986            conditional around just the two extra args, but that might
1987            be undefined if yyoverflow is a macro.  */
1988         yyoverflow (YY_("memory exhausted"),
1989                     &yyss1, yysize * sizeof (*yyssp),
1990                     &yyvs1, yysize * sizeof (*yyvsp),
1991                     &yyls1, yysize * sizeof (*yylsp),
1992                     &yystacksize);
1993         yyss = yyss1;
1994         yyvs = yyvs1;
1995         yyls = yyls1;
1996       }
1997 # else /* defined YYSTACK_RELOCATE */
1998       /* Extend the stack our own way.  */
1999       if (YYMAXDEPTH <= yystacksize)
2000         goto yyexhaustedlab;
2001       yystacksize *= 2;
2002       if (YYMAXDEPTH < yystacksize)
2003         yystacksize = YYMAXDEPTH;
2004 
2005       {
2006         yytype_int16 *yyss1 = yyss;
2007         union yyalloc *yyptr =
2008           (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
2009         if (! yyptr)
2010           goto yyexhaustedlab;
2011         YYSTACK_RELOCATE (yyss_alloc, yyss);
2012         YYSTACK_RELOCATE (yyvs_alloc, yyvs);
2013         YYSTACK_RELOCATE (yyls_alloc, yyls);
2014 # undef YYSTACK_RELOCATE
2015         if (yyss1 != yyssa)
2016           YYSTACK_FREE (yyss1);
2017       }
2018 # endif
2019 
2020       yyssp = yyss + yysize - 1;
2021       yyvsp = yyvs + yysize - 1;
2022       yylsp = yyls + yysize - 1;
2023 
2024       YYDPRINTF ((stderr, "Stack size increased to %lu\n",
2025                   (unsigned long) yystacksize));
2026 
2027       if (yyss + yystacksize - 1 <= yyssp)
2028         YYABORT;
2029     }
2030 #endif /* !defined yyoverflow && !defined YYSTACK_RELOCATE */
2031 
2032   YYDPRINTF ((stderr, "Entering state %d\n", yystate));
2033 
2034   if (yystate == YYFINAL)
2035     YYACCEPT;
2036 
2037   goto yybackup;
2038 
2039 
2040 /*-----------.
2041 | yybackup.  |
2042 `-----------*/
2043 yybackup:
2044   /* Do appropriate processing given the current state.  Read a
2045      lookahead token if we need one and don't already have one.  */
2046 
2047   /* First try to decide what to do without reference to lookahead token.  */
2048   yyn = yypact[yystate];
2049   if (yypact_value_is_default (yyn))
2050     goto yydefault;
2051 
2052   /* Not known => get a lookahead token if don't already have one.  */
2053 
2054   /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol.  */
2055   if (yychar == YYEMPTY)
2056     {
2057       YYDPRINTF ((stderr, "Reading a token: "));
2058       yychar = yylex ();
2059     }
2060 
2061   if (yychar <= YYEOF)
2062     {
2063       yychar = yytoken = YYEOF;
2064       YYDPRINTF ((stderr, "Now at end of input.\n"));
2065     }
2066   else
2067     {
2068       yytoken = YYTRANSLATE (yychar);
2069       YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
2070     }
2071 
2072   /* If the proper action on seeing token YYTOKEN is to reduce or to
2073      detect an error, take that action.  */
2074   yyn += yytoken;
2075   if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
2076     goto yydefault;
2077   yyn = yytable[yyn];
2078   if (yyn <= 0)
2079     {
2080       if (yytable_value_is_error (yyn))
2081         goto yyerrlab;
2082       yyn = -yyn;
2083       goto yyreduce;
2084     }
2085 
2086   /* Count tokens shifted since error; after three, turn off error
2087      status.  */
2088   if (yyerrstatus)
2089     yyerrstatus--;
2090 
2091   /* Shift the lookahead token.  */
2092   YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
2093 
2094   /* Discard the shifted token.  */
2095   yychar = YYEMPTY;
2096 
2097   yystate = yyn;
2098   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
2099   *++yyvsp = yylval;
2100   YY_IGNORE_MAYBE_UNINITIALIZED_END
2101   *++yylsp = yylloc;
2102   goto yynewstate;
2103 
2104 
2105 /*-----------------------------------------------------------.
2106 | yydefault -- do the default action for the current state.  |
2107 `-----------------------------------------------------------*/
2108 yydefault:
2109   yyn = yydefact[yystate];
2110   if (yyn == 0)
2111     goto yyerrlab;
2112   goto yyreduce;
2113 
2114 
2115 /*-----------------------------.
2116 | yyreduce -- do a reduction.  |
2117 `-----------------------------*/
2118 yyreduce:
2119   /* yyn is the number of a rule to reduce with.  */
2120   yylen = yyr2[yyn];
2121 
2122   /* If YYLEN is nonzero, implement the default value of the action:
2123      '$$ = $1'.
2124 
2125      Otherwise, the following line sets YYVAL to garbage.
2126      This behavior is undocumented and Bison
2127      users should not rely upon it.  Assigning to YYVAL
2128      unconditionally makes the parser a bit smaller, and it avoids a
2129      GCC warning that YYVAL may be used uninitialized.  */
2130   yyval = yyvsp[1-yylen];
2131 
2132   /* Default location. */
2133   YYLLOC_DEFAULT (yyloc, (yylsp - yylen), yylen);
2134   yyerror_range[1] = yyloc;
2135   YY_REDUCE_PRINT (yyn);
2136   switch (yyn)
2137     {
2138         case 2:
2139 #line 361 "pl_gram.y" /* yacc.c:1652  */
2140     {
2141 						plpgsql_parse_result = (PLpgSQL_stmt_block *) (yyvsp[-1].stmt);
2142 					}
2143 #line 2144 "pl_gram.c" /* yacc.c:1652  */
2144     break;
2145 
2146   case 5:
2147 #line 371 "pl_gram.y" /* yacc.c:1652  */
2148     {
2149 						plpgsql_DumpExecTree = true;
2150 					}
2151 #line 2152 "pl_gram.c" /* yacc.c:1652  */
2152     break;
2153 
2154   case 6:
2155 #line 375 "pl_gram.y" /* yacc.c:1652  */
2156     {
2157 						if (strcmp((yyvsp[0].str), "on") == 0)
2158 							plpgsql_curr_compile->print_strict_params = true;
2159 						else if (strcmp((yyvsp[0].str), "off") == 0)
2160 							plpgsql_curr_compile->print_strict_params = false;
2161 						else
2162 							elog(ERROR, "unrecognized print_strict_params option %s", (yyvsp[0].str));
2163 					}
2164 #line 2165 "pl_gram.c" /* yacc.c:1652  */
2165     break;
2166 
2167   case 7:
2168 #line 384 "pl_gram.y" /* yacc.c:1652  */
2169     {
2170 						plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_ERROR;
2171 					}
2172 #line 2173 "pl_gram.c" /* yacc.c:1652  */
2173     break;
2174 
2175   case 8:
2176 #line 388 "pl_gram.y" /* yacc.c:1652  */
2177     {
2178 						plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_VARIABLE;
2179 					}
2180 #line 2181 "pl_gram.c" /* yacc.c:1652  */
2181     break;
2182 
2183   case 9:
2184 #line 392 "pl_gram.y" /* yacc.c:1652  */
2185     {
2186 						plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_COLUMN;
2187 					}
2188 #line 2189 "pl_gram.c" /* yacc.c:1652  */
2189     break;
2190 
2191   case 10:
2192 #line 398 "pl_gram.y" /* yacc.c:1652  */
2193     {
2194 					(yyval.str) = (yyvsp[0].word).ident;
2195 				}
2196 #line 2197 "pl_gram.c" /* yacc.c:1652  */
2197     break;
2198 
2199   case 11:
2200 #line 402 "pl_gram.y" /* yacc.c:1652  */
2201     {
2202 					(yyval.str) = pstrdup((yyvsp[0].keyword));
2203 				}
2204 #line 2205 "pl_gram.c" /* yacc.c:1652  */
2205     break;
2206 
2207   case 14:
2208 #line 411 "pl_gram.y" /* yacc.c:1652  */
2209     {
2210 						PLpgSQL_stmt_block *new;
2211 
2212 						new = palloc0(sizeof(PLpgSQL_stmt_block));
2213 
2214 						new->cmd_type	= PLPGSQL_STMT_BLOCK;
2215 						new->lineno		= plpgsql_location_to_lineno((yylsp[-4]));
2216 						new->label		= (yyvsp[-5].declhdr).label;
2217 						new->n_initvars = (yyvsp[-5].declhdr).n_initvars;
2218 						new->initvarnos = (yyvsp[-5].declhdr).initvarnos;
2219 						new->body		= (yyvsp[-3].list);
2220 						new->exceptions	= (yyvsp[-2].exception_block);
2221 
2222 						check_labels((yyvsp[-5].declhdr).label, (yyvsp[0].str), (yylsp[0]));
2223 						plpgsql_ns_pop();
2224 
2225 						(yyval.stmt) = (PLpgSQL_stmt *)new;
2226 					}
2227 #line 2228 "pl_gram.c" /* yacc.c:1652  */
2228     break;
2229 
2230   case 15:
2231 #line 433 "pl_gram.y" /* yacc.c:1652  */
2232     {
2233 						/* done with decls, so resume identifier lookup */
2234 						plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
2235 						(yyval.declhdr).label	  = (yyvsp[0].str);
2236 						(yyval.declhdr).n_initvars = 0;
2237 						(yyval.declhdr).initvarnos = NULL;
2238 					}
2239 #line 2240 "pl_gram.c" /* yacc.c:1652  */
2240     break;
2241 
2242   case 16:
2243 #line 441 "pl_gram.y" /* yacc.c:1652  */
2244     {
2245 						plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
2246 						(yyval.declhdr).label	  = (yyvsp[-1].str);
2247 						(yyval.declhdr).n_initvars = 0;
2248 						(yyval.declhdr).initvarnos = NULL;
2249 					}
2250 #line 2251 "pl_gram.c" /* yacc.c:1652  */
2251     break;
2252 
2253   case 17:
2254 #line 448 "pl_gram.y" /* yacc.c:1652  */
2255     {
2256 						plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
2257 						(yyval.declhdr).label	  = (yyvsp[-2].str);
2258 						/* Remember variables declared in decl_stmts */
2259 						(yyval.declhdr).n_initvars = plpgsql_add_initdatums(&((yyval.declhdr).initvarnos));
2260 					}
2261 #line 2262 "pl_gram.c" /* yacc.c:1652  */
2262     break;
2263 
2264   case 18:
2265 #line 457 "pl_gram.y" /* yacc.c:1652  */
2266     {
2267 						/* Forget any variables created before block */
2268 						plpgsql_add_initdatums(NULL);
2269 						/*
2270 						 * Disable scanner lookup of identifiers while
2271 						 * we process the decl_stmts
2272 						 */
2273 						plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
2274 					}
2275 #line 2276 "pl_gram.c" /* yacc.c:1652  */
2276     break;
2277 
2278   case 22:
2279 #line 474 "pl_gram.y" /* yacc.c:1652  */
2280     {
2281 						/* We allow useless extra DECLAREs */
2282 					}
2283 #line 2284 "pl_gram.c" /* yacc.c:1652  */
2284     break;
2285 
2286   case 23:
2287 #line 478 "pl_gram.y" /* yacc.c:1652  */
2288     {
2289 						/*
2290 						 * Throw a helpful error if user tries to put block
2291 						 * label just before BEGIN, instead of before DECLARE.
2292 						 */
2293 						ereport(ERROR,
2294 								(errcode(ERRCODE_SYNTAX_ERROR),
2295 								 errmsg("block label must be placed before DECLARE, not after"),
2296 								 parser_errposition((yylsp[-2]))));
2297 					}
2298 #line 2299 "pl_gram.c" /* yacc.c:1652  */
2299     break;
2300 
2301   case 24:
2302 #line 491 "pl_gram.y" /* yacc.c:1652  */
2303     {
2304 						PLpgSQL_variable	*var;
2305 
2306 						/*
2307 						 * If a collation is supplied, insert it into the
2308 						 * datatype.  We assume decl_datatype always returns
2309 						 * a freshly built struct not shared with other
2310 						 * variables.
2311 						 */
2312 						if (OidIsValid((yyvsp[-2].oid)))
2313 						{
2314 							if (!OidIsValid((yyvsp[-3].dtype)->collation))
2315 								ereport(ERROR,
2316 										(errcode(ERRCODE_DATATYPE_MISMATCH),
2317 										 errmsg("collations are not supported by type %s",
2318 												format_type_be((yyvsp[-3].dtype)->typoid)),
2319 										 parser_errposition((yylsp[-2]))));
2320 							(yyvsp[-3].dtype)->collation = (yyvsp[-2].oid);
2321 						}
2322 
2323 						var = plpgsql_build_variable((yyvsp[-5].varname).name, (yyvsp[-5].varname).lineno,
2324 													 (yyvsp[-3].dtype), true);
2325 						var->isconst = (yyvsp[-4].boolean);
2326 						var->notnull = (yyvsp[-1].boolean);
2327 						var->default_val = (yyvsp[0].expr);
2328 
2329 						/*
2330 						 * The combination of NOT NULL without an initializer
2331 						 * can't work, so let's reject it at compile time.
2332 						 */
2333 						if (var->notnull && var->default_val == NULL)
2334 							ereport(ERROR,
2335 									(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2336 									 errmsg("variable \"%s\" must have a default value, since it's declared NOT NULL",
2337 											var->refname),
2338 									 parser_errposition((yylsp[-1]))));
2339 					}
2340 #line 2341 "pl_gram.c" /* yacc.c:1652  */
2341     break;
2342 
2343   case 25:
2344 #line 529 "pl_gram.y" /* yacc.c:1652  */
2345     {
2346 						plpgsql_ns_additem((yyvsp[-1].nsitem)->itemtype,
2347 										   (yyvsp[-1].nsitem)->itemno, (yyvsp[-4].varname).name);
2348 					}
2349 #line 2350 "pl_gram.c" /* yacc.c:1652  */
2350     break;
2351 
2352   case 26:
2353 #line 534 "pl_gram.y" /* yacc.c:1652  */
2354     { plpgsql_ns_push((yyvsp[-2].varname).name, PLPGSQL_LABEL_OTHER); }
2355 #line 2356 "pl_gram.c" /* yacc.c:1652  */
2356     break;
2357 
2358   case 27:
2359 #line 536 "pl_gram.y" /* yacc.c:1652  */
2360     {
2361 						PLpgSQL_var *new;
2362 						PLpgSQL_expr *curname_def;
2363 						char		buf[1024];
2364 						char		*cp1;
2365 						char		*cp2;
2366 
2367 						/* pop local namespace for cursor args */
2368 						plpgsql_ns_pop();
2369 
2370 						new = (PLpgSQL_var *)
2371 							plpgsql_build_variable((yyvsp[-6].varname).name, (yyvsp[-6].varname).lineno,
2372 												   plpgsql_build_datatype(REFCURSOROID,
2373 																		  -1,
2374 																		  InvalidOid,
2375 																		  NULL),
2376 												   true);
2377 
2378 						curname_def = palloc0(sizeof(PLpgSQL_expr));
2379 
2380 						strcpy(buf, "SELECT ");
2381 						cp1 = new->refname;
2382 						cp2 = buf + strlen(buf);
2383 						/*
2384 						 * Don't trust standard_conforming_strings here;
2385 						 * it might change before we use the string.
2386 						 */
2387 						if (strchr(cp1, '\\') != NULL)
2388 							*cp2++ = ESCAPE_STRING_SYNTAX;
2389 						*cp2++ = '\'';
2390 						while (*cp1)
2391 						{
2392 							if (SQL_STR_DOUBLE(*cp1, true))
2393 								*cp2++ = *cp1;
2394 							*cp2++ = *cp1++;
2395 						}
2396 						strcpy(cp2, "'::pg_catalog.refcursor");
2397 						curname_def->query = pstrdup(buf);
2398 						new->default_val = curname_def;
2399 
2400 						new->cursor_explicit_expr = (yyvsp[0].expr);
2401 						if ((yyvsp[-2].datum) == NULL)
2402 							new->cursor_explicit_argrow = -1;
2403 						else
2404 							new->cursor_explicit_argrow = (yyvsp[-2].datum)->dno;
2405 						new->cursor_options = CURSOR_OPT_FAST_PLAN | (yyvsp[-5].ival);
2406 					}
2407 #line 2408 "pl_gram.c" /* yacc.c:1652  */
2408     break;
2409 
2410   case 28:
2411 #line 586 "pl_gram.y" /* yacc.c:1652  */
2412     {
2413 						(yyval.ival) = 0;
2414 					}
2415 #line 2416 "pl_gram.c" /* yacc.c:1652  */
2416     break;
2417 
2418   case 29:
2419 #line 590 "pl_gram.y" /* yacc.c:1652  */
2420     {
2421 						(yyval.ival) = CURSOR_OPT_NO_SCROLL;
2422 					}
2423 #line 2424 "pl_gram.c" /* yacc.c:1652  */
2424     break;
2425 
2426   case 30:
2427 #line 594 "pl_gram.y" /* yacc.c:1652  */
2428     {
2429 						(yyval.ival) = CURSOR_OPT_SCROLL;
2430 					}
2431 #line 2432 "pl_gram.c" /* yacc.c:1652  */
2432     break;
2433 
2434   case 31:
2435 #line 600 "pl_gram.y" /* yacc.c:1652  */
2436     {
2437 						(yyval.expr) = read_sql_stmt("");
2438 					}
2439 #line 2440 "pl_gram.c" /* yacc.c:1652  */
2440     break;
2441 
2442   case 32:
2443 #line 606 "pl_gram.y" /* yacc.c:1652  */
2444     {
2445 						(yyval.datum) = NULL;
2446 					}
2447 #line 2448 "pl_gram.c" /* yacc.c:1652  */
2448     break;
2449 
2450   case 33:
2451 #line 610 "pl_gram.y" /* yacc.c:1652  */
2452     {
2453 						PLpgSQL_row *new;
2454 						int i;
2455 						ListCell *l;
2456 
2457 						new = palloc0(sizeof(PLpgSQL_row));
2458 						new->dtype = PLPGSQL_DTYPE_ROW;
2459 						new->refname = "(unnamed row)";
2460 						new->lineno = plpgsql_location_to_lineno((yylsp[-2]));
2461 						new->rowtupdesc = NULL;
2462 						new->nfields = list_length((yyvsp[-1].list));
2463 						new->fieldnames = palloc(new->nfields * sizeof(char *));
2464 						new->varnos = palloc(new->nfields * sizeof(int));
2465 
2466 						i = 0;
2467 						foreach (l, (yyvsp[-1].list))
2468 						{
2469 							PLpgSQL_variable *arg = (PLpgSQL_variable *) lfirst(l);
2470 							Assert(!arg->isconst);
2471 							new->fieldnames[i] = arg->refname;
2472 							new->varnos[i] = arg->dno;
2473 							i++;
2474 						}
2475 						list_free((yyvsp[-1].list));
2476 
2477 						plpgsql_adddatum((PLpgSQL_datum *) new);
2478 						(yyval.datum) = (PLpgSQL_datum *) new;
2479 					}
2480 #line 2481 "pl_gram.c" /* yacc.c:1652  */
2481     break;
2482 
2483   case 34:
2484 #line 641 "pl_gram.y" /* yacc.c:1652  */
2485     {
2486 						(yyval.list) = list_make1((yyvsp[0].datum));
2487 					}
2488 #line 2489 "pl_gram.c" /* yacc.c:1652  */
2489     break;
2490 
2491   case 35:
2492 #line 645 "pl_gram.y" /* yacc.c:1652  */
2493     {
2494 						(yyval.list) = lappend((yyvsp[-2].list), (yyvsp[0].datum));
2495 					}
2496 #line 2497 "pl_gram.c" /* yacc.c:1652  */
2497     break;
2498 
2499   case 36:
2500 #line 651 "pl_gram.y" /* yacc.c:1652  */
2501     {
2502 						(yyval.datum) = (PLpgSQL_datum *)
2503 							plpgsql_build_variable((yyvsp[-1].varname).name, (yyvsp[-1].varname).lineno,
2504 												   (yyvsp[0].dtype), true);
2505 					}
2506 #line 2507 "pl_gram.c" /* yacc.c:1652  */
2507     break;
2508 
2509   case 39:
2510 #line 662 "pl_gram.y" /* yacc.c:1652  */
2511     {
2512 						PLpgSQL_nsitem *nsi;
2513 
2514 						nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
2515 												(yyvsp[0].word).ident, NULL, NULL,
2516 												NULL);
2517 						if (nsi == NULL)
2518 							ereport(ERROR,
2519 									(errcode(ERRCODE_UNDEFINED_OBJECT),
2520 									 errmsg("variable \"%s\" does not exist",
2521 											(yyvsp[0].word).ident),
2522 									 parser_errposition((yylsp[0]))));
2523 						(yyval.nsitem) = nsi;
2524 					}
2525 #line 2526 "pl_gram.c" /* yacc.c:1652  */
2526     break;
2527 
2528   case 40:
2529 #line 677 "pl_gram.y" /* yacc.c:1652  */
2530     {
2531 						PLpgSQL_nsitem *nsi;
2532 
2533 						nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
2534 												(yyvsp[0].keyword), NULL, NULL,
2535 												NULL);
2536 						if (nsi == NULL)
2537 							ereport(ERROR,
2538 									(errcode(ERRCODE_UNDEFINED_OBJECT),
2539 									 errmsg("variable \"%s\" does not exist",
2540 											(yyvsp[0].keyword)),
2541 									 parser_errposition((yylsp[0]))));
2542 						(yyval.nsitem) = nsi;
2543 					}
2544 #line 2545 "pl_gram.c" /* yacc.c:1652  */
2545     break;
2546 
2547   case 41:
2548 #line 692 "pl_gram.y" /* yacc.c:1652  */
2549     {
2550 						PLpgSQL_nsitem *nsi;
2551 
2552 						if (list_length((yyvsp[0].cword).idents) == 2)
2553 							nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
2554 													strVal(linitial((yyvsp[0].cword).idents)),
2555 													strVal(lsecond((yyvsp[0].cword).idents)),
2556 													NULL,
2557 													NULL);
2558 						else if (list_length((yyvsp[0].cword).idents) == 3)
2559 							nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
2560 													strVal(linitial((yyvsp[0].cword).idents)),
2561 													strVal(lsecond((yyvsp[0].cword).idents)),
2562 													strVal(lthird((yyvsp[0].cword).idents)),
2563 													NULL);
2564 						else
2565 							nsi = NULL;
2566 						if (nsi == NULL)
2567 							ereport(ERROR,
2568 									(errcode(ERRCODE_UNDEFINED_OBJECT),
2569 									 errmsg("variable \"%s\" does not exist",
2570 											NameListToString((yyvsp[0].cword).idents)),
2571 									 parser_errposition((yylsp[0]))));
2572 						(yyval.nsitem) = nsi;
2573 					}
2574 #line 2575 "pl_gram.c" /* yacc.c:1652  */
2575     break;
2576 
2577   case 42:
2578 #line 720 "pl_gram.y" /* yacc.c:1652  */
2579     {
2580 						(yyval.varname).name = (yyvsp[0].word).ident;
2581 						(yyval.varname).lineno = plpgsql_location_to_lineno((yylsp[0]));
2582 						/*
2583 						 * Check to make sure name isn't already declared
2584 						 * in the current block.
2585 						 */
2586 						if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
2587 											  (yyvsp[0].word).ident, NULL, NULL,
2588 											  NULL) != NULL)
2589 							yyerror("duplicate declaration");
2590 
2591 						if (plpgsql_curr_compile->extra_warnings & PLPGSQL_XCHECK_SHADOWVAR ||
2592 							plpgsql_curr_compile->extra_errors & PLPGSQL_XCHECK_SHADOWVAR)
2593 						{
2594 							PLpgSQL_nsitem *nsi;
2595 							nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
2596 													(yyvsp[0].word).ident, NULL, NULL, NULL);
2597 							if (nsi != NULL)
2598 								ereport(plpgsql_curr_compile->extra_errors & PLPGSQL_XCHECK_SHADOWVAR ? ERROR : WARNING,
2599 										(errcode(ERRCODE_DUPLICATE_ALIAS),
2600 										 errmsg("variable \"%s\" shadows a previously defined variable",
2601 												(yyvsp[0].word).ident),
2602 										 parser_errposition((yylsp[0]))));
2603 						}
2604 
2605 					}
2606 #line 2607 "pl_gram.c" /* yacc.c:1652  */
2607     break;
2608 
2609   case 43:
2610 #line 748 "pl_gram.y" /* yacc.c:1652  */
2611     {
2612 						(yyval.varname).name = pstrdup((yyvsp[0].keyword));
2613 						(yyval.varname).lineno = plpgsql_location_to_lineno((yylsp[0]));
2614 						/*
2615 						 * Check to make sure name isn't already declared
2616 						 * in the current block.
2617 						 */
2618 						if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
2619 											  (yyvsp[0].keyword), NULL, NULL,
2620 											  NULL) != NULL)
2621 							yyerror("duplicate declaration");
2622 
2623 						if (plpgsql_curr_compile->extra_warnings & PLPGSQL_XCHECK_SHADOWVAR ||
2624 							plpgsql_curr_compile->extra_errors & PLPGSQL_XCHECK_SHADOWVAR)
2625 						{
2626 							PLpgSQL_nsitem *nsi;
2627 							nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
2628 													(yyvsp[0].keyword), NULL, NULL, NULL);
2629 							if (nsi != NULL)
2630 								ereport(plpgsql_curr_compile->extra_errors & PLPGSQL_XCHECK_SHADOWVAR ? ERROR : WARNING,
2631 										(errcode(ERRCODE_DUPLICATE_ALIAS),
2632 										 errmsg("variable \"%s\" shadows a previously defined variable",
2633 												(yyvsp[0].keyword)),
2634 										 parser_errposition((yylsp[0]))));
2635 						}
2636 
2637 					}
2638 #line 2639 "pl_gram.c" /* yacc.c:1652  */
2639     break;
2640 
2641   case 44:
2642 #line 778 "pl_gram.y" /* yacc.c:1652  */
2643     { (yyval.boolean) = false; }
2644 #line 2645 "pl_gram.c" /* yacc.c:1652  */
2645     break;
2646 
2647   case 45:
2648 #line 780 "pl_gram.y" /* yacc.c:1652  */
2649     { (yyval.boolean) = true; }
2650 #line 2651 "pl_gram.c" /* yacc.c:1652  */
2651     break;
2652 
2653   case 46:
2654 #line 784 "pl_gram.y" /* yacc.c:1652  */
2655     {
2656 						/*
2657 						 * If there's a lookahead token, read_datatype
2658 						 * should consume it.
2659 						 */
2660 						(yyval.dtype) = read_datatype(yychar);
2661 						yyclearin;
2662 					}
2663 #line 2664 "pl_gram.c" /* yacc.c:1652  */
2664     break;
2665 
2666   case 47:
2667 #line 795 "pl_gram.y" /* yacc.c:1652  */
2668     { (yyval.oid) = InvalidOid; }
2669 #line 2670 "pl_gram.c" /* yacc.c:1652  */
2670     break;
2671 
2672   case 48:
2673 #line 797 "pl_gram.y" /* yacc.c:1652  */
2674     {
2675 						(yyval.oid) = get_collation_oid(list_make1(makeString((yyvsp[0].word).ident)),
2676 											   false);
2677 					}
2678 #line 2679 "pl_gram.c" /* yacc.c:1652  */
2679     break;
2680 
2681   case 49:
2682 #line 802 "pl_gram.y" /* yacc.c:1652  */
2683     {
2684 						(yyval.oid) = get_collation_oid(list_make1(makeString(pstrdup((yyvsp[0].keyword)))),
2685 											   false);
2686 					}
2687 #line 2688 "pl_gram.c" /* yacc.c:1652  */
2688     break;
2689 
2690   case 50:
2691 #line 807 "pl_gram.y" /* yacc.c:1652  */
2692     {
2693 						(yyval.oid) = get_collation_oid((yyvsp[0].cword).idents, false);
2694 					}
2695 #line 2696 "pl_gram.c" /* yacc.c:1652  */
2696     break;
2697 
2698   case 51:
2699 #line 813 "pl_gram.y" /* yacc.c:1652  */
2700     { (yyval.boolean) = false; }
2701 #line 2702 "pl_gram.c" /* yacc.c:1652  */
2702     break;
2703 
2704   case 52:
2705 #line 815 "pl_gram.y" /* yacc.c:1652  */
2706     { (yyval.boolean) = true; }
2707 #line 2708 "pl_gram.c" /* yacc.c:1652  */
2708     break;
2709 
2710   case 53:
2711 #line 819 "pl_gram.y" /* yacc.c:1652  */
2712     { (yyval.expr) = NULL; }
2713 #line 2714 "pl_gram.c" /* yacc.c:1652  */
2714     break;
2715 
2716   case 54:
2717 #line 821 "pl_gram.y" /* yacc.c:1652  */
2718     {
2719 						(yyval.expr) = read_sql_expression(';', ";");
2720 					}
2721 #line 2722 "pl_gram.c" /* yacc.c:1652  */
2722     break;
2723 
2724   case 59:
2725 #line 840 "pl_gram.y" /* yacc.c:1652  */
2726     { (yyval.list) = NIL; }
2727 #line 2728 "pl_gram.c" /* yacc.c:1652  */
2728     break;
2729 
2730   case 60:
2731 #line 842 "pl_gram.y" /* yacc.c:1652  */
2732     {
2733 						/* don't bother linking null statements into list */
2734 						if ((yyvsp[0].stmt) == NULL)
2735 							(yyval.list) = (yyvsp[-1].list);
2736 						else
2737 							(yyval.list) = lappend((yyvsp[-1].list), (yyvsp[0].stmt));
2738 					}
2739 #line 2740 "pl_gram.c" /* yacc.c:1652  */
2740     break;
2741 
2742   case 61:
2743 #line 852 "pl_gram.y" /* yacc.c:1652  */
2744     { (yyval.stmt) = (yyvsp[-1].stmt); }
2745 #line 2746 "pl_gram.c" /* yacc.c:1652  */
2746     break;
2747 
2748   case 62:
2749 #line 854 "pl_gram.y" /* yacc.c:1652  */
2750     { (yyval.stmt) = (yyvsp[0].stmt); }
2751 #line 2752 "pl_gram.c" /* yacc.c:1652  */
2752     break;
2753 
2754   case 63:
2755 #line 856 "pl_gram.y" /* yacc.c:1652  */
2756     { (yyval.stmt) = (yyvsp[0].stmt); }
2757 #line 2758 "pl_gram.c" /* yacc.c:1652  */
2758     break;
2759 
2760   case 64:
2761 #line 858 "pl_gram.y" /* yacc.c:1652  */
2762     { (yyval.stmt) = (yyvsp[0].stmt); }
2763 #line 2764 "pl_gram.c" /* yacc.c:1652  */
2764     break;
2765 
2766   case 65:
2767 #line 860 "pl_gram.y" /* yacc.c:1652  */
2768     { (yyval.stmt) = (yyvsp[0].stmt); }
2769 #line 2770 "pl_gram.c" /* yacc.c:1652  */
2770     break;
2771 
2772   case 66:
2773 #line 862 "pl_gram.y" /* yacc.c:1652  */
2774     { (yyval.stmt) = (yyvsp[0].stmt); }
2775 #line 2776 "pl_gram.c" /* yacc.c:1652  */
2776     break;
2777 
2778   case 67:
2779 #line 864 "pl_gram.y" /* yacc.c:1652  */
2780     { (yyval.stmt) = (yyvsp[0].stmt); }
2781 #line 2782 "pl_gram.c" /* yacc.c:1652  */
2782     break;
2783 
2784   case 68:
2785 #line 866 "pl_gram.y" /* yacc.c:1652  */
2786     { (yyval.stmt) = (yyvsp[0].stmt); }
2787 #line 2788 "pl_gram.c" /* yacc.c:1652  */
2788     break;
2789 
2790   case 69:
2791 #line 868 "pl_gram.y" /* yacc.c:1652  */
2792     { (yyval.stmt) = (yyvsp[0].stmt); }
2793 #line 2794 "pl_gram.c" /* yacc.c:1652  */
2794     break;
2795 
2796   case 70:
2797 #line 870 "pl_gram.y" /* yacc.c:1652  */
2798     { (yyval.stmt) = (yyvsp[0].stmt); }
2799 #line 2800 "pl_gram.c" /* yacc.c:1652  */
2800     break;
2801 
2802   case 71:
2803 #line 872 "pl_gram.y" /* yacc.c:1652  */
2804     { (yyval.stmt) = (yyvsp[0].stmt); }
2805 #line 2806 "pl_gram.c" /* yacc.c:1652  */
2806     break;
2807 
2808   case 72:
2809 #line 874 "pl_gram.y" /* yacc.c:1652  */
2810     { (yyval.stmt) = (yyvsp[0].stmt); }
2811 #line 2812 "pl_gram.c" /* yacc.c:1652  */
2812     break;
2813 
2814   case 73:
2815 #line 876 "pl_gram.y" /* yacc.c:1652  */
2816     { (yyval.stmt) = (yyvsp[0].stmt); }
2817 #line 2818 "pl_gram.c" /* yacc.c:1652  */
2818     break;
2819 
2820   case 74:
2821 #line 878 "pl_gram.y" /* yacc.c:1652  */
2822     { (yyval.stmt) = (yyvsp[0].stmt); }
2823 #line 2824 "pl_gram.c" /* yacc.c:1652  */
2824     break;
2825 
2826   case 75:
2827 #line 880 "pl_gram.y" /* yacc.c:1652  */
2828     { (yyval.stmt) = (yyvsp[0].stmt); }
2829 #line 2830 "pl_gram.c" /* yacc.c:1652  */
2830     break;
2831 
2832   case 76:
2833 #line 882 "pl_gram.y" /* yacc.c:1652  */
2834     { (yyval.stmt) = (yyvsp[0].stmt); }
2835 #line 2836 "pl_gram.c" /* yacc.c:1652  */
2836     break;
2837 
2838   case 77:
2839 #line 884 "pl_gram.y" /* yacc.c:1652  */
2840     { (yyval.stmt) = (yyvsp[0].stmt); }
2841 #line 2842 "pl_gram.c" /* yacc.c:1652  */
2842     break;
2843 
2844   case 78:
2845 #line 886 "pl_gram.y" /* yacc.c:1652  */
2846     { (yyval.stmt) = (yyvsp[0].stmt); }
2847 #line 2848 "pl_gram.c" /* yacc.c:1652  */
2848     break;
2849 
2850   case 79:
2851 #line 888 "pl_gram.y" /* yacc.c:1652  */
2852     { (yyval.stmt) = (yyvsp[0].stmt); }
2853 #line 2854 "pl_gram.c" /* yacc.c:1652  */
2854     break;
2855 
2856   case 80:
2857 #line 890 "pl_gram.y" /* yacc.c:1652  */
2858     { (yyval.stmt) = (yyvsp[0].stmt); }
2859 #line 2860 "pl_gram.c" /* yacc.c:1652  */
2860     break;
2861 
2862   case 81:
2863 #line 892 "pl_gram.y" /* yacc.c:1652  */
2864     { (yyval.stmt) = (yyvsp[0].stmt); }
2865 #line 2866 "pl_gram.c" /* yacc.c:1652  */
2866     break;
2867 
2868   case 82:
2869 #line 894 "pl_gram.y" /* yacc.c:1652  */
2870     { (yyval.stmt) = (yyvsp[0].stmt); }
2871 #line 2872 "pl_gram.c" /* yacc.c:1652  */
2872     break;
2873 
2874   case 83:
2875 #line 896 "pl_gram.y" /* yacc.c:1652  */
2876     { (yyval.stmt) = (yyvsp[0].stmt); }
2877 #line 2878 "pl_gram.c" /* yacc.c:1652  */
2878     break;
2879 
2880   case 84:
2881 #line 898 "pl_gram.y" /* yacc.c:1652  */
2882     { (yyval.stmt) = (yyvsp[0].stmt); }
2883 #line 2884 "pl_gram.c" /* yacc.c:1652  */
2884     break;
2885 
2886   case 85:
2887 #line 900 "pl_gram.y" /* yacc.c:1652  */
2888     { (yyval.stmt) = (yyvsp[0].stmt); }
2889 #line 2890 "pl_gram.c" /* yacc.c:1652  */
2890     break;
2891 
2892   case 86:
2893 #line 904 "pl_gram.y" /* yacc.c:1652  */
2894     {
2895 						PLpgSQL_stmt_perform *new;
2896 
2897 						new = palloc0(sizeof(PLpgSQL_stmt_perform));
2898 						new->cmd_type = PLPGSQL_STMT_PERFORM;
2899 						new->lineno   = plpgsql_location_to_lineno((yylsp[-1]));
2900 						new->expr  = (yyvsp[0].expr);
2901 
2902 						(yyval.stmt) = (PLpgSQL_stmt *)new;
2903 					}
2904 #line 2905 "pl_gram.c" /* yacc.c:1652  */
2905     break;
2906 
2907   case 87:
2908 #line 917 "pl_gram.y" /* yacc.c:1652  */
2909     {
2910 						PLpgSQL_stmt_call *new;
2911 
2912 						new = palloc0(sizeof(PLpgSQL_stmt_call));
2913 						new->cmd_type = PLPGSQL_STMT_CALL;
2914 						new->lineno = plpgsql_location_to_lineno((yylsp[0]));
2915 						new->expr = read_sql_stmt("CALL ");
2916 						new->is_call = true;
2917 
2918 						(yyval.stmt) = (PLpgSQL_stmt *)new;
2919 
2920 					}
2921 #line 2922 "pl_gram.c" /* yacc.c:1652  */
2922     break;
2923 
2924   case 88:
2925 #line 930 "pl_gram.y" /* yacc.c:1652  */
2926     {
2927 						/* use the same structures as for CALL, for simplicity */
2928 						PLpgSQL_stmt_call *new;
2929 
2930 						new = palloc0(sizeof(PLpgSQL_stmt_call));
2931 						new->cmd_type = PLPGSQL_STMT_CALL;
2932 						new->lineno = plpgsql_location_to_lineno((yylsp[0]));
2933 						new->expr = read_sql_stmt("DO ");
2934 						new->is_call = false;
2935 
2936 						(yyval.stmt) = (PLpgSQL_stmt *)new;
2937 
2938 					}
2939 #line 2940 "pl_gram.c" /* yacc.c:1652  */
2940     break;
2941 
2942   case 89:
2943 #line 946 "pl_gram.y" /* yacc.c:1652  */
2944     {
2945 						PLpgSQL_stmt_assign *new;
2946 
2947 						new = palloc0(sizeof(PLpgSQL_stmt_assign));
2948 						new->cmd_type = PLPGSQL_STMT_ASSIGN;
2949 						new->lineno   = plpgsql_location_to_lineno((yylsp[-2]));
2950 						new->varno = (yyvsp[-2].datum)->dno;
2951 						new->expr  = (yyvsp[0].expr);
2952 
2953 						(yyval.stmt) = (PLpgSQL_stmt *)new;
2954 					}
2955 #line 2956 "pl_gram.c" /* yacc.c:1652  */
2956     break;
2957 
2958   case 90:
2959 #line 960 "pl_gram.y" /* yacc.c:1652  */
2960     {
2961 						PLpgSQL_stmt_getdiag	 *new;
2962 						ListCell		*lc;
2963 
2964 						new = palloc0(sizeof(PLpgSQL_stmt_getdiag));
2965 						new->cmd_type = PLPGSQL_STMT_GETDIAG;
2966 						new->lineno   = plpgsql_location_to_lineno((yylsp[-4]));
2967 						new->is_stacked = (yyvsp[-3].boolean);
2968 						new->diag_items = (yyvsp[-1].list);
2969 
2970 						/*
2971 						 * Check information items are valid for area option.
2972 						 */
2973 						foreach(lc, new->diag_items)
2974 						{
2975 							PLpgSQL_diag_item *ditem = (PLpgSQL_diag_item *) lfirst(lc);
2976 
2977 							switch (ditem->kind)
2978 							{
2979 								/* these fields are disallowed in stacked case */
2980 								case PLPGSQL_GETDIAG_ROW_COUNT:
2981 								case PLPGSQL_GETDIAG_RESULT_OID:
2982 									if (new->is_stacked)
2983 										ereport(ERROR,
2984 												(errcode(ERRCODE_SYNTAX_ERROR),
2985 												 errmsg("diagnostics item %s is not allowed in GET STACKED DIAGNOSTICS",
2986 														plpgsql_getdiag_kindname(ditem->kind)),
2987 												 parser_errposition((yylsp[-4]))));
2988 									break;
2989 								/* these fields are disallowed in current case */
2990 								case PLPGSQL_GETDIAG_ERROR_CONTEXT:
2991 								case PLPGSQL_GETDIAG_ERROR_DETAIL:
2992 								case PLPGSQL_GETDIAG_ERROR_HINT:
2993 								case PLPGSQL_GETDIAG_RETURNED_SQLSTATE:
2994 								case PLPGSQL_GETDIAG_COLUMN_NAME:
2995 								case PLPGSQL_GETDIAG_CONSTRAINT_NAME:
2996 								case PLPGSQL_GETDIAG_DATATYPE_NAME:
2997 								case PLPGSQL_GETDIAG_MESSAGE_TEXT:
2998 								case PLPGSQL_GETDIAG_TABLE_NAME:
2999 								case PLPGSQL_GETDIAG_SCHEMA_NAME:
3000 									if (!new->is_stacked)
3001 										ereport(ERROR,
3002 												(errcode(ERRCODE_SYNTAX_ERROR),
3003 												 errmsg("diagnostics item %s is not allowed in GET CURRENT DIAGNOSTICS",
3004 														plpgsql_getdiag_kindname(ditem->kind)),
3005 												 parser_errposition((yylsp[-4]))));
3006 									break;
3007 								/* these fields are allowed in either case */
3008 								case PLPGSQL_GETDIAG_CONTEXT:
3009 									break;
3010 								default:
3011 									elog(ERROR, "unrecognized diagnostic item kind: %d",
3012 										 ditem->kind);
3013 									break;
3014 							}
3015 						}
3016 
3017 						(yyval.stmt) = (PLpgSQL_stmt *)new;
3018 					}
3019 #line 3020 "pl_gram.c" /* yacc.c:1652  */
3020     break;
3021 
3022   case 91:
3023 #line 1022 "pl_gram.y" /* yacc.c:1652  */
3024     {
3025 						(yyval.boolean) = false;
3026 					}
3027 #line 3028 "pl_gram.c" /* yacc.c:1652  */
3028     break;
3029 
3030   case 92:
3031 #line 1026 "pl_gram.y" /* yacc.c:1652  */
3032     {
3033 						(yyval.boolean) = false;
3034 					}
3035 #line 3036 "pl_gram.c" /* yacc.c:1652  */
3036     break;
3037 
3038   case 93:
3039 #line 1030 "pl_gram.y" /* yacc.c:1652  */
3040     {
3041 						(yyval.boolean) = true;
3042 					}
3043 #line 3044 "pl_gram.c" /* yacc.c:1652  */
3044     break;
3045 
3046   case 94:
3047 #line 1036 "pl_gram.y" /* yacc.c:1652  */
3048     {
3049 						(yyval.list) = lappend((yyvsp[-2].list), (yyvsp[0].diagitem));
3050 					}
3051 #line 3052 "pl_gram.c" /* yacc.c:1652  */
3052     break;
3053 
3054   case 95:
3055 #line 1040 "pl_gram.y" /* yacc.c:1652  */
3056     {
3057 						(yyval.list) = list_make1((yyvsp[0].diagitem));
3058 					}
3059 #line 3060 "pl_gram.c" /* yacc.c:1652  */
3060     break;
3061 
3062   case 96:
3063 #line 1046 "pl_gram.y" /* yacc.c:1652  */
3064     {
3065 						PLpgSQL_diag_item *new;
3066 
3067 						new = palloc(sizeof(PLpgSQL_diag_item));
3068 						new->target = (yyvsp[-2].datum)->dno;
3069 						new->kind = (yyvsp[0].ival);
3070 
3071 						(yyval.diagitem) = new;
3072 					}
3073 #line 3074 "pl_gram.c" /* yacc.c:1652  */
3074     break;
3075 
3076   case 97:
3077 #line 1058 "pl_gram.y" /* yacc.c:1652  */
3078     {
3079 						int	tok = yylex();
3080 
3081 						if (tok_is_keyword(tok, &yylval,
3082 										   K_ROW_COUNT, "row_count"))
3083 							(yyval.ival) = PLPGSQL_GETDIAG_ROW_COUNT;
3084 						else if (tok_is_keyword(tok, &yylval,
3085 												K_RESULT_OID, "result_oid"))
3086 							(yyval.ival) = PLPGSQL_GETDIAG_RESULT_OID;
3087 						else if (tok_is_keyword(tok, &yylval,
3088 												K_PG_CONTEXT, "pg_context"))
3089 							(yyval.ival) = PLPGSQL_GETDIAG_CONTEXT;
3090 						else if (tok_is_keyword(tok, &yylval,
3091 												K_PG_EXCEPTION_DETAIL, "pg_exception_detail"))
3092 							(yyval.ival) = PLPGSQL_GETDIAG_ERROR_DETAIL;
3093 						else if (tok_is_keyword(tok, &yylval,
3094 												K_PG_EXCEPTION_HINT, "pg_exception_hint"))
3095 							(yyval.ival) = PLPGSQL_GETDIAG_ERROR_HINT;
3096 						else if (tok_is_keyword(tok, &yylval,
3097 												K_PG_EXCEPTION_CONTEXT, "pg_exception_context"))
3098 							(yyval.ival) = PLPGSQL_GETDIAG_ERROR_CONTEXT;
3099 						else if (tok_is_keyword(tok, &yylval,
3100 												K_COLUMN_NAME, "column_name"))
3101 							(yyval.ival) = PLPGSQL_GETDIAG_COLUMN_NAME;
3102 						else if (tok_is_keyword(tok, &yylval,
3103 												K_CONSTRAINT_NAME, "constraint_name"))
3104 							(yyval.ival) = PLPGSQL_GETDIAG_CONSTRAINT_NAME;
3105 						else if (tok_is_keyword(tok, &yylval,
3106 												K_PG_DATATYPE_NAME, "pg_datatype_name"))
3107 							(yyval.ival) = PLPGSQL_GETDIAG_DATATYPE_NAME;
3108 						else if (tok_is_keyword(tok, &yylval,
3109 												K_MESSAGE_TEXT, "message_text"))
3110 							(yyval.ival) = PLPGSQL_GETDIAG_MESSAGE_TEXT;
3111 						else if (tok_is_keyword(tok, &yylval,
3112 												K_TABLE_NAME, "table_name"))
3113 							(yyval.ival) = PLPGSQL_GETDIAG_TABLE_NAME;
3114 						else if (tok_is_keyword(tok, &yylval,
3115 												K_SCHEMA_NAME, "schema_name"))
3116 							(yyval.ival) = PLPGSQL_GETDIAG_SCHEMA_NAME;
3117 						else if (tok_is_keyword(tok, &yylval,
3118 												K_RETURNED_SQLSTATE, "returned_sqlstate"))
3119 							(yyval.ival) = PLPGSQL_GETDIAG_RETURNED_SQLSTATE;
3120 						else
3121 							yyerror("unrecognized GET DIAGNOSTICS item");
3122 					}
3123 #line 3124 "pl_gram.c" /* yacc.c:1652  */
3124     break;
3125 
3126   case 98:
3127 #line 1106 "pl_gram.y" /* yacc.c:1652  */
3128     {
3129 						if ((yyvsp[0].datum)->dtype == PLPGSQL_DTYPE_ROW ||
3130 							(yyvsp[0].datum)->dtype == PLPGSQL_DTYPE_REC)
3131 							ereport(ERROR,
3132 									(errcode(ERRCODE_SYNTAX_ERROR),
3133 									 errmsg("\"%s\" is not a scalar variable",
3134 											((PLpgSQL_variable *) (yyvsp[0].datum))->refname),
3135 									 parser_errposition((yylsp[0]))));
3136 						(yyval.datum) = (yyvsp[0].datum);
3137 					}
3138 #line 3139 "pl_gram.c" /* yacc.c:1652  */
3139     break;
3140 
3141   case 99:
3142 #line 1117 "pl_gram.y" /* yacc.c:1652  */
3143     {
3144 						/* just to give a better message than "syntax error" */
3145 						word_is_not_variable(&((yyvsp[0].word)), (yylsp[0]));
3146 					}
3147 #line 3148 "pl_gram.c" /* yacc.c:1652  */
3148     break;
3149 
3150   case 100:
3151 #line 1122 "pl_gram.y" /* yacc.c:1652  */
3152     {
3153 						/* just to give a better message than "syntax error" */
3154 						cword_is_not_variable(&((yyvsp[0].cword)), (yylsp[0]));
3155 					}
3156 #line 3157 "pl_gram.c" /* yacc.c:1652  */
3157     break;
3158 
3159   case 101:
3160 #line 1130 "pl_gram.y" /* yacc.c:1652  */
3161     {
3162 						check_assignable((yyvsp[0].wdatum).datum, (yylsp[0]));
3163 						(yyval.datum) = (yyvsp[0].wdatum).datum;
3164 					}
3165 #line 3166 "pl_gram.c" /* yacc.c:1652  */
3166     break;
3167 
3168   case 102:
3169 #line 1135 "pl_gram.y" /* yacc.c:1652  */
3170     {
3171 						PLpgSQL_arrayelem	*new;
3172 
3173 						new = palloc0(sizeof(PLpgSQL_arrayelem));
3174 						new->dtype		= PLPGSQL_DTYPE_ARRAYELEM;
3175 						new->subscript	= (yyvsp[0].expr);
3176 						new->arrayparentno = (yyvsp[-2].datum)->dno;
3177 						/* initialize cached type data to "not valid" */
3178 						new->parenttypoid = InvalidOid;
3179 
3180 						plpgsql_adddatum((PLpgSQL_datum *) new);
3181 
3182 						(yyval.datum) = (PLpgSQL_datum *) new;
3183 					}
3184 #line 3185 "pl_gram.c" /* yacc.c:1652  */
3185     break;
3186 
3187   case 103:
3188 #line 1152 "pl_gram.y" /* yacc.c:1652  */
3189     {
3190 						PLpgSQL_stmt_if *new;
3191 
3192 						new = palloc0(sizeof(PLpgSQL_stmt_if));
3193 						new->cmd_type	= PLPGSQL_STMT_IF;
3194 						new->lineno		= plpgsql_location_to_lineno((yylsp[-7]));
3195 						new->cond		= (yyvsp[-6].expr);
3196 						new->then_body	= (yyvsp[-5].list);
3197 						new->elsif_list = (yyvsp[-4].list);
3198 						new->else_body  = (yyvsp[-3].list);
3199 
3200 						(yyval.stmt) = (PLpgSQL_stmt *)new;
3201 					}
3202 #line 3203 "pl_gram.c" /* yacc.c:1652  */
3203     break;
3204 
3205   case 104:
3206 #line 1168 "pl_gram.y" /* yacc.c:1652  */
3207     {
3208 						(yyval.list) = NIL;
3209 					}
3210 #line 3211 "pl_gram.c" /* yacc.c:1652  */
3211     break;
3212 
3213   case 105:
3214 #line 1172 "pl_gram.y" /* yacc.c:1652  */
3215     {
3216 						PLpgSQL_if_elsif *new;
3217 
3218 						new = palloc0(sizeof(PLpgSQL_if_elsif));
3219 						new->lineno = plpgsql_location_to_lineno((yylsp[-2]));
3220 						new->cond   = (yyvsp[-1].expr);
3221 						new->stmts  = (yyvsp[0].list);
3222 
3223 						(yyval.list) = lappend((yyvsp[-3].list), new);
3224 					}
3225 #line 3226 "pl_gram.c" /* yacc.c:1652  */
3226     break;
3227 
3228   case 106:
3229 #line 1185 "pl_gram.y" /* yacc.c:1652  */
3230     {
3231 						(yyval.list) = NIL;
3232 					}
3233 #line 3234 "pl_gram.c" /* yacc.c:1652  */
3234     break;
3235 
3236   case 107:
3237 #line 1189 "pl_gram.y" /* yacc.c:1652  */
3238     {
3239 						(yyval.list) = (yyvsp[0].list);
3240 					}
3241 #line 3242 "pl_gram.c" /* yacc.c:1652  */
3242     break;
3243 
3244   case 108:
3245 #line 1195 "pl_gram.y" /* yacc.c:1652  */
3246     {
3247 						(yyval.stmt) = make_case((yylsp[-6]), (yyvsp[-5].expr), (yyvsp[-4].list), (yyvsp[-3].list));
3248 					}
3249 #line 3250 "pl_gram.c" /* yacc.c:1652  */
3250     break;
3251 
3252   case 109:
3253 #line 1201 "pl_gram.y" /* yacc.c:1652  */
3254     {
3255 						PLpgSQL_expr *expr = NULL;
3256 						int	tok = yylex();
3257 
3258 						if (tok != K_WHEN)
3259 						{
3260 							plpgsql_push_back_token(tok);
3261 							expr = read_sql_expression(K_WHEN, "WHEN");
3262 						}
3263 						plpgsql_push_back_token(K_WHEN);
3264 						(yyval.expr) = expr;
3265 					}
3266 #line 3267 "pl_gram.c" /* yacc.c:1652  */
3267     break;
3268 
3269   case 110:
3270 #line 1216 "pl_gram.y" /* yacc.c:1652  */
3271     {
3272 						(yyval.list) = lappend((yyvsp[-1].list), (yyvsp[0].casewhen));
3273 					}
3274 #line 3275 "pl_gram.c" /* yacc.c:1652  */
3275     break;
3276 
3277   case 111:
3278 #line 1220 "pl_gram.y" /* yacc.c:1652  */
3279     {
3280 						(yyval.list) = list_make1((yyvsp[0].casewhen));
3281 					}
3282 #line 3283 "pl_gram.c" /* yacc.c:1652  */
3283     break;
3284 
3285   case 112:
3286 #line 1226 "pl_gram.y" /* yacc.c:1652  */
3287     {
3288 						PLpgSQL_case_when *new = palloc(sizeof(PLpgSQL_case_when));
3289 
3290 						new->lineno	= plpgsql_location_to_lineno((yylsp[-2]));
3291 						new->expr	= (yyvsp[-1].expr);
3292 						new->stmts	= (yyvsp[0].list);
3293 						(yyval.casewhen) = new;
3294 					}
3295 #line 3296 "pl_gram.c" /* yacc.c:1652  */
3296     break;
3297 
3298   case 113:
3299 #line 1237 "pl_gram.y" /* yacc.c:1652  */
3300     {
3301 						(yyval.list) = NIL;
3302 					}
3303 #line 3304 "pl_gram.c" /* yacc.c:1652  */
3304     break;
3305 
3306   case 114:
3307 #line 1241 "pl_gram.y" /* yacc.c:1652  */
3308     {
3309 						/*
3310 						 * proc_sect could return an empty list, but we
3311 						 * must distinguish that from not having ELSE at all.
3312 						 * Simplest fix is to return a list with one NULL
3313 						 * pointer, which make_case() must take care of.
3314 						 */
3315 						if ((yyvsp[0].list) != NIL)
3316 							(yyval.list) = (yyvsp[0].list);
3317 						else
3318 							(yyval.list) = list_make1(NULL);
3319 					}
3320 #line 3321 "pl_gram.c" /* yacc.c:1652  */
3321     break;
3322 
3323   case 115:
3324 #line 1256 "pl_gram.y" /* yacc.c:1652  */
3325     {
3326 						PLpgSQL_stmt_loop *new;
3327 
3328 						new = palloc0(sizeof(PLpgSQL_stmt_loop));
3329 						new->cmd_type = PLPGSQL_STMT_LOOP;
3330 						new->lineno   = plpgsql_location_to_lineno((yylsp[-1]));
3331 						new->label	  = (yyvsp[-2].str);
3332 						new->body	  = (yyvsp[0].loop_body).stmts;
3333 
3334 						check_labels((yyvsp[-2].str), (yyvsp[0].loop_body).end_label, (yyvsp[0].loop_body).end_label_location);
3335 						plpgsql_ns_pop();
3336 
3337 						(yyval.stmt) = (PLpgSQL_stmt *)new;
3338 					}
3339 #line 3340 "pl_gram.c" /* yacc.c:1652  */
3340     break;
3341 
3342   case 116:
3343 #line 1273 "pl_gram.y" /* yacc.c:1652  */
3344     {
3345 						PLpgSQL_stmt_while *new;
3346 
3347 						new = palloc0(sizeof(PLpgSQL_stmt_while));
3348 						new->cmd_type = PLPGSQL_STMT_WHILE;
3349 						new->lineno   = plpgsql_location_to_lineno((yylsp[-2]));
3350 						new->label	  = (yyvsp[-3].str);
3351 						new->cond	  = (yyvsp[-1].expr);
3352 						new->body	  = (yyvsp[0].loop_body).stmts;
3353 
3354 						check_labels((yyvsp[-3].str), (yyvsp[0].loop_body).end_label, (yyvsp[0].loop_body).end_label_location);
3355 						plpgsql_ns_pop();
3356 
3357 						(yyval.stmt) = (PLpgSQL_stmt *)new;
3358 					}
3359 #line 3360 "pl_gram.c" /* yacc.c:1652  */
3360     break;
3361 
3362   case 117:
3363 #line 1291 "pl_gram.y" /* yacc.c:1652  */
3364     {
3365 						/* This runs after we've scanned the loop body */
3366 						if ((yyvsp[-1].stmt)->cmd_type == PLPGSQL_STMT_FORI)
3367 						{
3368 							PLpgSQL_stmt_fori		*new;
3369 
3370 							new = (PLpgSQL_stmt_fori *) (yyvsp[-1].stmt);
3371 							new->lineno   = plpgsql_location_to_lineno((yylsp[-2]));
3372 							new->label	  = (yyvsp[-3].str);
3373 							new->body	  = (yyvsp[0].loop_body).stmts;
3374 							(yyval.stmt) = (PLpgSQL_stmt *) new;
3375 						}
3376 						else
3377 						{
3378 							PLpgSQL_stmt_forq		*new;
3379 
3380 							Assert((yyvsp[-1].stmt)->cmd_type == PLPGSQL_STMT_FORS ||
3381 								   (yyvsp[-1].stmt)->cmd_type == PLPGSQL_STMT_FORC ||
3382 								   (yyvsp[-1].stmt)->cmd_type == PLPGSQL_STMT_DYNFORS);
3383 							/* forq is the common supertype of all three */
3384 							new = (PLpgSQL_stmt_forq *) (yyvsp[-1].stmt);
3385 							new->lineno   = plpgsql_location_to_lineno((yylsp[-2]));
3386 							new->label	  = (yyvsp[-3].str);
3387 							new->body	  = (yyvsp[0].loop_body).stmts;
3388 							(yyval.stmt) = (PLpgSQL_stmt *) new;
3389 						}
3390 
3391 						check_labels((yyvsp[-3].str), (yyvsp[0].loop_body).end_label, (yyvsp[0].loop_body).end_label_location);
3392 						/* close namespace started in opt_loop_label */
3393 						plpgsql_ns_pop();
3394 					}
3395 #line 3396 "pl_gram.c" /* yacc.c:1652  */
3396     break;
3397 
3398   case 118:
3399 #line 1325 "pl_gram.y" /* yacc.c:1652  */
3400     {
3401 						int			tok = yylex();
3402 						int			tokloc = yylloc;
3403 
3404 						if (tok == K_EXECUTE)
3405 						{
3406 							/* EXECUTE means it's a dynamic FOR loop */
3407 							PLpgSQL_stmt_dynfors	*new;
3408 							PLpgSQL_expr			*expr;
3409 							int						term;
3410 
3411 							expr = read_sql_expression2(K_LOOP, K_USING,
3412 														"LOOP or USING",
3413 														&term);
3414 
3415 							new = palloc0(sizeof(PLpgSQL_stmt_dynfors));
3416 							new->cmd_type = PLPGSQL_STMT_DYNFORS;
3417 							if ((yyvsp[-1].forvariable).row)
3418 							{
3419 								new->var = (PLpgSQL_variable *) (yyvsp[-1].forvariable).row;
3420 								check_assignable((yyvsp[-1].forvariable).row, (yylsp[-1]));
3421 							}
3422 							else if ((yyvsp[-1].forvariable).scalar)
3423 							{
3424 								/* convert single scalar to list */
3425 								new->var = (PLpgSQL_variable *)
3426 									make_scalar_list1((yyvsp[-1].forvariable).name, (yyvsp[-1].forvariable).scalar,
3427 													  (yyvsp[-1].forvariable).lineno, (yylsp[-1]));
3428 								/* make_scalar_list1 did check_assignable */
3429 							}
3430 							else
3431 							{
3432 								ereport(ERROR,
3433 										(errcode(ERRCODE_DATATYPE_MISMATCH),
3434 										 errmsg("loop variable of loop over rows must be a record variable or list of scalar variables"),
3435 										 parser_errposition((yylsp[-1]))));
3436 							}
3437 							new->query = expr;
3438 
3439 							if (term == K_USING)
3440 							{
3441 								do
3442 								{
3443 									expr = read_sql_expression2(',', K_LOOP,
3444 																", or LOOP",
3445 																&term);
3446 									new->params = lappend(new->params, expr);
3447 								} while (term == ',');
3448 							}
3449 
3450 							(yyval.stmt) = (PLpgSQL_stmt *) new;
3451 						}
3452 						else if (tok == T_DATUM &&
3453 								 yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_VAR &&
3454 								 ((PLpgSQL_var *) yylval.wdatum.datum)->datatype->typoid == REFCURSOROID)
3455 						{
3456 							/* It's FOR var IN cursor */
3457 							PLpgSQL_stmt_forc	*new;
3458 							PLpgSQL_var			*cursor = (PLpgSQL_var *) yylval.wdatum.datum;
3459 
3460 							new = (PLpgSQL_stmt_forc *) palloc0(sizeof(PLpgSQL_stmt_forc));
3461 							new->cmd_type = PLPGSQL_STMT_FORC;
3462 							new->curvar = cursor->dno;
3463 
3464 							/* Should have had a single variable name */
3465 							if ((yyvsp[-1].forvariable).scalar && (yyvsp[-1].forvariable).row)
3466 								ereport(ERROR,
3467 										(errcode(ERRCODE_SYNTAX_ERROR),
3468 										 errmsg("cursor FOR loop must have only one target variable"),
3469 										 parser_errposition((yylsp[-1]))));
3470 
3471 							/* can't use an unbound cursor this way */
3472 							if (cursor->cursor_explicit_expr == NULL)
3473 								ereport(ERROR,
3474 										(errcode(ERRCODE_SYNTAX_ERROR),
3475 										 errmsg("cursor FOR loop must use a bound cursor variable"),
3476 										 parser_errposition(tokloc)));
3477 
3478 							/* collect cursor's parameters if any */
3479 							new->argquery = read_cursor_args(cursor,
3480 															 K_LOOP,
3481 															 "LOOP");
3482 
3483 							/* create loop's private RECORD variable */
3484 							new->var = (PLpgSQL_variable *)
3485 								plpgsql_build_record((yyvsp[-1].forvariable).name,
3486 													 (yyvsp[-1].forvariable).lineno,
3487 													 NULL,
3488 													 RECORDOID,
3489 													 true);
3490 
3491 							(yyval.stmt) = (PLpgSQL_stmt *) new;
3492 						}
3493 						else
3494 						{
3495 							PLpgSQL_expr	*expr1;
3496 							int				expr1loc;
3497 							bool			reverse = false;
3498 
3499 							/*
3500 							 * We have to distinguish between two
3501 							 * alternatives: FOR var IN a .. b and FOR
3502 							 * var IN query. Unfortunately this is
3503 							 * tricky, since the query in the second
3504 							 * form needn't start with a SELECT
3505 							 * keyword.  We use the ugly hack of
3506 							 * looking for two periods after the first
3507 							 * token. We also check for the REVERSE
3508 							 * keyword, which means it must be an
3509 							 * integer loop.
3510 							 */
3511 							if (tok_is_keyword(tok, &yylval,
3512 											   K_REVERSE, "reverse"))
3513 								reverse = true;
3514 							else
3515 								plpgsql_push_back_token(tok);
3516 
3517 							/*
3518 							 * Read tokens until we see either a ".."
3519 							 * or a LOOP. The text we read may not
3520 							 * necessarily be a well-formed SQL
3521 							 * statement, so we need to invoke
3522 							 * read_sql_construct directly.
3523 							 */
3524 							expr1 = read_sql_construct(DOT_DOT,
3525 													   K_LOOP,
3526 													   0,
3527 													   "LOOP",
3528 													   "SELECT ",
3529 													   true,
3530 													   false,
3531 													   true,
3532 													   &expr1loc,
3533 													   &tok);
3534 
3535 							if (tok == DOT_DOT)
3536 							{
3537 								/* Saw "..", so it must be an integer loop */
3538 								PLpgSQL_expr		*expr2;
3539 								PLpgSQL_expr		*expr_by;
3540 								PLpgSQL_var			*fvar;
3541 								PLpgSQL_stmt_fori	*new;
3542 
3543 								/* Check first expression is well-formed */
3544 								check_sql_expr(expr1->query, expr1loc, 7);
3545 
3546 								/* Read and check the second one */
3547 								expr2 = read_sql_expression2(K_LOOP, K_BY,
3548 															 "LOOP",
3549 															 &tok);
3550 
3551 								/* Get the BY clause if any */
3552 								if (tok == K_BY)
3553 									expr_by = read_sql_expression(K_LOOP,
3554 																  "LOOP");
3555 								else
3556 									expr_by = NULL;
3557 
3558 								/* Should have had a single variable name */
3559 								if ((yyvsp[-1].forvariable).scalar && (yyvsp[-1].forvariable).row)
3560 									ereport(ERROR,
3561 											(errcode(ERRCODE_SYNTAX_ERROR),
3562 											 errmsg("integer FOR loop must have only one target variable"),
3563 											 parser_errposition((yylsp[-1]))));
3564 
3565 								/* create loop's private variable */
3566 								fvar = (PLpgSQL_var *)
3567 									plpgsql_build_variable((yyvsp[-1].forvariable).name,
3568 														   (yyvsp[-1].forvariable).lineno,
3569 														   plpgsql_build_datatype(INT4OID,
3570 																				  -1,
3571 																				  InvalidOid,
3572 																				  NULL),
3573 														   true);
3574 
3575 								new = palloc0(sizeof(PLpgSQL_stmt_fori));
3576 								new->cmd_type = PLPGSQL_STMT_FORI;
3577 								new->var	  = fvar;
3578 								new->reverse  = reverse;
3579 								new->lower	  = expr1;
3580 								new->upper	  = expr2;
3581 								new->step	  = expr_by;
3582 
3583 								(yyval.stmt) = (PLpgSQL_stmt *) new;
3584 							}
3585 							else
3586 							{
3587 								/*
3588 								 * No "..", so it must be a query loop. We've
3589 								 * prefixed an extra SELECT to the query text,
3590 								 * so we need to remove that before performing
3591 								 * syntax checking.
3592 								 */
3593 								char				*tmp_query;
3594 								PLpgSQL_stmt_fors	*new;
3595 
3596 								if (reverse)
3597 									ereport(ERROR,
3598 											(errcode(ERRCODE_SYNTAX_ERROR),
3599 											 errmsg("cannot specify REVERSE in query FOR loop"),
3600 											 parser_errposition(tokloc)));
3601 
3602 								Assert(strncmp(expr1->query, "SELECT ", 7) == 0);
3603 								tmp_query = pstrdup(expr1->query + 7);
3604 								pfree(expr1->query);
3605 								expr1->query = tmp_query;
3606 
3607 								check_sql_expr(expr1->query, expr1loc, 0);
3608 
3609 								new = palloc0(sizeof(PLpgSQL_stmt_fors));
3610 								new->cmd_type = PLPGSQL_STMT_FORS;
3611 								if ((yyvsp[-1].forvariable).row)
3612 								{
3613 									new->var = (PLpgSQL_variable *) (yyvsp[-1].forvariable).row;
3614 									check_assignable((yyvsp[-1].forvariable).row, (yylsp[-1]));
3615 								}
3616 								else if ((yyvsp[-1].forvariable).scalar)
3617 								{
3618 									/* convert single scalar to list */
3619 									new->var = (PLpgSQL_variable *)
3620 										make_scalar_list1((yyvsp[-1].forvariable).name, (yyvsp[-1].forvariable).scalar,
3621 														  (yyvsp[-1].forvariable).lineno, (yylsp[-1]));
3622 									/* make_scalar_list1 did check_assignable */
3623 								}
3624 								else
3625 								{
3626 									ereport(ERROR,
3627 											(errcode(ERRCODE_SYNTAX_ERROR),
3628 											 errmsg("loop variable of loop over rows must be a record variable or list of scalar variables"),
3629 											 parser_errposition((yylsp[-1]))));
3630 								}
3631 
3632 								new->query = expr1;
3633 								(yyval.stmt) = (PLpgSQL_stmt *) new;
3634 							}
3635 						}
3636 					}
3637 #line 3638 "pl_gram.c" /* yacc.c:1652  */
3638     break;
3639 
3640   case 119:
3641 #line 1583 "pl_gram.y" /* yacc.c:1652  */
3642     {
3643 						(yyval.forvariable).name = NameOfDatum(&((yyvsp[0].wdatum)));
3644 						(yyval.forvariable).lineno = plpgsql_location_to_lineno((yylsp[0]));
3645 						if ((yyvsp[0].wdatum).datum->dtype == PLPGSQL_DTYPE_ROW ||
3646 							(yyvsp[0].wdatum).datum->dtype == PLPGSQL_DTYPE_REC)
3647 						{
3648 							(yyval.forvariable).scalar = NULL;
3649 							(yyval.forvariable).row = (yyvsp[0].wdatum).datum;
3650 						}
3651 						else
3652 						{
3653 							int			tok;
3654 
3655 							(yyval.forvariable).scalar = (yyvsp[0].wdatum).datum;
3656 							(yyval.forvariable).row = NULL;
3657 							/* check for comma-separated list */
3658 							tok = yylex();
3659 							plpgsql_push_back_token(tok);
3660 							if (tok == ',')
3661 								(yyval.forvariable).row = (PLpgSQL_datum *)
3662 									read_into_scalar_list((yyval.forvariable).name,
3663 														  (yyval.forvariable).scalar,
3664 														  (yylsp[0]));
3665 						}
3666 					}
3667 #line 3668 "pl_gram.c" /* yacc.c:1652  */
3668     break;
3669 
3670   case 120:
3671 #line 1609 "pl_gram.y" /* yacc.c:1652  */
3672     {
3673 						int			tok;
3674 
3675 						(yyval.forvariable).name = (yyvsp[0].word).ident;
3676 						(yyval.forvariable).lineno = plpgsql_location_to_lineno((yylsp[0]));
3677 						(yyval.forvariable).scalar = NULL;
3678 						(yyval.forvariable).row = NULL;
3679 						/* check for comma-separated list */
3680 						tok = yylex();
3681 						plpgsql_push_back_token(tok);
3682 						if (tok == ',')
3683 							word_is_not_variable(&((yyvsp[0].word)), (yylsp[0]));
3684 					}
3685 #line 3686 "pl_gram.c" /* yacc.c:1652  */
3686     break;
3687 
3688   case 121:
3689 #line 1623 "pl_gram.y" /* yacc.c:1652  */
3690     {
3691 						/* just to give a better message than "syntax error" */
3692 						cword_is_not_variable(&((yyvsp[0].cword)), (yylsp[0]));
3693 					}
3694 #line 3695 "pl_gram.c" /* yacc.c:1652  */
3695     break;
3696 
3697   case 122:
3698 #line 1630 "pl_gram.y" /* yacc.c:1652  */
3699     {
3700 						PLpgSQL_stmt_foreach_a *new;
3701 
3702 						new = palloc0(sizeof(PLpgSQL_stmt_foreach_a));
3703 						new->cmd_type = PLPGSQL_STMT_FOREACH_A;
3704 						new->lineno = plpgsql_location_to_lineno((yylsp[-6]));
3705 						new->label = (yyvsp[-7].str);
3706 						new->slice = (yyvsp[-4].ival);
3707 						new->expr = (yyvsp[-1].expr);
3708 						new->body = (yyvsp[0].loop_body).stmts;
3709 
3710 						if ((yyvsp[-5].forvariable).row)
3711 						{
3712 							new->varno = (yyvsp[-5].forvariable).row->dno;
3713 							check_assignable((yyvsp[-5].forvariable).row, (yylsp[-5]));
3714 						}
3715 						else if ((yyvsp[-5].forvariable).scalar)
3716 						{
3717 							new->varno = (yyvsp[-5].forvariable).scalar->dno;
3718 							check_assignable((yyvsp[-5].forvariable).scalar, (yylsp[-5]));
3719 						}
3720 						else
3721 						{
3722 							ereport(ERROR,
3723 									(errcode(ERRCODE_SYNTAX_ERROR),
3724 									 errmsg("loop variable of FOREACH must be a known variable or list of variables"),
3725 											 parser_errposition((yylsp[-5]))));
3726 						}
3727 
3728 						check_labels((yyvsp[-7].str), (yyvsp[0].loop_body).end_label, (yyvsp[0].loop_body).end_label_location);
3729 						plpgsql_ns_pop();
3730 
3731 						(yyval.stmt) = (PLpgSQL_stmt *) new;
3732 					}
3733 #line 3734 "pl_gram.c" /* yacc.c:1652  */
3734     break;
3735 
3736   case 123:
3737 #line 1667 "pl_gram.y" /* yacc.c:1652  */
3738     {
3739 						(yyval.ival) = 0;
3740 					}
3741 #line 3742 "pl_gram.c" /* yacc.c:1652  */
3742     break;
3743 
3744   case 124:
3745 #line 1671 "pl_gram.y" /* yacc.c:1652  */
3746     {
3747 						(yyval.ival) = (yyvsp[0].ival);
3748 					}
3749 #line 3750 "pl_gram.c" /* yacc.c:1652  */
3750     break;
3751 
3752   case 125:
3753 #line 1677 "pl_gram.y" /* yacc.c:1652  */
3754     {
3755 						PLpgSQL_stmt_exit *new;
3756 
3757 						new = palloc0(sizeof(PLpgSQL_stmt_exit));
3758 						new->cmd_type = PLPGSQL_STMT_EXIT;
3759 						new->is_exit  = (yyvsp[-2].boolean);
3760 						new->lineno	  = plpgsql_location_to_lineno((yylsp[-2]));
3761 						new->label	  = (yyvsp[-1].str);
3762 						new->cond	  = (yyvsp[0].expr);
3763 
3764 						if ((yyvsp[-1].str))
3765 						{
3766 							/* We have a label, so verify it exists */
3767 							PLpgSQL_nsitem *label;
3768 
3769 							label = plpgsql_ns_lookup_label(plpgsql_ns_top(), (yyvsp[-1].str));
3770 							if (label == NULL)
3771 								ereport(ERROR,
3772 										(errcode(ERRCODE_SYNTAX_ERROR),
3773 										 errmsg("there is no label \"%s\" "
3774 												"attached to any block or loop enclosing this statement",
3775 												(yyvsp[-1].str)),
3776 										 parser_errposition((yylsp[-1]))));
3777 							/* CONTINUE only allows loop labels */
3778 							if (label->itemno != PLPGSQL_LABEL_LOOP && !new->is_exit)
3779 								ereport(ERROR,
3780 										(errcode(ERRCODE_SYNTAX_ERROR),
3781 										 errmsg("block label \"%s\" cannot be used in CONTINUE",
3782 												(yyvsp[-1].str)),
3783 										 parser_errposition((yylsp[-1]))));
3784 						}
3785 						else
3786 						{
3787 							/*
3788 							 * No label, so make sure there is some loop (an
3789 							 * unlabelled EXIT does not match a block, so this
3790 							 * is the same test for both EXIT and CONTINUE)
3791 							 */
3792 							if (plpgsql_ns_find_nearest_loop(plpgsql_ns_top()) == NULL)
3793 								ereport(ERROR,
3794 										(errcode(ERRCODE_SYNTAX_ERROR),
3795 										 new->is_exit ?
3796 										 errmsg("EXIT cannot be used outside a loop, unless it has a label") :
3797 										 errmsg("CONTINUE cannot be used outside a loop"),
3798 										 parser_errposition((yylsp[-2]))));
3799 						}
3800 
3801 						(yyval.stmt) = (PLpgSQL_stmt *)new;
3802 					}
3803 #line 3804 "pl_gram.c" /* yacc.c:1652  */
3804     break;
3805 
3806   case 126:
3807 #line 1729 "pl_gram.y" /* yacc.c:1652  */
3808     {
3809 						(yyval.boolean) = true;
3810 					}
3811 #line 3812 "pl_gram.c" /* yacc.c:1652  */
3812     break;
3813 
3814   case 127:
3815 #line 1733 "pl_gram.y" /* yacc.c:1652  */
3816     {
3817 						(yyval.boolean) = false;
3818 					}
3819 #line 3820 "pl_gram.c" /* yacc.c:1652  */
3820     break;
3821 
3822   case 128:
3823 #line 1739 "pl_gram.y" /* yacc.c:1652  */
3824     {
3825 						int	tok;
3826 
3827 						tok = yylex();
3828 						if (tok == 0)
3829 							yyerror("unexpected end of function definition");
3830 
3831 						if (tok_is_keyword(tok, &yylval,
3832 										   K_NEXT, "next"))
3833 						{
3834 							(yyval.stmt) = make_return_next_stmt((yylsp[0]));
3835 						}
3836 						else if (tok_is_keyword(tok, &yylval,
3837 												K_QUERY, "query"))
3838 						{
3839 							(yyval.stmt) = make_return_query_stmt((yylsp[0]));
3840 						}
3841 						else
3842 						{
3843 							plpgsql_push_back_token(tok);
3844 							(yyval.stmt) = make_return_stmt((yylsp[0]));
3845 						}
3846 					}
3847 #line 3848 "pl_gram.c" /* yacc.c:1652  */
3848     break;
3849 
3850   case 129:
3851 #line 1765 "pl_gram.y" /* yacc.c:1652  */
3852     {
3853 						PLpgSQL_stmt_raise		*new;
3854 						int	tok;
3855 
3856 						new = palloc(sizeof(PLpgSQL_stmt_raise));
3857 
3858 						new->cmd_type	= PLPGSQL_STMT_RAISE;
3859 						new->lineno		= plpgsql_location_to_lineno((yylsp[0]));
3860 						new->elog_level = ERROR;	/* default */
3861 						new->condname	= NULL;
3862 						new->message	= NULL;
3863 						new->params		= NIL;
3864 						new->options	= NIL;
3865 
3866 						tok = yylex();
3867 						if (tok == 0)
3868 							yyerror("unexpected end of function definition");
3869 
3870 						/*
3871 						 * We could have just RAISE, meaning to re-throw
3872 						 * the current error.
3873 						 */
3874 						if (tok != ';')
3875 						{
3876 							/*
3877 							 * First is an optional elog severity level.
3878 							 */
3879 							if (tok_is_keyword(tok, &yylval,
3880 											   K_EXCEPTION, "exception"))
3881 							{
3882 								new->elog_level = ERROR;
3883 								tok = yylex();
3884 							}
3885 							else if (tok_is_keyword(tok, &yylval,
3886 													K_WARNING, "warning"))
3887 							{
3888 								new->elog_level = WARNING;
3889 								tok = yylex();
3890 							}
3891 							else if (tok_is_keyword(tok, &yylval,
3892 													K_NOTICE, "notice"))
3893 							{
3894 								new->elog_level = NOTICE;
3895 								tok = yylex();
3896 							}
3897 							else if (tok_is_keyword(tok, &yylval,
3898 													K_INFO, "info"))
3899 							{
3900 								new->elog_level = INFO;
3901 								tok = yylex();
3902 							}
3903 							else if (tok_is_keyword(tok, &yylval,
3904 													K_LOG, "log"))
3905 							{
3906 								new->elog_level = LOG;
3907 								tok = yylex();
3908 							}
3909 							else if (tok_is_keyword(tok, &yylval,
3910 													K_DEBUG, "debug"))
3911 							{
3912 								new->elog_level = DEBUG1;
3913 								tok = yylex();
3914 							}
3915 							if (tok == 0)
3916 								yyerror("unexpected end of function definition");
3917 
3918 							/*
3919 							 * Next we can have a condition name, or
3920 							 * equivalently SQLSTATE 'xxxxx', or a string
3921 							 * literal that is the old-style message format,
3922 							 * or USING to start the option list immediately.
3923 							 */
3924 							if (tok == SCONST)
3925 							{
3926 								/* old style message and parameters */
3927 								new->message = yylval.str;
3928 								/*
3929 								 * We expect either a semi-colon, which
3930 								 * indicates no parameters, or a comma that
3931 								 * begins the list of parameter expressions,
3932 								 * or USING to begin the options list.
3933 								 */
3934 								tok = yylex();
3935 								if (tok != ',' && tok != ';' && tok != K_USING)
3936 									yyerror("syntax error");
3937 
3938 								while (tok == ',')
3939 								{
3940 									PLpgSQL_expr *expr;
3941 
3942 									expr = read_sql_construct(',', ';', K_USING,
3943 															  ", or ; or USING",
3944 															  "SELECT ",
3945 															  true, true, true,
3946 															  NULL, &tok);
3947 									new->params = lappend(new->params, expr);
3948 								}
3949 							}
3950 							else if (tok != K_USING)
3951 							{
3952 								/* must be condition name or SQLSTATE */
3953 								if (tok_is_keyword(tok, &yylval,
3954 												   K_SQLSTATE, "sqlstate"))
3955 								{
3956 									/* next token should be a string literal */
3957 									char   *sqlstatestr;
3958 
3959 									if (yylex() != SCONST)
3960 										yyerror("syntax error");
3961 									sqlstatestr = yylval.str;
3962 
3963 									if (strlen(sqlstatestr) != 5)
3964 										yyerror("invalid SQLSTATE code");
3965 									if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
3966 										yyerror("invalid SQLSTATE code");
3967 									new->condname = sqlstatestr;
3968 								}
3969 								else
3970 								{
3971 									if (tok == T_WORD)
3972 										new->condname = yylval.word.ident;
3973 									else if (plpgsql_token_is_unreserved_keyword(tok))
3974 										new->condname = pstrdup(yylval.keyword);
3975 									else
3976 										yyerror("syntax error");
3977 									plpgsql_recognize_err_condition(new->condname,
3978 																	false);
3979 								}
3980 								tok = yylex();
3981 								if (tok != ';' && tok != K_USING)
3982 									yyerror("syntax error");
3983 							}
3984 
3985 							if (tok == K_USING)
3986 								new->options = read_raise_options();
3987 						}
3988 
3989 						check_raise_parameters(new);
3990 
3991 						(yyval.stmt) = (PLpgSQL_stmt *)new;
3992 					}
3993 #line 3994 "pl_gram.c" /* yacc.c:1652  */
3994     break;
3995 
3996   case 130:
3997 #line 1909 "pl_gram.y" /* yacc.c:1652  */
3998     {
3999 						PLpgSQL_stmt_assert		*new;
4000 						int	tok;
4001 
4002 						new = palloc(sizeof(PLpgSQL_stmt_assert));
4003 
4004 						new->cmd_type	= PLPGSQL_STMT_ASSERT;
4005 						new->lineno		= plpgsql_location_to_lineno((yylsp[0]));
4006 
4007 						new->cond = read_sql_expression2(',', ';',
4008 														 ", or ;",
4009 														 &tok);
4010 
4011 						if (tok == ',')
4012 							new->message = read_sql_expression(';', ";");
4013 						else
4014 							new->message = NULL;
4015 
4016 						(yyval.stmt) = (PLpgSQL_stmt *) new;
4017 					}
4018 #line 4019 "pl_gram.c" /* yacc.c:1652  */
4019     break;
4020 
4021   case 131:
4022 #line 1932 "pl_gram.y" /* yacc.c:1652  */
4023     {
4024 						(yyval.loop_body).stmts = (yyvsp[-4].list);
4025 						(yyval.loop_body).end_label = (yyvsp[-1].str);
4026 						(yyval.loop_body).end_label_location = (yylsp[-1]);
4027 					}
4028 #line 4029 "pl_gram.c" /* yacc.c:1652  */
4029     break;
4030 
4031   case 132:
4032 #line 1950 "pl_gram.y" /* yacc.c:1652  */
4033     {
4034 						(yyval.stmt) = make_execsql_stmt(K_IMPORT, (yylsp[0]));
4035 					}
4036 #line 4037 "pl_gram.c" /* yacc.c:1652  */
4037     break;
4038 
4039   case 133:
4040 #line 1954 "pl_gram.y" /* yacc.c:1652  */
4041     {
4042 						(yyval.stmt) = make_execsql_stmt(K_INSERT, (yylsp[0]));
4043 					}
4044 #line 4045 "pl_gram.c" /* yacc.c:1652  */
4045     break;
4046 
4047   case 134:
4048 #line 1958 "pl_gram.y" /* yacc.c:1652  */
4049     {
4050 						int			tok;
4051 
4052 						tok = yylex();
4053 						plpgsql_push_back_token(tok);
4054 						if (tok == '=' || tok == COLON_EQUALS || tok == '[')
4055 							word_is_not_variable(&((yyvsp[0].word)), (yylsp[0]));
4056 						(yyval.stmt) = make_execsql_stmt(T_WORD, (yylsp[0]));
4057 					}
4058 #line 4059 "pl_gram.c" /* yacc.c:1652  */
4059     break;
4060 
4061   case 135:
4062 #line 1968 "pl_gram.y" /* yacc.c:1652  */
4063     {
4064 						int			tok;
4065 
4066 						tok = yylex();
4067 						plpgsql_push_back_token(tok);
4068 						if (tok == '=' || tok == COLON_EQUALS || tok == '[')
4069 							cword_is_not_variable(&((yyvsp[0].cword)), (yylsp[0]));
4070 						(yyval.stmt) = make_execsql_stmt(T_CWORD, (yylsp[0]));
4071 					}
4072 #line 4073 "pl_gram.c" /* yacc.c:1652  */
4073     break;
4074 
4075   case 136:
4076 #line 1980 "pl_gram.y" /* yacc.c:1652  */
4077     {
4078 						PLpgSQL_stmt_dynexecute *new;
4079 						PLpgSQL_expr *expr;
4080 						int endtoken;
4081 
4082 						expr = read_sql_construct(K_INTO, K_USING, ';',
4083 												  "INTO or USING or ;",
4084 												  "SELECT ",
4085 												  true, true, true,
4086 												  NULL, &endtoken);
4087 
4088 						new = palloc(sizeof(PLpgSQL_stmt_dynexecute));
4089 						new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
4090 						new->lineno = plpgsql_location_to_lineno((yylsp[0]));
4091 						new->query = expr;
4092 						new->into = false;
4093 						new->strict = false;
4094 						new->target = NULL;
4095 						new->params = NIL;
4096 
4097 						/*
4098 						 * We loop to allow the INTO and USING clauses to
4099 						 * appear in either order, since people easily get
4100 						 * that wrong.  This coding also prevents "INTO foo"
4101 						 * from getting absorbed into a USING expression,
4102 						 * which is *really* confusing.
4103 						 */
4104 						for (;;)
4105 						{
4106 							if (endtoken == K_INTO)
4107 							{
4108 								if (new->into)			/* multiple INTO */
4109 									yyerror("syntax error");
4110 								new->into = true;
4111 								read_into_target(&new->target, &new->strict);
4112 								endtoken = yylex();
4113 							}
4114 							else if (endtoken == K_USING)
4115 							{
4116 								if (new->params)		/* multiple USING */
4117 									yyerror("syntax error");
4118 								do
4119 								{
4120 									expr = read_sql_construct(',', ';', K_INTO,
4121 															  ", or ; or INTO",
4122 															  "SELECT ",
4123 															  true, true, true,
4124 															  NULL, &endtoken);
4125 									new->params = lappend(new->params, expr);
4126 								} while (endtoken == ',');
4127 							}
4128 							else if (endtoken == ';')
4129 								break;
4130 							else
4131 								yyerror("syntax error");
4132 						}
4133 
4134 						(yyval.stmt) = (PLpgSQL_stmt *)new;
4135 					}
4136 #line 4137 "pl_gram.c" /* yacc.c:1652  */
4137     break;
4138 
4139   case 137:
4140 #line 2043 "pl_gram.y" /* yacc.c:1652  */
4141     {
4142 						PLpgSQL_stmt_open *new;
4143 						int				  tok;
4144 
4145 						new = palloc0(sizeof(PLpgSQL_stmt_open));
4146 						new->cmd_type = PLPGSQL_STMT_OPEN;
4147 						new->lineno = plpgsql_location_to_lineno((yylsp[-1]));
4148 						new->curvar = (yyvsp[0].var)->dno;
4149 						new->cursor_options = CURSOR_OPT_FAST_PLAN;
4150 
4151 						if ((yyvsp[0].var)->cursor_explicit_expr == NULL)
4152 						{
4153 							/* be nice if we could use opt_scrollable here */
4154 							tok = yylex();
4155 							if (tok_is_keyword(tok, &yylval,
4156 											   K_NO, "no"))
4157 							{
4158 								tok = yylex();
4159 								if (tok_is_keyword(tok, &yylval,
4160 												   K_SCROLL, "scroll"))
4161 								{
4162 									new->cursor_options |= CURSOR_OPT_NO_SCROLL;
4163 									tok = yylex();
4164 								}
4165 							}
4166 							else if (tok_is_keyword(tok, &yylval,
4167 													K_SCROLL, "scroll"))
4168 							{
4169 								new->cursor_options |= CURSOR_OPT_SCROLL;
4170 								tok = yylex();
4171 							}
4172 
4173 							if (tok != K_FOR)
4174 								yyerror("syntax error, expected \"FOR\"");
4175 
4176 							tok = yylex();
4177 							if (tok == K_EXECUTE)
4178 							{
4179 								int		endtoken;
4180 
4181 								new->dynquery =
4182 									read_sql_expression2(K_USING, ';',
4183 														 "USING or ;",
4184 														 &endtoken);
4185 
4186 								/* If we found "USING", collect argument(s) */
4187 								if (endtoken == K_USING)
4188 								{
4189 									PLpgSQL_expr *expr;
4190 
4191 									do
4192 									{
4193 										expr = read_sql_expression2(',', ';',
4194 																	", or ;",
4195 																	&endtoken);
4196 										new->params = lappend(new->params,
4197 															  expr);
4198 									} while (endtoken == ',');
4199 								}
4200 							}
4201 							else
4202 							{
4203 								plpgsql_push_back_token(tok);
4204 								new->query = read_sql_stmt("");
4205 							}
4206 						}
4207 						else
4208 						{
4209 							/* predefined cursor query, so read args */
4210 							new->argquery = read_cursor_args((yyvsp[0].var), ';', ";");
4211 						}
4212 
4213 						(yyval.stmt) = (PLpgSQL_stmt *)new;
4214 					}
4215 #line 4216 "pl_gram.c" /* yacc.c:1652  */
4216     break;
4217 
4218   case 138:
4219 #line 2120 "pl_gram.y" /* yacc.c:1652  */
4220     {
4221 						PLpgSQL_stmt_fetch *fetch = (yyvsp[-2].fetch);
4222 						PLpgSQL_variable *target;
4223 
4224 						/* We have already parsed everything through the INTO keyword */
4225 						read_into_target(&target, NULL);
4226 
4227 						if (yylex() != ';')
4228 							yyerror("syntax error");
4229 
4230 						/*
4231 						 * We don't allow multiple rows in PL/pgSQL's FETCH
4232 						 * statement, only in MOVE.
4233 						 */
4234 						if (fetch->returns_multiple_rows)
4235 							ereport(ERROR,
4236 									(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4237 									 errmsg("FETCH statement cannot return multiple rows"),
4238 									 parser_errposition((yylsp[-3]))));
4239 
4240 						fetch->lineno = plpgsql_location_to_lineno((yylsp[-3]));
4241 						fetch->target	= target;
4242 						fetch->curvar	= (yyvsp[-1].var)->dno;
4243 						fetch->is_move	= false;
4244 
4245 						(yyval.stmt) = (PLpgSQL_stmt *)fetch;
4246 					}
4247 #line 4248 "pl_gram.c" /* yacc.c:1652  */
4248     break;
4249 
4250   case 139:
4251 #line 2150 "pl_gram.y" /* yacc.c:1652  */
4252     {
4253 						PLpgSQL_stmt_fetch *fetch = (yyvsp[-2].fetch);
4254 
4255 						fetch->lineno = plpgsql_location_to_lineno((yylsp[-3]));
4256 						fetch->curvar	= (yyvsp[-1].var)->dno;
4257 						fetch->is_move	= true;
4258 
4259 						(yyval.stmt) = (PLpgSQL_stmt *)fetch;
4260 					}
4261 #line 4262 "pl_gram.c" /* yacc.c:1652  */
4262     break;
4263 
4264   case 140:
4265 #line 2162 "pl_gram.y" /* yacc.c:1652  */
4266     {
4267 						(yyval.fetch) = read_fetch_direction();
4268 					}
4269 #line 4270 "pl_gram.c" /* yacc.c:1652  */
4270     break;
4271 
4272   case 141:
4273 #line 2168 "pl_gram.y" /* yacc.c:1652  */
4274     {
4275 						PLpgSQL_stmt_close *new;
4276 
4277 						new = palloc(sizeof(PLpgSQL_stmt_close));
4278 						new->cmd_type = PLPGSQL_STMT_CLOSE;
4279 						new->lineno = plpgsql_location_to_lineno((yylsp[-2]));
4280 						new->curvar = (yyvsp[-1].var)->dno;
4281 
4282 						(yyval.stmt) = (PLpgSQL_stmt *)new;
4283 					}
4284 #line 4285 "pl_gram.c" /* yacc.c:1652  */
4285     break;
4286 
4287   case 142:
4288 #line 2181 "pl_gram.y" /* yacc.c:1652  */
4289     {
4290 						/* We do not bother building a node for NULL */
4291 						(yyval.stmt) = NULL;
4292 					}
4293 #line 4294 "pl_gram.c" /* yacc.c:1652  */
4294     break;
4295 
4296   case 143:
4297 #line 2188 "pl_gram.y" /* yacc.c:1652  */
4298     {
4299 						PLpgSQL_stmt_commit *new;
4300 
4301 						new = palloc(sizeof(PLpgSQL_stmt_commit));
4302 						new->cmd_type = PLPGSQL_STMT_COMMIT;
4303 						new->lineno = plpgsql_location_to_lineno((yylsp[-1]));
4304 
4305 						(yyval.stmt) = (PLpgSQL_stmt *)new;
4306 					}
4307 #line 4308 "pl_gram.c" /* yacc.c:1652  */
4308     break;
4309 
4310   case 144:
4311 #line 2200 "pl_gram.y" /* yacc.c:1652  */
4312     {
4313 						PLpgSQL_stmt_rollback *new;
4314 
4315 						new = palloc(sizeof(PLpgSQL_stmt_rollback));
4316 						new->cmd_type = PLPGSQL_STMT_ROLLBACK;
4317 						new->lineno = plpgsql_location_to_lineno((yylsp[-1]));
4318 
4319 						(yyval.stmt) = (PLpgSQL_stmt *)new;
4320 					}
4321 #line 4322 "pl_gram.c" /* yacc.c:1652  */
4322     break;
4323 
4324   case 145:
4325 #line 2212 "pl_gram.y" /* yacc.c:1652  */
4326     {
4327 						PLpgSQL_stmt_set *new;
4328 
4329 						new = palloc0(sizeof(PLpgSQL_stmt_set));
4330 						new->cmd_type = PLPGSQL_STMT_SET;
4331 						new->lineno = plpgsql_location_to_lineno((yylsp[0]));
4332 						new->expr = read_sql_stmt("SET ");
4333 
4334 						(yyval.stmt) = (PLpgSQL_stmt *)new;
4335 					}
4336 #line 4337 "pl_gram.c" /* yacc.c:1652  */
4337     break;
4338 
4339   case 146:
4340 #line 2223 "pl_gram.y" /* yacc.c:1652  */
4341     {
4342 						PLpgSQL_stmt_set *new;
4343 
4344 						new = palloc0(sizeof(PLpgSQL_stmt_set));
4345 						new->cmd_type = PLPGSQL_STMT_SET;
4346 						new->lineno = plpgsql_location_to_lineno((yylsp[0]));
4347 						new->expr = read_sql_stmt("RESET ");
4348 
4349 						(yyval.stmt) = (PLpgSQL_stmt *)new;
4350 					}
4351 #line 4352 "pl_gram.c" /* yacc.c:1652  */
4352     break;
4353 
4354   case 147:
4355 #line 2237 "pl_gram.y" /* yacc.c:1652  */
4356     {
4357 						/*
4358 						 * In principle we should support a cursor_variable
4359 						 * that is an array element, but for now we don't, so
4360 						 * just throw an error if next token is '['.
4361 						 */
4362 						if ((yyvsp[0].wdatum).datum->dtype != PLPGSQL_DTYPE_VAR ||
4363 							plpgsql_peek() == '[')
4364 							ereport(ERROR,
4365 									(errcode(ERRCODE_DATATYPE_MISMATCH),
4366 									 errmsg("cursor variable must be a simple variable"),
4367 									 parser_errposition((yylsp[0]))));
4368 
4369 						if (((PLpgSQL_var *) (yyvsp[0].wdatum).datum)->datatype->typoid != REFCURSOROID)
4370 							ereport(ERROR,
4371 									(errcode(ERRCODE_DATATYPE_MISMATCH),
4372 									 errmsg("variable \"%s\" must be of type cursor or refcursor",
4373 											((PLpgSQL_var *) (yyvsp[0].wdatum).datum)->refname),
4374 									 parser_errposition((yylsp[0]))));
4375 						(yyval.var) = (PLpgSQL_var *) (yyvsp[0].wdatum).datum;
4376 					}
4377 #line 4378 "pl_gram.c" /* yacc.c:1652  */
4378     break;
4379 
4380   case 148:
4381 #line 2259 "pl_gram.y" /* yacc.c:1652  */
4382     {
4383 						/* just to give a better message than "syntax error" */
4384 						word_is_not_variable(&((yyvsp[0].word)), (yylsp[0]));
4385 					}
4386 #line 4387 "pl_gram.c" /* yacc.c:1652  */
4387     break;
4388 
4389   case 149:
4390 #line 2264 "pl_gram.y" /* yacc.c:1652  */
4391     {
4392 						/* just to give a better message than "syntax error" */
4393 						cword_is_not_variable(&((yyvsp[0].cword)), (yylsp[0]));
4394 					}
4395 #line 4396 "pl_gram.c" /* yacc.c:1652  */
4396     break;
4397 
4398   case 150:
4399 #line 2271 "pl_gram.y" /* yacc.c:1652  */
4400     { (yyval.exception_block) = NULL; }
4401 #line 4402 "pl_gram.c" /* yacc.c:1652  */
4402     break;
4403 
4404   case 151:
4405 #line 2273 "pl_gram.y" /* yacc.c:1652  */
4406     {
4407 						/*
4408 						 * We use a mid-rule action to add these
4409 						 * special variables to the namespace before
4410 						 * parsing the WHEN clauses themselves.  The
4411 						 * scope of the names extends to the end of the
4412 						 * current block.
4413 						 */
4414 						int			lineno = plpgsql_location_to_lineno((yylsp[0]));
4415 						PLpgSQL_exception_block *new = palloc(sizeof(PLpgSQL_exception_block));
4416 						PLpgSQL_variable *var;
4417 
4418 						var = plpgsql_build_variable("sqlstate", lineno,
4419 													 plpgsql_build_datatype(TEXTOID,
4420 																			-1,
4421 																			plpgsql_curr_compile->fn_input_collation,
4422 																			NULL),
4423 													 true);
4424 						var->isconst = true;
4425 						new->sqlstate_varno = var->dno;
4426 
4427 						var = plpgsql_build_variable("sqlerrm", lineno,
4428 													 plpgsql_build_datatype(TEXTOID,
4429 																			-1,
4430 																			plpgsql_curr_compile->fn_input_collation,
4431 																			NULL),
4432 													 true);
4433 						var->isconst = true;
4434 						new->sqlerrm_varno = var->dno;
4435 
4436 						(yyval.exception_block) = new;
4437 					}
4438 #line 4439 "pl_gram.c" /* yacc.c:1652  */
4439     break;
4440 
4441   case 152:
4442 #line 2306 "pl_gram.y" /* yacc.c:1652  */
4443     {
4444 						PLpgSQL_exception_block *new = (yyvsp[-1].exception_block);
4445 						new->exc_list = (yyvsp[0].list);
4446 
4447 						(yyval.exception_block) = new;
4448 					}
4449 #line 4450 "pl_gram.c" /* yacc.c:1652  */
4450     break;
4451 
4452   case 153:
4453 #line 2315 "pl_gram.y" /* yacc.c:1652  */
4454     {
4455 							(yyval.list) = lappend((yyvsp[-1].list), (yyvsp[0].exception));
4456 						}
4457 #line 4458 "pl_gram.c" /* yacc.c:1652  */
4458     break;
4459 
4460   case 154:
4461 #line 2319 "pl_gram.y" /* yacc.c:1652  */
4462     {
4463 							(yyval.list) = list_make1((yyvsp[0].exception));
4464 						}
4465 #line 4466 "pl_gram.c" /* yacc.c:1652  */
4466     break;
4467 
4468   case 155:
4469 #line 2325 "pl_gram.y" /* yacc.c:1652  */
4470     {
4471 						PLpgSQL_exception *new;
4472 
4473 						new = palloc0(sizeof(PLpgSQL_exception));
4474 						new->lineno = plpgsql_location_to_lineno((yylsp[-3]));
4475 						new->conditions = (yyvsp[-2].condition);
4476 						new->action = (yyvsp[0].list);
4477 
4478 						(yyval.exception) = new;
4479 					}
4480 #line 4481 "pl_gram.c" /* yacc.c:1652  */
4481     break;
4482 
4483   case 156:
4484 #line 2338 "pl_gram.y" /* yacc.c:1652  */
4485     {
4486 							PLpgSQL_condition	*old;
4487 
4488 							for (old = (yyvsp[-2].condition); old->next != NULL; old = old->next)
4489 								/* skip */ ;
4490 							old->next = (yyvsp[0].condition);
4491 							(yyval.condition) = (yyvsp[-2].condition);
4492 						}
4493 #line 4494 "pl_gram.c" /* yacc.c:1652  */
4494     break;
4495 
4496   case 157:
4497 #line 2347 "pl_gram.y" /* yacc.c:1652  */
4498     {
4499 							(yyval.condition) = (yyvsp[0].condition);
4500 						}
4501 #line 4502 "pl_gram.c" /* yacc.c:1652  */
4502     break;
4503 
4504   case 158:
4505 #line 2353 "pl_gram.y" /* yacc.c:1652  */
4506     {
4507 							if (strcmp((yyvsp[0].str), "sqlstate") != 0)
4508 							{
4509 								(yyval.condition) = plpgsql_parse_err_condition((yyvsp[0].str));
4510 							}
4511 							else
4512 							{
4513 								PLpgSQL_condition *new;
4514 								char   *sqlstatestr;
4515 
4516 								/* next token should be a string literal */
4517 								if (yylex() != SCONST)
4518 									yyerror("syntax error");
4519 								sqlstatestr = yylval.str;
4520 
4521 								if (strlen(sqlstatestr) != 5)
4522 									yyerror("invalid SQLSTATE code");
4523 								if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
4524 									yyerror("invalid SQLSTATE code");
4525 
4526 								new = palloc(sizeof(PLpgSQL_condition));
4527 								new->sqlerrstate =
4528 									MAKE_SQLSTATE(sqlstatestr[0],
4529 												  sqlstatestr[1],
4530 												  sqlstatestr[2],
4531 												  sqlstatestr[3],
4532 												  sqlstatestr[4]);
4533 								new->condname = sqlstatestr;
4534 								new->next = NULL;
4535 
4536 								(yyval.condition) = new;
4537 							}
4538 						}
4539 #line 4540 "pl_gram.c" /* yacc.c:1652  */
4540     break;
4541 
4542   case 159:
4543 #line 2389 "pl_gram.y" /* yacc.c:1652  */
4544     { (yyval.expr) = read_sql_expression(';', ";"); }
4545 #line 4546 "pl_gram.c" /* yacc.c:1652  */
4546     break;
4547 
4548   case 160:
4549 #line 2393 "pl_gram.y" /* yacc.c:1652  */
4550     { (yyval.expr) = read_sql_expression(']', "]"); }
4551 #line 4552 "pl_gram.c" /* yacc.c:1652  */
4552     break;
4553 
4554   case 161:
4555 #line 2397 "pl_gram.y" /* yacc.c:1652  */
4556     { (yyval.expr) = read_sql_expression(K_THEN, "THEN"); }
4557 #line 4558 "pl_gram.c" /* yacc.c:1652  */
4558     break;
4559 
4560   case 162:
4561 #line 2401 "pl_gram.y" /* yacc.c:1652  */
4562     { (yyval.expr) = read_sql_expression(K_LOOP, "LOOP"); }
4563 #line 4564 "pl_gram.c" /* yacc.c:1652  */
4564     break;
4565 
4566   case 163:
4567 #line 2405 "pl_gram.y" /* yacc.c:1652  */
4568     {
4569 						plpgsql_ns_push(NULL, PLPGSQL_LABEL_BLOCK);
4570 						(yyval.str) = NULL;
4571 					}
4572 #line 4573 "pl_gram.c" /* yacc.c:1652  */
4573     break;
4574 
4575   case 164:
4576 #line 2410 "pl_gram.y" /* yacc.c:1652  */
4577     {
4578 						plpgsql_ns_push((yyvsp[-1].str), PLPGSQL_LABEL_BLOCK);
4579 						(yyval.str) = (yyvsp[-1].str);
4580 					}
4581 #line 4582 "pl_gram.c" /* yacc.c:1652  */
4582     break;
4583 
4584   case 165:
4585 #line 2417 "pl_gram.y" /* yacc.c:1652  */
4586     {
4587 						plpgsql_ns_push(NULL, PLPGSQL_LABEL_LOOP);
4588 						(yyval.str) = NULL;
4589 					}
4590 #line 4591 "pl_gram.c" /* yacc.c:1652  */
4591     break;
4592 
4593   case 166:
4594 #line 2422 "pl_gram.y" /* yacc.c:1652  */
4595     {
4596 						plpgsql_ns_push((yyvsp[-1].str), PLPGSQL_LABEL_LOOP);
4597 						(yyval.str) = (yyvsp[-1].str);
4598 					}
4599 #line 4600 "pl_gram.c" /* yacc.c:1652  */
4600     break;
4601 
4602   case 167:
4603 #line 2429 "pl_gram.y" /* yacc.c:1652  */
4604     {
4605 						(yyval.str) = NULL;
4606 					}
4607 #line 4608 "pl_gram.c" /* yacc.c:1652  */
4608     break;
4609 
4610   case 168:
4611 #line 2433 "pl_gram.y" /* yacc.c:1652  */
4612     {
4613 						/* label validity will be checked by outer production */
4614 						(yyval.str) = (yyvsp[0].str);
4615 					}
4616 #line 4617 "pl_gram.c" /* yacc.c:1652  */
4617     break;
4618 
4619   case 169:
4620 #line 2440 "pl_gram.y" /* yacc.c:1652  */
4621     { (yyval.expr) = NULL; }
4622 #line 4623 "pl_gram.c" /* yacc.c:1652  */
4623     break;
4624 
4625   case 170:
4626 #line 2442 "pl_gram.y" /* yacc.c:1652  */
4627     { (yyval.expr) = (yyvsp[0].expr); }
4628 #line 4629 "pl_gram.c" /* yacc.c:1652  */
4629     break;
4630 
4631   case 171:
4632 #line 2449 "pl_gram.y" /* yacc.c:1652  */
4633     {
4634 						(yyval.str) = (yyvsp[0].word).ident;
4635 					}
4636 #line 4637 "pl_gram.c" /* yacc.c:1652  */
4637     break;
4638 
4639   case 172:
4640 #line 2453 "pl_gram.y" /* yacc.c:1652  */
4641     {
4642 						(yyval.str) = pstrdup((yyvsp[0].keyword));
4643 					}
4644 #line 4645 "pl_gram.c" /* yacc.c:1652  */
4645     break;
4646 
4647   case 173:
4648 #line 2457 "pl_gram.y" /* yacc.c:1652  */
4649     {
4650 						if ((yyvsp[0].wdatum).ident == NULL) /* composite name not OK */
4651 							yyerror("syntax error");
4652 						(yyval.str) = (yyvsp[0].wdatum).ident;
4653 					}
4654 #line 4655 "pl_gram.c" /* yacc.c:1652  */
4655     break;
4656 
4657 
4658 #line 4659 "pl_gram.c" /* yacc.c:1652  */
4659       default: break;
4660     }
4661   /* User semantic actions sometimes alter yychar, and that requires
4662      that yytoken be updated with the new translation.  We take the
4663      approach of translating immediately before every use of yytoken.
4664      One alternative is translating here after every semantic action,
4665      but that translation would be missed if the semantic action invokes
4666      YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
4667      if it invokes YYBACKUP.  In the case of YYABORT or YYACCEPT, an
4668      incorrect destructor might then be invoked immediately.  In the
4669      case of YYERROR or YYBACKUP, subsequent parser actions might lead
4670      to an incorrect destructor call or verbose syntax error message
4671      before the lookahead is translated.  */
4672   YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
4673 
4674   YYPOPSTACK (yylen);
4675   yylen = 0;
4676   YY_STACK_PRINT (yyss, yyssp);
4677 
4678   *++yyvsp = yyval;
4679   *++yylsp = yyloc;
4680 
4681   /* Now 'shift' the result of the reduction.  Determine what state
4682      that goes to, based on the state we popped back to and the rule
4683      number reduced by.  */
4684   {
4685     const int yylhs = yyr1[yyn] - YYNTOKENS;
4686     const int yyi = yypgoto[yylhs] + *yyssp;
4687     yystate = (0 <= yyi && yyi <= YYLAST && yycheck[yyi] == *yyssp
4688                ? yytable[yyi]
4689                : yydefgoto[yylhs]);
4690   }
4691 
4692   goto yynewstate;
4693 
4694 
4695 /*--------------------------------------.
4696 | yyerrlab -- here on detecting error.  |
4697 `--------------------------------------*/
4698 yyerrlab:
4699   /* Make sure we have latest lookahead translation.  See comments at
4700      user semantic actions for why this is necessary.  */
4701   yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
4702 
4703   /* If not already recovering from an error, report this error.  */
4704   if (!yyerrstatus)
4705     {
4706       ++yynerrs;
4707 #if ! YYERROR_VERBOSE
4708       yyerror (YY_("syntax error"));
4709 #else
4710 # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \
4711                                         yyssp, yytoken)
4712       {
4713         char const *yymsgp = YY_("syntax error");
4714         int yysyntax_error_status;
4715         yysyntax_error_status = YYSYNTAX_ERROR;
4716         if (yysyntax_error_status == 0)
4717           yymsgp = yymsg;
4718         else if (yysyntax_error_status == 1)
4719           {
4720             if (yymsg != yymsgbuf)
4721               YYSTACK_FREE (yymsg);
4722             yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc);
4723             if (!yymsg)
4724               {
4725                 yymsg = yymsgbuf;
4726                 yymsg_alloc = sizeof yymsgbuf;
4727                 yysyntax_error_status = 2;
4728               }
4729             else
4730               {
4731                 yysyntax_error_status = YYSYNTAX_ERROR;
4732                 yymsgp = yymsg;
4733               }
4734           }
4735         yyerror (yymsgp);
4736         if (yysyntax_error_status == 2)
4737           goto yyexhaustedlab;
4738       }
4739 # undef YYSYNTAX_ERROR
4740 #endif
4741     }
4742 
4743   yyerror_range[1] = yylloc;
4744 
4745   if (yyerrstatus == 3)
4746     {
4747       /* If just tried and failed to reuse lookahead token after an
4748          error, discard it.  */
4749 
4750       if (yychar <= YYEOF)
4751         {
4752           /* Return failure if at end of input.  */
4753           if (yychar == YYEOF)
4754             YYABORT;
4755         }
4756       else
4757         {
4758           yydestruct ("Error: discarding",
4759                       yytoken, &yylval, &yylloc);
4760           yychar = YYEMPTY;
4761         }
4762     }
4763 
4764   /* Else will try to reuse lookahead token after shifting the error
4765      token.  */
4766   goto yyerrlab1;
4767 
4768 
4769 /*---------------------------------------------------.
4770 | yyerrorlab -- error raised explicitly by YYERROR.  |
4771 `---------------------------------------------------*/
4772 yyerrorlab:
4773   /* Pacify compilers when the user code never invokes YYERROR and the
4774      label yyerrorlab therefore never appears in user code.  */
4775   if (0)
4776     YYERROR;
4777 
4778   /* Do not reclaim the symbols of the rule whose action triggered
4779      this YYERROR.  */
4780   YYPOPSTACK (yylen);
4781   yylen = 0;
4782   YY_STACK_PRINT (yyss, yyssp);
4783   yystate = *yyssp;
4784   goto yyerrlab1;
4785 
4786 
4787 /*-------------------------------------------------------------.
4788 | yyerrlab1 -- common code for both syntax error and YYERROR.  |
4789 `-------------------------------------------------------------*/
4790 yyerrlab1:
4791   yyerrstatus = 3;      /* Each real token shifted decrements this.  */
4792 
4793   for (;;)
4794     {
4795       yyn = yypact[yystate];
4796       if (!yypact_value_is_default (yyn))
4797         {
4798           yyn += YYTERROR;
4799           if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
4800             {
4801               yyn = yytable[yyn];
4802               if (0 < yyn)
4803                 break;
4804             }
4805         }
4806 
4807       /* Pop the current state because it cannot handle the error token.  */
4808       if (yyssp == yyss)
4809         YYABORT;
4810 
4811       yyerror_range[1] = *yylsp;
4812       yydestruct ("Error: popping",
4813                   yystos[yystate], yyvsp, yylsp);
4814       YYPOPSTACK (1);
4815       yystate = *yyssp;
4816       YY_STACK_PRINT (yyss, yyssp);
4817     }
4818 
4819   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
4820   *++yyvsp = yylval;
4821   YY_IGNORE_MAYBE_UNINITIALIZED_END
4822 
4823   yyerror_range[2] = yylloc;
4824   /* Using YYLLOC is tempting, but would change the location of
4825      the lookahead.  YYLOC is available though.  */
4826   YYLLOC_DEFAULT (yyloc, yyerror_range, 2);
4827   *++yylsp = yyloc;
4828 
4829   /* Shift the error token.  */
4830   YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
4831 
4832   yystate = yyn;
4833   goto yynewstate;
4834 
4835 
4836 /*-------------------------------------.
4837 | yyacceptlab -- YYACCEPT comes here.  |
4838 `-------------------------------------*/
4839 yyacceptlab:
4840   yyresult = 0;
4841   goto yyreturn;
4842 
4843 
4844 /*-----------------------------------.
4845 | yyabortlab -- YYABORT comes here.  |
4846 `-----------------------------------*/
4847 yyabortlab:
4848   yyresult = 1;
4849   goto yyreturn;
4850 
4851 
4852 #if !defined yyoverflow || YYERROR_VERBOSE
4853 /*-------------------------------------------------.
4854 | yyexhaustedlab -- memory exhaustion comes here.  |
4855 `-------------------------------------------------*/
4856 yyexhaustedlab:
4857   yyerror (YY_("memory exhausted"));
4858   yyresult = 2;
4859   /* Fall through.  */
4860 #endif
4861 
4862 
4863 /*-----------------------------------------------------.
4864 | yyreturn -- parsing is finished, return the result.  |
4865 `-----------------------------------------------------*/
4866 yyreturn:
4867   if (yychar != YYEMPTY)
4868     {
4869       /* Make sure we have latest lookahead translation.  See comments at
4870          user semantic actions for why this is necessary.  */
4871       yytoken = YYTRANSLATE (yychar);
4872       yydestruct ("Cleanup: discarding lookahead",
4873                   yytoken, &yylval, &yylloc);
4874     }
4875   /* Do not reclaim the symbols of the rule whose action triggered
4876      this YYABORT or YYACCEPT.  */
4877   YYPOPSTACK (yylen);
4878   YY_STACK_PRINT (yyss, yyssp);
4879   while (yyssp != yyss)
4880     {
4881       yydestruct ("Cleanup: popping",
4882                   yystos[*yyssp], yyvsp, yylsp);
4883       YYPOPSTACK (1);
4884     }
4885 #ifndef yyoverflow
4886   if (yyss != yyssa)
4887     YYSTACK_FREE (yyss);
4888 #endif
4889 #if YYERROR_VERBOSE
4890   if (yymsg != yymsgbuf)
4891     YYSTACK_FREE (yymsg);
4892 #endif
4893   return yyresult;
4894 }
4895 #line 2548 "pl_gram.y" /* yacc.c:1918  */
4896 
4897 
4898 /*
4899  * Check whether a token represents an "unreserved keyword".
4900  * We have various places where we want to recognize a keyword in preference
4901  * to a variable name, but not reserve that keyword in other contexts.
4902  * Hence, this kluge.
4903  */
4904 static bool
tok_is_keyword(int token,union YYSTYPE * lval,int kw_token,const char * kw_str)4905 tok_is_keyword(int token, union YYSTYPE *lval,
4906 			   int kw_token, const char *kw_str)
4907 {
4908 	if (token == kw_token)
4909 	{
4910 		/* Normal case, was recognized by scanner (no conflicting variable) */
4911 		return true;
4912 	}
4913 	else if (token == T_DATUM)
4914 	{
4915 		/*
4916 		 * It's a variable, so recheck the string name.  Note we will not
4917 		 * match composite names (hence an unreserved word followed by "."
4918 		 * will not be recognized).
4919 		 */
4920 		if (!lval->wdatum.quoted && lval->wdatum.ident != NULL &&
4921 			strcmp(lval->wdatum.ident, kw_str) == 0)
4922 			return true;
4923 	}
4924 	return false;				/* not the keyword */
4925 }
4926 
4927 /*
4928  * Convenience routine to complain when we expected T_DATUM and got T_WORD,
4929  * ie, unrecognized variable.
4930  */
4931 static void
word_is_not_variable(PLword * word,int location)4932 word_is_not_variable(PLword *word, int location)
4933 {
4934 	ereport(ERROR,
4935 			(errcode(ERRCODE_SYNTAX_ERROR),
4936 			 errmsg("\"%s\" is not a known variable",
4937 					word->ident),
4938 			 parser_errposition(location)));
4939 }
4940 
4941 /* Same, for a CWORD */
4942 static void
cword_is_not_variable(PLcword * cword,int location)4943 cword_is_not_variable(PLcword *cword, int location)
4944 {
4945 	ereport(ERROR,
4946 			(errcode(ERRCODE_SYNTAX_ERROR),
4947 			 errmsg("\"%s\" is not a known variable",
4948 					NameListToString(cword->idents)),
4949 			 parser_errposition(location)));
4950 }
4951 
4952 /*
4953  * Convenience routine to complain when we expected T_DATUM and got
4954  * something else.  "tok" must be the current token, since we also
4955  * look at yylval and yylloc.
4956  */
4957 static void
current_token_is_not_variable(int tok)4958 current_token_is_not_variable(int tok)
4959 {
4960 	if (tok == T_WORD)
4961 		word_is_not_variable(&(yylval.word), yylloc);
4962 	else if (tok == T_CWORD)
4963 		cword_is_not_variable(&(yylval.cword), yylloc);
4964 	else
4965 		yyerror("syntax error");
4966 }
4967 
4968 /* Convenience routine to read an expression with one possible terminator */
4969 static PLpgSQL_expr *
read_sql_expression(int until,const char * expected)4970 read_sql_expression(int until, const char *expected)
4971 {
4972 	return read_sql_construct(until, 0, 0, expected,
4973 							  "SELECT ", true, true, true, NULL, NULL);
4974 }
4975 
4976 /* Convenience routine to read an expression with two possible terminators */
4977 static PLpgSQL_expr *
read_sql_expression2(int until,int until2,const char * expected,int * endtoken)4978 read_sql_expression2(int until, int until2, const char *expected,
4979 					 int *endtoken)
4980 {
4981 	return read_sql_construct(until, until2, 0, expected,
4982 							  "SELECT ", true, true, true, NULL, endtoken);
4983 }
4984 
4985 /* Convenience routine to read a SQL statement that must end with ';' */
4986 static PLpgSQL_expr *
read_sql_stmt(const char * sqlstart)4987 read_sql_stmt(const char *sqlstart)
4988 {
4989 	return read_sql_construct(';', 0, 0, ";",
4990 							  sqlstart, false, true, true, NULL, NULL);
4991 }
4992 
4993 /*
4994  * Read a SQL construct and build a PLpgSQL_expr for it.
4995  *
4996  * until:		token code for expected terminator
4997  * until2:		token code for alternate terminator (pass 0 if none)
4998  * until3:		token code for another alternate terminator (pass 0 if none)
4999  * expected:	text to use in complaining that terminator was not found
5000  * sqlstart:	text to prefix to the accumulated SQL text
5001  * isexpression: whether to say we're reading an "expression" or a "statement"
5002  * valid_sql:   whether to check the syntax of the expr (prefixed with sqlstart)
5003  * trim:		trim trailing whitespace
5004  * startloc:	if not NULL, location of first token is stored at *startloc
5005  * endtoken:	if not NULL, ending token is stored at *endtoken
5006  *				(this is only interesting if until2 or until3 isn't zero)
5007  */
5008 static PLpgSQL_expr *
read_sql_construct(int until,int until2,int until3,const char * expected,const char * sqlstart,bool isexpression,bool valid_sql,bool trim,int * startloc,int * endtoken)5009 read_sql_construct(int until,
5010 				   int until2,
5011 				   int until3,
5012 				   const char *expected,
5013 				   const char *sqlstart,
5014 				   bool isexpression,
5015 				   bool valid_sql,
5016 				   bool trim,
5017 				   int *startloc,
5018 				   int *endtoken)
5019 {
5020 	int					tok;
5021 	StringInfoData		ds;
5022 	IdentifierLookup	save_IdentifierLookup;
5023 	int					startlocation = -1;
5024 	int					parenlevel = 0;
5025 	PLpgSQL_expr		*expr;
5026 
5027 	initStringInfo(&ds);
5028 	appendStringInfoString(&ds, sqlstart);
5029 
5030 	/* special lookup mode for identifiers within the SQL text */
5031 	save_IdentifierLookup = plpgsql_IdentifierLookup;
5032 	plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_EXPR;
5033 
5034 	for (;;)
5035 	{
5036 		tok = yylex();
5037 		if (startlocation < 0)			/* remember loc of first token */
5038 			startlocation = yylloc;
5039 		if (tok == until && parenlevel == 0)
5040 			break;
5041 		if (tok == until2 && parenlevel == 0)
5042 			break;
5043 		if (tok == until3 && parenlevel == 0)
5044 			break;
5045 		if (tok == '(' || tok == '[')
5046 			parenlevel++;
5047 		else if (tok == ')' || tok == ']')
5048 		{
5049 			parenlevel--;
5050 			if (parenlevel < 0)
5051 				yyerror("mismatched parentheses");
5052 		}
5053 		/*
5054 		 * End of function definition is an error, and we don't expect to
5055 		 * hit a semicolon either (unless it's the until symbol, in which
5056 		 * case we should have fallen out above).
5057 		 */
5058 		if (tok == 0 || tok == ';')
5059 		{
5060 			if (parenlevel != 0)
5061 				yyerror("mismatched parentheses");
5062 			if (isexpression)
5063 				ereport(ERROR,
5064 						(errcode(ERRCODE_SYNTAX_ERROR),
5065 						 errmsg("missing \"%s\" at end of SQL expression",
5066 								expected),
5067 						 parser_errposition(yylloc)));
5068 			else
5069 				ereport(ERROR,
5070 						(errcode(ERRCODE_SYNTAX_ERROR),
5071 						 errmsg("missing \"%s\" at end of SQL statement",
5072 								expected),
5073 						 parser_errposition(yylloc)));
5074 		}
5075 	}
5076 
5077 	plpgsql_IdentifierLookup = save_IdentifierLookup;
5078 
5079 	if (startloc)
5080 		*startloc = startlocation;
5081 	if (endtoken)
5082 		*endtoken = tok;
5083 
5084 	/* give helpful complaint about empty input */
5085 	if (startlocation >= yylloc)
5086 	{
5087 		if (isexpression)
5088 			yyerror("missing expression");
5089 		else
5090 			yyerror("missing SQL statement");
5091 	}
5092 
5093 	plpgsql_append_source_text(&ds, startlocation, yylloc);
5094 
5095 	/* trim any trailing whitespace, for neatness */
5096 	if (trim)
5097 	{
5098 		while (ds.len > 0 && scanner_isspace(ds.data[ds.len - 1]))
5099 			ds.data[--ds.len] = '\0';
5100 	}
5101 
5102 	expr = palloc0(sizeof(PLpgSQL_expr));
5103 	expr->query			= pstrdup(ds.data);
5104 	expr->plan			= NULL;
5105 	expr->paramnos		= NULL;
5106 	expr->rwparam		= -1;
5107 	expr->ns			= plpgsql_ns_top();
5108 	pfree(ds.data);
5109 
5110 	if (valid_sql)
5111 		check_sql_expr(expr->query, startlocation, strlen(sqlstart));
5112 
5113 	return expr;
5114 }
5115 
5116 static PLpgSQL_type *
read_datatype(int tok)5117 read_datatype(int tok)
5118 {
5119 	StringInfoData		ds;
5120 	char			   *type_name;
5121 	int					startlocation;
5122 	PLpgSQL_type		*result;
5123 	int					parenlevel = 0;
5124 
5125 	/* Should only be called while parsing DECLARE sections */
5126 	Assert(plpgsql_IdentifierLookup == IDENTIFIER_LOOKUP_DECLARE);
5127 
5128 	/* Often there will be a lookahead token, but if not, get one */
5129 	if (tok == YYEMPTY)
5130 		tok = yylex();
5131 
5132 	startlocation = yylloc;
5133 
5134 	/*
5135 	 * If we have a simple or composite identifier, check for %TYPE
5136 	 * and %ROWTYPE constructs.
5137 	 */
5138 	if (tok == T_WORD)
5139 	{
5140 		char   *dtname = yylval.word.ident;
5141 
5142 		tok = yylex();
5143 		if (tok == '%')
5144 		{
5145 			tok = yylex();
5146 			if (tok_is_keyword(tok, &yylval,
5147 							   K_TYPE, "type"))
5148 			{
5149 				result = plpgsql_parse_wordtype(dtname);
5150 				if (result)
5151 					return result;
5152 			}
5153 			else if (tok_is_keyword(tok, &yylval,
5154 									K_ROWTYPE, "rowtype"))
5155 			{
5156 				result = plpgsql_parse_wordrowtype(dtname);
5157 				if (result)
5158 					return result;
5159 			}
5160 		}
5161 	}
5162 	else if (plpgsql_token_is_unreserved_keyword(tok))
5163 	{
5164 		char   *dtname = pstrdup(yylval.keyword);
5165 
5166 		tok = yylex();
5167 		if (tok == '%')
5168 		{
5169 			tok = yylex();
5170 			if (tok_is_keyword(tok, &yylval,
5171 							   K_TYPE, "type"))
5172 			{
5173 				result = plpgsql_parse_wordtype(dtname);
5174 				if (result)
5175 					return result;
5176 			}
5177 			else if (tok_is_keyword(tok, &yylval,
5178 									K_ROWTYPE, "rowtype"))
5179 			{
5180 				result = plpgsql_parse_wordrowtype(dtname);
5181 				if (result)
5182 					return result;
5183 			}
5184 		}
5185 	}
5186 	else if (tok == T_CWORD)
5187 	{
5188 		List   *dtnames = yylval.cword.idents;
5189 
5190 		tok = yylex();
5191 		if (tok == '%')
5192 		{
5193 			tok = yylex();
5194 			if (tok_is_keyword(tok, &yylval,
5195 							   K_TYPE, "type"))
5196 			{
5197 				result = plpgsql_parse_cwordtype(dtnames);
5198 				if (result)
5199 					return result;
5200 			}
5201 			else if (tok_is_keyword(tok, &yylval,
5202 									K_ROWTYPE, "rowtype"))
5203 			{
5204 				result = plpgsql_parse_cwordrowtype(dtnames);
5205 				if (result)
5206 					return result;
5207 			}
5208 		}
5209 	}
5210 
5211 	while (tok != ';')
5212 	{
5213 		if (tok == 0)
5214 		{
5215 			if (parenlevel != 0)
5216 				yyerror("mismatched parentheses");
5217 			else
5218 				yyerror("incomplete data type declaration");
5219 		}
5220 		/* Possible followers for datatype in a declaration */
5221 		if (tok == K_COLLATE || tok == K_NOT ||
5222 			tok == '=' || tok == COLON_EQUALS || tok == K_DEFAULT)
5223 			break;
5224 		/* Possible followers for datatype in a cursor_arg list */
5225 		if ((tok == ',' || tok == ')') && parenlevel == 0)
5226 			break;
5227 		if (tok == '(')
5228 			parenlevel++;
5229 		else if (tok == ')')
5230 			parenlevel--;
5231 
5232 		tok = yylex();
5233 	}
5234 
5235 	/* set up ds to contain complete typename text */
5236 	initStringInfo(&ds);
5237 	plpgsql_append_source_text(&ds, startlocation, yylloc);
5238 	type_name = ds.data;
5239 
5240 	if (type_name[0] == '\0')
5241 		yyerror("missing data type declaration");
5242 
5243 	result = parse_datatype(type_name, startlocation);
5244 
5245 	pfree(ds.data);
5246 
5247 	plpgsql_push_back_token(tok);
5248 
5249 	return result;
5250 }
5251 
5252 static PLpgSQL_stmt *
make_execsql_stmt(int firsttoken,int location)5253 make_execsql_stmt(int firsttoken, int location)
5254 {
5255 	StringInfoData		ds;
5256 	IdentifierLookup	save_IdentifierLookup;
5257 	PLpgSQL_stmt_execsql *execsql;
5258 	PLpgSQL_expr		*expr;
5259 	PLpgSQL_variable	*target = NULL;
5260 	int					tok;
5261 	int					prev_tok;
5262 	bool				have_into = false;
5263 	bool				have_strict = false;
5264 	int					into_start_loc = -1;
5265 	int					into_end_loc = -1;
5266 
5267 	initStringInfo(&ds);
5268 
5269 	/* special lookup mode for identifiers within the SQL text */
5270 	save_IdentifierLookup = plpgsql_IdentifierLookup;
5271 	plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_EXPR;
5272 
5273 	/*
5274 	 * Scan to the end of the SQL command.  Identify any INTO-variables
5275 	 * clause lurking within it, and parse that via read_into_target().
5276 	 *
5277 	 * Because INTO is sometimes used in the main SQL grammar, we have to be
5278 	 * careful not to take any such usage of INTO as a PL/pgSQL INTO clause.
5279 	 * There are currently three such cases:
5280 	 *
5281 	 * 1. SELECT ... INTO.  We don't care, we just override that with the
5282 	 * PL/pgSQL definition.
5283 	 *
5284 	 * 2. INSERT INTO.  This is relatively easy to recognize since the words
5285 	 * must appear adjacently; but we can't assume INSERT starts the command,
5286 	 * because it can appear in CREATE RULE or WITH.  Unfortunately, INSERT is
5287 	 * *not* fully reserved, so that means there is a chance of a false match;
5288 	 * but it's not very likely.
5289 	 *
5290 	 * 3. IMPORT FOREIGN SCHEMA ... INTO.  This is not allowed in CREATE RULE
5291 	 * or WITH, so we just check for IMPORT as the command's first token.
5292 	 * (If IMPORT FOREIGN SCHEMA returned data someone might wish to capture
5293 	 * with an INTO-variables clause, we'd have to work much harder here.)
5294 	 *
5295 	 * Fortunately, INTO is a fully reserved word in the main grammar, so
5296 	 * at least we need not worry about it appearing as an identifier.
5297 	 *
5298 	 * Any future additional uses of INTO in the main grammar will doubtless
5299 	 * break this logic again ... beware!
5300 	 */
5301 	tok = firsttoken;
5302 	for (;;)
5303 	{
5304 		prev_tok = tok;
5305 		tok = yylex();
5306 		if (have_into && into_end_loc < 0)
5307 			into_end_loc = yylloc;		/* token after the INTO part */
5308 		if (tok == ';')
5309 			break;
5310 		if (tok == 0)
5311 			yyerror("unexpected end of function definition");
5312 		if (tok == K_INTO)
5313 		{
5314 			if (prev_tok == K_INSERT)
5315 				continue;		/* INSERT INTO is not an INTO-target */
5316 			if (firsttoken == K_IMPORT)
5317 				continue;		/* IMPORT ... INTO is not an INTO-target */
5318 			if (have_into)
5319 				yyerror("INTO specified more than once");
5320 			have_into = true;
5321 			into_start_loc = yylloc;
5322 			plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
5323 			read_into_target(&target, &have_strict);
5324 			plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_EXPR;
5325 		}
5326 	}
5327 
5328 	plpgsql_IdentifierLookup = save_IdentifierLookup;
5329 
5330 	if (have_into)
5331 	{
5332 		/*
5333 		 * Insert an appropriate number of spaces corresponding to the
5334 		 * INTO text, so that locations within the redacted SQL statement
5335 		 * still line up with those in the original source text.
5336 		 */
5337 		plpgsql_append_source_text(&ds, location, into_start_loc);
5338 		appendStringInfoSpaces(&ds, into_end_loc - into_start_loc);
5339 		plpgsql_append_source_text(&ds, into_end_loc, yylloc);
5340 	}
5341 	else
5342 		plpgsql_append_source_text(&ds, location, yylloc);
5343 
5344 	/* trim any trailing whitespace, for neatness */
5345 	while (ds.len > 0 && scanner_isspace(ds.data[ds.len - 1]))
5346 		ds.data[--ds.len] = '\0';
5347 
5348 	expr = palloc0(sizeof(PLpgSQL_expr));
5349 	expr->query			= pstrdup(ds.data);
5350 	expr->plan			= NULL;
5351 	expr->paramnos		= NULL;
5352 	expr->rwparam		= -1;
5353 	expr->ns			= plpgsql_ns_top();
5354 	pfree(ds.data);
5355 
5356 	check_sql_expr(expr->query, location, 0);
5357 
5358 	execsql = palloc0(sizeof(PLpgSQL_stmt_execsql));
5359 	execsql->cmd_type = PLPGSQL_STMT_EXECSQL;
5360 	execsql->lineno  = plpgsql_location_to_lineno(location);
5361 	execsql->sqlstmt = expr;
5362 	execsql->into	 = have_into;
5363 	execsql->strict	 = have_strict;
5364 	execsql->target	 = target;
5365 
5366 	return (PLpgSQL_stmt *) execsql;
5367 }
5368 
5369 
5370 /*
5371  * Read FETCH or MOVE direction clause (everything through FROM/IN).
5372  */
5373 static PLpgSQL_stmt_fetch *
read_fetch_direction(void)5374 read_fetch_direction(void)
5375 {
5376 	PLpgSQL_stmt_fetch *fetch;
5377 	int			tok;
5378 	bool		check_FROM = true;
5379 
5380 	/*
5381 	 * We create the PLpgSQL_stmt_fetch struct here, but only fill in
5382 	 * the fields arising from the optional direction clause
5383 	 */
5384 	fetch = (PLpgSQL_stmt_fetch *) palloc0(sizeof(PLpgSQL_stmt_fetch));
5385 	fetch->cmd_type = PLPGSQL_STMT_FETCH;
5386 	/* set direction defaults: */
5387 	fetch->direction = FETCH_FORWARD;
5388 	fetch->how_many  = 1;
5389 	fetch->expr		 = NULL;
5390 	fetch->returns_multiple_rows = false;
5391 
5392 	tok = yylex();
5393 	if (tok == 0)
5394 		yyerror("unexpected end of function definition");
5395 
5396 	if (tok_is_keyword(tok, &yylval,
5397 					   K_NEXT, "next"))
5398 	{
5399 		/* use defaults */
5400 	}
5401 	else if (tok_is_keyword(tok, &yylval,
5402 							K_PRIOR, "prior"))
5403 	{
5404 		fetch->direction = FETCH_BACKWARD;
5405 	}
5406 	else if (tok_is_keyword(tok, &yylval,
5407 							K_FIRST, "first"))
5408 	{
5409 		fetch->direction = FETCH_ABSOLUTE;
5410 	}
5411 	else if (tok_is_keyword(tok, &yylval,
5412 							K_LAST, "last"))
5413 	{
5414 		fetch->direction = FETCH_ABSOLUTE;
5415 		fetch->how_many  = -1;
5416 	}
5417 	else if (tok_is_keyword(tok, &yylval,
5418 							K_ABSOLUTE, "absolute"))
5419 	{
5420 		fetch->direction = FETCH_ABSOLUTE;
5421 		fetch->expr = read_sql_expression2(K_FROM, K_IN,
5422 										   "FROM or IN",
5423 										   NULL);
5424 		check_FROM = false;
5425 	}
5426 	else if (tok_is_keyword(tok, &yylval,
5427 							K_RELATIVE, "relative"))
5428 	{
5429 		fetch->direction = FETCH_RELATIVE;
5430 		fetch->expr = read_sql_expression2(K_FROM, K_IN,
5431 										   "FROM or IN",
5432 										   NULL);
5433 		check_FROM = false;
5434 	}
5435 	else if (tok_is_keyword(tok, &yylval,
5436 							K_ALL, "all"))
5437 	{
5438 		fetch->how_many = FETCH_ALL;
5439 		fetch->returns_multiple_rows = true;
5440 	}
5441 	else if (tok_is_keyword(tok, &yylval,
5442 							K_FORWARD, "forward"))
5443 	{
5444 		complete_direction(fetch, &check_FROM);
5445 	}
5446 	else if (tok_is_keyword(tok, &yylval,
5447 							K_BACKWARD, "backward"))
5448 	{
5449 		fetch->direction = FETCH_BACKWARD;
5450 		complete_direction(fetch, &check_FROM);
5451 	}
5452 	else if (tok == K_FROM || tok == K_IN)
5453 	{
5454 		/* empty direction */
5455 		check_FROM = false;
5456 	}
5457 	else if (tok == T_DATUM)
5458 	{
5459 		/* Assume there's no direction clause and tok is a cursor name */
5460 		plpgsql_push_back_token(tok);
5461 		check_FROM = false;
5462 	}
5463 	else
5464 	{
5465 		/*
5466 		 * Assume it's a count expression with no preceding keyword.
5467 		 * Note: we allow this syntax because core SQL does, but we don't
5468 		 * document it because of the ambiguity with the omitted-direction
5469 		 * case.  For instance, "MOVE n IN c" will fail if n is a variable.
5470 		 * Perhaps this can be improved someday, but it's hardly worth a
5471 		 * lot of work.
5472 		 */
5473 		plpgsql_push_back_token(tok);
5474 		fetch->expr = read_sql_expression2(K_FROM, K_IN,
5475 										   "FROM or IN",
5476 										   NULL);
5477 		fetch->returns_multiple_rows = true;
5478 		check_FROM = false;
5479 	}
5480 
5481 	/* check FROM or IN keyword after direction's specification */
5482 	if (check_FROM)
5483 	{
5484 		tok = yylex();
5485 		if (tok != K_FROM && tok != K_IN)
5486 			yyerror("expected FROM or IN");
5487 	}
5488 
5489 	return fetch;
5490 }
5491 
5492 /*
5493  * Process remainder of FETCH/MOVE direction after FORWARD or BACKWARD.
5494  * Allows these cases:
5495  *   FORWARD expr,  FORWARD ALL,  FORWARD
5496  *   BACKWARD expr, BACKWARD ALL, BACKWARD
5497  */
5498 static void
complete_direction(PLpgSQL_stmt_fetch * fetch,bool * check_FROM)5499 complete_direction(PLpgSQL_stmt_fetch *fetch,  bool *check_FROM)
5500 {
5501 	int			tok;
5502 
5503 	tok = yylex();
5504 	if (tok == 0)
5505 		yyerror("unexpected end of function definition");
5506 
5507 	if (tok == K_FROM || tok == K_IN)
5508 	{
5509 		*check_FROM = false;
5510 		return;
5511 	}
5512 
5513 	if (tok == K_ALL)
5514 	{
5515 		fetch->how_many = FETCH_ALL;
5516 		fetch->returns_multiple_rows = true;
5517 		*check_FROM = true;
5518 		return;
5519 	}
5520 
5521 	plpgsql_push_back_token(tok);
5522 	fetch->expr = read_sql_expression2(K_FROM, K_IN,
5523 									   "FROM or IN",
5524 									   NULL);
5525 	fetch->returns_multiple_rows = true;
5526 	*check_FROM = false;
5527 }
5528 
5529 
5530 static PLpgSQL_stmt *
make_return_stmt(int location)5531 make_return_stmt(int location)
5532 {
5533 	PLpgSQL_stmt_return *new;
5534 
5535 	new = palloc0(sizeof(PLpgSQL_stmt_return));
5536 	new->cmd_type = PLPGSQL_STMT_RETURN;
5537 	new->lineno   = plpgsql_location_to_lineno(location);
5538 	new->expr	  = NULL;
5539 	new->retvarno = -1;
5540 
5541 	if (plpgsql_curr_compile->fn_retset)
5542 	{
5543 		if (yylex() != ';')
5544 			ereport(ERROR,
5545 					(errcode(ERRCODE_DATATYPE_MISMATCH),
5546 					 errmsg("RETURN cannot have a parameter in function returning set"),
5547 					 errhint("Use RETURN NEXT or RETURN QUERY."),
5548 					 parser_errposition(yylloc)));
5549 	}
5550 	else if (plpgsql_curr_compile->fn_rettype == VOIDOID)
5551 	{
5552 		if (yylex() != ';')
5553 		{
5554 			if (plpgsql_curr_compile->fn_prokind == PROKIND_PROCEDURE)
5555 				ereport(ERROR,
5556 						(errcode(ERRCODE_SYNTAX_ERROR),
5557 						 errmsg("RETURN cannot have a parameter in a procedure"),
5558 						 parser_errposition(yylloc)));
5559 			else
5560 				ereport(ERROR,
5561 						(errcode(ERRCODE_DATATYPE_MISMATCH),
5562 						 errmsg("RETURN cannot have a parameter in function returning void"),
5563 						 parser_errposition(yylloc)));
5564 		}
5565 	}
5566 	else if (plpgsql_curr_compile->out_param_varno >= 0)
5567 	{
5568 		if (yylex() != ';')
5569 			ereport(ERROR,
5570 					(errcode(ERRCODE_DATATYPE_MISMATCH),
5571 					 errmsg("RETURN cannot have a parameter in function with OUT parameters"),
5572 					 parser_errposition(yylloc)));
5573 		new->retvarno = plpgsql_curr_compile->out_param_varno;
5574 	}
5575 	else
5576 	{
5577 		/*
5578 		 * We want to special-case simple variable references for efficiency.
5579 		 * So peek ahead to see if that's what we have.
5580 		 */
5581 		int		tok = yylex();
5582 
5583 		if (tok == T_DATUM && plpgsql_peek() == ';' &&
5584 			(yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_VAR ||
5585 			 yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_PROMISE ||
5586 			 yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_ROW ||
5587 			 yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_REC))
5588 		{
5589 			new->retvarno = yylval.wdatum.datum->dno;
5590 			/* eat the semicolon token that we only peeked at above */
5591 			tok = yylex();
5592 			Assert(tok == ';');
5593 		}
5594 		else
5595 		{
5596 			/*
5597 			 * Not (just) a variable name, so treat as expression.
5598 			 *
5599 			 * Note that a well-formed expression is _required_ here;
5600 			 * anything else is a compile-time error.
5601 			 */
5602 			plpgsql_push_back_token(tok);
5603 			new->expr = read_sql_expression(';', ";");
5604 		}
5605 	}
5606 
5607 	return (PLpgSQL_stmt *) new;
5608 }
5609 
5610 
5611 static PLpgSQL_stmt *
make_return_next_stmt(int location)5612 make_return_next_stmt(int location)
5613 {
5614 	PLpgSQL_stmt_return_next *new;
5615 
5616 	if (!plpgsql_curr_compile->fn_retset)
5617 		ereport(ERROR,
5618 				(errcode(ERRCODE_DATATYPE_MISMATCH),
5619 				 errmsg("cannot use RETURN NEXT in a non-SETOF function"),
5620 				 parser_errposition(location)));
5621 
5622 	new = palloc0(sizeof(PLpgSQL_stmt_return_next));
5623 	new->cmd_type	= PLPGSQL_STMT_RETURN_NEXT;
5624 	new->lineno		= plpgsql_location_to_lineno(location);
5625 	new->expr		= NULL;
5626 	new->retvarno	= -1;
5627 
5628 	if (plpgsql_curr_compile->out_param_varno >= 0)
5629 	{
5630 		if (yylex() != ';')
5631 			ereport(ERROR,
5632 					(errcode(ERRCODE_DATATYPE_MISMATCH),
5633 					 errmsg("RETURN NEXT cannot have a parameter in function with OUT parameters"),
5634 					 parser_errposition(yylloc)));
5635 		new->retvarno = plpgsql_curr_compile->out_param_varno;
5636 	}
5637 	else
5638 	{
5639 		/*
5640 		 * We want to special-case simple variable references for efficiency.
5641 		 * So peek ahead to see if that's what we have.
5642 		 */
5643 		int		tok = yylex();
5644 
5645 		if (tok == T_DATUM && plpgsql_peek() == ';' &&
5646 			(yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_VAR ||
5647 			 yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_PROMISE ||
5648 			 yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_ROW ||
5649 			 yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_REC))
5650 		{
5651 			new->retvarno = yylval.wdatum.datum->dno;
5652 			/* eat the semicolon token that we only peeked at above */
5653 			tok = yylex();
5654 			Assert(tok == ';');
5655 		}
5656 		else
5657 		{
5658 			/*
5659 			 * Not (just) a variable name, so treat as expression.
5660 			 *
5661 			 * Note that a well-formed expression is _required_ here;
5662 			 * anything else is a compile-time error.
5663 			 */
5664 			plpgsql_push_back_token(tok);
5665 			new->expr = read_sql_expression(';', ";");
5666 		}
5667 	}
5668 
5669 	return (PLpgSQL_stmt *) new;
5670 }
5671 
5672 
5673 static PLpgSQL_stmt *
make_return_query_stmt(int location)5674 make_return_query_stmt(int location)
5675 {
5676 	PLpgSQL_stmt_return_query *new;
5677 	int			tok;
5678 
5679 	if (!plpgsql_curr_compile->fn_retset)
5680 		ereport(ERROR,
5681 				(errcode(ERRCODE_DATATYPE_MISMATCH),
5682 				 errmsg("cannot use RETURN QUERY in a non-SETOF function"),
5683 				 parser_errposition(location)));
5684 
5685 	new = palloc0(sizeof(PLpgSQL_stmt_return_query));
5686 	new->cmd_type = PLPGSQL_STMT_RETURN_QUERY;
5687 	new->lineno = plpgsql_location_to_lineno(location);
5688 
5689 	/* check for RETURN QUERY EXECUTE */
5690 	if ((tok = yylex()) != K_EXECUTE)
5691 	{
5692 		/* ordinary static query */
5693 		plpgsql_push_back_token(tok);
5694 		new->query = read_sql_stmt("");
5695 	}
5696 	else
5697 	{
5698 		/* dynamic SQL */
5699 		int		term;
5700 
5701 		new->dynquery = read_sql_expression2(';', K_USING, "; or USING",
5702 											 &term);
5703 		if (term == K_USING)
5704 		{
5705 			do
5706 			{
5707 				PLpgSQL_expr *expr;
5708 
5709 				expr = read_sql_expression2(',', ';', ", or ;", &term);
5710 				new->params = lappend(new->params, expr);
5711 			} while (term == ',');
5712 		}
5713 	}
5714 
5715 	return (PLpgSQL_stmt *) new;
5716 }
5717 
5718 
5719 /* convenience routine to fetch the name of a T_DATUM */
5720 static char *
NameOfDatum(PLwdatum * wdatum)5721 NameOfDatum(PLwdatum *wdatum)
5722 {
5723 	if (wdatum->ident)
5724 		return wdatum->ident;
5725 	Assert(wdatum->idents != NIL);
5726 	return NameListToString(wdatum->idents);
5727 }
5728 
5729 static void
check_assignable(PLpgSQL_datum * datum,int location)5730 check_assignable(PLpgSQL_datum *datum, int location)
5731 {
5732 	switch (datum->dtype)
5733 	{
5734 		case PLPGSQL_DTYPE_VAR:
5735 		case PLPGSQL_DTYPE_PROMISE:
5736 		case PLPGSQL_DTYPE_REC:
5737 			if (((PLpgSQL_variable *) datum)->isconst)
5738 				ereport(ERROR,
5739 						(errcode(ERRCODE_ERROR_IN_ASSIGNMENT),
5740 						 errmsg("variable \"%s\" is declared CONSTANT",
5741 								((PLpgSQL_variable *) datum)->refname),
5742 						 parser_errposition(location)));
5743 			break;
5744 		case PLPGSQL_DTYPE_ROW:
5745 			/* always assignable; member vars were checked at compile time */
5746 			break;
5747 		case PLPGSQL_DTYPE_RECFIELD:
5748 			/* assignable if parent record is */
5749 			check_assignable(plpgsql_Datums[((PLpgSQL_recfield *) datum)->recparentno],
5750 							 location);
5751 			break;
5752 		case PLPGSQL_DTYPE_ARRAYELEM:
5753 			/* assignable if parent array is */
5754 			check_assignable(plpgsql_Datums[((PLpgSQL_arrayelem *) datum)->arrayparentno],
5755 							 location);
5756 			break;
5757 		default:
5758 			elog(ERROR, "unrecognized dtype: %d", datum->dtype);
5759 			break;
5760 	}
5761 }
5762 
5763 /*
5764  * Read the argument of an INTO clause.  On entry, we have just read the
5765  * INTO keyword.
5766  */
5767 static void
read_into_target(PLpgSQL_variable ** target,bool * strict)5768 read_into_target(PLpgSQL_variable **target, bool *strict)
5769 {
5770 	int			tok;
5771 
5772 	/* Set default results */
5773 	*target = NULL;
5774 	if (strict)
5775 		*strict = false;
5776 
5777 	tok = yylex();
5778 	if (strict && tok == K_STRICT)
5779 	{
5780 		*strict = true;
5781 		tok = yylex();
5782 	}
5783 
5784 	/*
5785 	 * Currently, a row or record variable can be the single INTO target,
5786 	 * but not a member of a multi-target list.  So we throw error if there
5787 	 * is a comma after it, because that probably means the user tried to
5788 	 * write a multi-target list.  If this ever gets generalized, we should
5789 	 * probably refactor read_into_scalar_list so it handles all cases.
5790 	 */
5791 	switch (tok)
5792 	{
5793 		case T_DATUM:
5794 			if (yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_ROW ||
5795 				yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_REC)
5796 			{
5797 				check_assignable(yylval.wdatum.datum, yylloc);
5798 				*target = (PLpgSQL_variable *) yylval.wdatum.datum;
5799 
5800 				if ((tok = yylex()) == ',')
5801 					ereport(ERROR,
5802 							(errcode(ERRCODE_SYNTAX_ERROR),
5803 							 errmsg("record variable cannot be part of multiple-item INTO list"),
5804 							 parser_errposition(yylloc)));
5805 				plpgsql_push_back_token(tok);
5806 			}
5807 			else
5808 			{
5809 				*target = (PLpgSQL_variable *)
5810 					read_into_scalar_list(NameOfDatum(&(yylval.wdatum)),
5811 										  yylval.wdatum.datum, yylloc);
5812 			}
5813 			break;
5814 
5815 		default:
5816 			/* just to give a better message than "syntax error" */
5817 			current_token_is_not_variable(tok);
5818 	}
5819 }
5820 
5821 /*
5822  * Given the first datum and name in the INTO list, continue to read
5823  * comma-separated scalar variables until we run out. Then construct
5824  * and return a fake "row" variable that represents the list of
5825  * scalars.
5826  */
5827 static PLpgSQL_row *
read_into_scalar_list(char * initial_name,PLpgSQL_datum * initial_datum,int initial_location)5828 read_into_scalar_list(char *initial_name,
5829 					  PLpgSQL_datum *initial_datum,
5830 					  int initial_location)
5831 {
5832 	int				 nfields;
5833 	char			*fieldnames[1024];
5834 	int				 varnos[1024];
5835 	PLpgSQL_row		*row;
5836 	int				 tok;
5837 
5838 	check_assignable(initial_datum, initial_location);
5839 	fieldnames[0] = initial_name;
5840 	varnos[0]	  = initial_datum->dno;
5841 	nfields		  = 1;
5842 
5843 	while ((tok = yylex()) == ',')
5844 	{
5845 		/* Check for array overflow */
5846 		if (nfields >= 1024)
5847 			ereport(ERROR,
5848 					(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5849 					 errmsg("too many INTO variables specified"),
5850 					 parser_errposition(yylloc)));
5851 
5852 		tok = yylex();
5853 		switch (tok)
5854 		{
5855 			case T_DATUM:
5856 				check_assignable(yylval.wdatum.datum, yylloc);
5857 				if (yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_ROW ||
5858 					yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_REC)
5859 					ereport(ERROR,
5860 							(errcode(ERRCODE_SYNTAX_ERROR),
5861 							 errmsg("\"%s\" is not a scalar variable",
5862 									NameOfDatum(&(yylval.wdatum))),
5863 							 parser_errposition(yylloc)));
5864 				fieldnames[nfields] = NameOfDatum(&(yylval.wdatum));
5865 				varnos[nfields++]	= yylval.wdatum.datum->dno;
5866 				break;
5867 
5868 			default:
5869 				/* just to give a better message than "syntax error" */
5870 				current_token_is_not_variable(tok);
5871 		}
5872 	}
5873 
5874 	/*
5875 	 * We read an extra, non-comma token from yylex(), so push it
5876 	 * back onto the input stream
5877 	 */
5878 	plpgsql_push_back_token(tok);
5879 
5880 	row = palloc0(sizeof(PLpgSQL_row));
5881 	row->dtype = PLPGSQL_DTYPE_ROW;
5882 	row->refname = "(unnamed row)";
5883 	row->lineno = plpgsql_location_to_lineno(initial_location);
5884 	row->rowtupdesc = NULL;
5885 	row->nfields = nfields;
5886 	row->fieldnames = palloc(sizeof(char *) * nfields);
5887 	row->varnos = palloc(sizeof(int) * nfields);
5888 	while (--nfields >= 0)
5889 	{
5890 		row->fieldnames[nfields] = fieldnames[nfields];
5891 		row->varnos[nfields] = varnos[nfields];
5892 	}
5893 
5894 	plpgsql_adddatum((PLpgSQL_datum *)row);
5895 
5896 	return row;
5897 }
5898 
5899 /*
5900  * Convert a single scalar into a "row" list.  This is exactly
5901  * like read_into_scalar_list except we never consume any input.
5902  *
5903  * Note: lineno could be computed from location, but since callers
5904  * have it at hand already, we may as well pass it in.
5905  */
5906 static PLpgSQL_row *
make_scalar_list1(char * initial_name,PLpgSQL_datum * initial_datum,int lineno,int location)5907 make_scalar_list1(char *initial_name,
5908 				  PLpgSQL_datum *initial_datum,
5909 				  int lineno, int location)
5910 {
5911 	PLpgSQL_row		*row;
5912 
5913 	check_assignable(initial_datum, location);
5914 
5915 	row = palloc0(sizeof(PLpgSQL_row));
5916 	row->dtype = PLPGSQL_DTYPE_ROW;
5917 	row->refname = "(unnamed row)";
5918 	row->lineno = lineno;
5919 	row->rowtupdesc = NULL;
5920 	row->nfields = 1;
5921 	row->fieldnames = palloc(sizeof(char *));
5922 	row->varnos = palloc(sizeof(int));
5923 	row->fieldnames[0] = initial_name;
5924 	row->varnos[0] = initial_datum->dno;
5925 
5926 	plpgsql_adddatum((PLpgSQL_datum *)row);
5927 
5928 	return row;
5929 }
5930 
5931 /*
5932  * When the PL/pgSQL parser expects to see a SQL statement, it is very
5933  * liberal in what it accepts; for example, we often assume an
5934  * unrecognized keyword is the beginning of a SQL statement. This
5935  * avoids the need to duplicate parts of the SQL grammar in the
5936  * PL/pgSQL grammar, but it means we can accept wildly malformed
5937  * input. To try and catch some of the more obviously invalid input,
5938  * we run the strings we expect to be SQL statements through the main
5939  * SQL parser.
5940  *
5941  * We only invoke the raw parser (not the analyzer); this doesn't do
5942  * any database access and does not check any semantic rules, it just
5943  * checks for basic syntactic correctness. We do this here, rather
5944  * than after parsing has finished, because a malformed SQL statement
5945  * may cause the PL/pgSQL parser to become confused about statement
5946  * borders. So it is best to bail out as early as we can.
5947  *
5948  * It is assumed that "stmt" represents a copy of the function source text
5949  * beginning at offset "location", with leader text of length "leaderlen"
5950  * (typically "SELECT ") prefixed to the source text.  We use this assumption
5951  * to transpose any error cursor position back to the function source text.
5952  * If no error cursor is provided, we'll just point at "location".
5953  */
5954 static void
check_sql_expr(const char * stmt,int location,int leaderlen)5955 check_sql_expr(const char *stmt, int location, int leaderlen)
5956 {
5957 	sql_error_callback_arg cbarg;
5958 	ErrorContextCallback  syntax_errcontext;
5959 	MemoryContext oldCxt;
5960 
5961 	if (!plpgsql_check_syntax)
5962 		return;
5963 
5964 	cbarg.location = location;
5965 	cbarg.leaderlen = leaderlen;
5966 
5967 	syntax_errcontext.callback = plpgsql_sql_error_callback;
5968 	syntax_errcontext.arg = &cbarg;
5969 	syntax_errcontext.previous = error_context_stack;
5970 	error_context_stack = &syntax_errcontext;
5971 
5972 	oldCxt = MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
5973 	(void) raw_parser(stmt);
5974 	MemoryContextSwitchTo(oldCxt);
5975 
5976 	/* Restore former ereport callback */
5977 	error_context_stack = syntax_errcontext.previous;
5978 }
5979 
5980 static void
plpgsql_sql_error_callback(void * arg)5981 plpgsql_sql_error_callback(void *arg)
5982 {
5983 	sql_error_callback_arg *cbarg = (sql_error_callback_arg *) arg;
5984 	int			errpos;
5985 
5986 	/*
5987 	 * First, set up internalerrposition to point to the start of the
5988 	 * statement text within the function text.  Note this converts
5989 	 * location (a byte offset) to a character number.
5990 	 */
5991 	parser_errposition(cbarg->location);
5992 
5993 	/*
5994 	 * If the core parser provided an error position, transpose it.
5995 	 * Note we are dealing with 1-based character numbers at this point.
5996 	 */
5997 	errpos = geterrposition();
5998 	if (errpos > cbarg->leaderlen)
5999 	{
6000 		int		myerrpos = getinternalerrposition();
6001 
6002 		if (myerrpos > 0)		/* safety check */
6003 			internalerrposition(myerrpos + errpos - cbarg->leaderlen - 1);
6004 	}
6005 
6006 	/* In any case, flush errposition --- we want internalerrpos only */
6007 	errposition(0);
6008 }
6009 
6010 /*
6011  * Parse a SQL datatype name and produce a PLpgSQL_type structure.
6012  *
6013  * The heavy lifting is done elsewhere.  Here we are only concerned
6014  * with setting up an errcontext link that will let us give an error
6015  * cursor pointing into the plpgsql function source, if necessary.
6016  * This is handled the same as in check_sql_expr(), and we likewise
6017  * expect that the given string is a copy from the source text.
6018  */
6019 static PLpgSQL_type *
parse_datatype(const char * string,int location)6020 parse_datatype(const char *string, int location)
6021 {
6022 	TypeName   *typeName;
6023 	Oid			type_id;
6024 	int32		typmod;
6025 	sql_error_callback_arg cbarg;
6026 	ErrorContextCallback  syntax_errcontext;
6027 
6028 	cbarg.location = location;
6029 	cbarg.leaderlen = 0;
6030 
6031 	syntax_errcontext.callback = plpgsql_sql_error_callback;
6032 	syntax_errcontext.arg = &cbarg;
6033 	syntax_errcontext.previous = error_context_stack;
6034 	error_context_stack = &syntax_errcontext;
6035 
6036 	/* Let the main parser try to parse it under standard SQL rules */
6037 	typeName = typeStringToTypeName(string);
6038 	typenameTypeIdAndMod(NULL, typeName, &type_id, &typmod);
6039 
6040 	/* Restore former ereport callback */
6041 	error_context_stack = syntax_errcontext.previous;
6042 
6043 	/* Okay, build a PLpgSQL_type data structure for it */
6044 	return plpgsql_build_datatype(type_id, typmod,
6045 								  plpgsql_curr_compile->fn_input_collation,
6046 								  typeName);
6047 }
6048 
6049 /*
6050  * Check block starting and ending labels match.
6051  */
6052 static void
check_labels(const char * start_label,const char * end_label,int end_location)6053 check_labels(const char *start_label, const char *end_label, int end_location)
6054 {
6055 	if (end_label)
6056 	{
6057 		if (!start_label)
6058 			ereport(ERROR,
6059 					(errcode(ERRCODE_SYNTAX_ERROR),
6060 					 errmsg("end label \"%s\" specified for unlabelled block",
6061 							end_label),
6062 					 parser_errposition(end_location)));
6063 
6064 		if (strcmp(start_label, end_label) != 0)
6065 			ereport(ERROR,
6066 					(errcode(ERRCODE_SYNTAX_ERROR),
6067 					 errmsg("end label \"%s\" differs from block's label \"%s\"",
6068 							end_label, start_label),
6069 					 parser_errposition(end_location)));
6070 	}
6071 }
6072 
6073 /*
6074  * Read the arguments (if any) for a cursor, followed by the until token
6075  *
6076  * If cursor has no args, just swallow the until token and return NULL.
6077  * If it does have args, we expect to see "( arg [, arg ...] )" followed
6078  * by the until token, where arg may be a plain expression, or a named
6079  * parameter assignment of the form argname := expr. Consume all that and
6080  * return a SELECT query that evaluates the expression(s) (without the outer
6081  * parens).
6082  */
6083 static PLpgSQL_expr *
read_cursor_args(PLpgSQL_var * cursor,int until,const char * expected)6084 read_cursor_args(PLpgSQL_var *cursor, int until, const char *expected)
6085 {
6086 	PLpgSQL_expr *expr;
6087 	PLpgSQL_row *row;
6088 	int			tok;
6089 	int			argc;
6090 	char	  **argv;
6091 	StringInfoData ds;
6092 	char	   *sqlstart = "SELECT ";
6093 	bool		any_named = false;
6094 
6095 	tok = yylex();
6096 	if (cursor->cursor_explicit_argrow < 0)
6097 	{
6098 		/* No arguments expected */
6099 		if (tok == '(')
6100 			ereport(ERROR,
6101 					(errcode(ERRCODE_SYNTAX_ERROR),
6102 					 errmsg("cursor \"%s\" has no arguments",
6103 							cursor->refname),
6104 					 parser_errposition(yylloc)));
6105 
6106 		if (tok != until)
6107 			yyerror("syntax error");
6108 
6109 		return NULL;
6110 	}
6111 
6112 	/* Else better provide arguments */
6113 	if (tok != '(')
6114 		ereport(ERROR,
6115 				(errcode(ERRCODE_SYNTAX_ERROR),
6116 				 errmsg("cursor \"%s\" has arguments",
6117 						cursor->refname),
6118 				 parser_errposition(yylloc)));
6119 
6120 	/*
6121 	 * Read the arguments, one by one.
6122 	 */
6123 	row = (PLpgSQL_row *) plpgsql_Datums[cursor->cursor_explicit_argrow];
6124 	argv = (char **) palloc0(row->nfields * sizeof(char *));
6125 
6126 	for (argc = 0; argc < row->nfields; argc++)
6127 	{
6128 		PLpgSQL_expr *item;
6129 		int		endtoken;
6130 		int		argpos;
6131 		int		tok1,
6132 				tok2;
6133 		int		arglocation;
6134 
6135 		/* Check if it's a named parameter: "param := value" */
6136 		plpgsql_peek2(&tok1, &tok2, &arglocation, NULL);
6137 		if (tok1 == IDENT && tok2 == COLON_EQUALS)
6138 		{
6139 			char   *argname;
6140 			IdentifierLookup save_IdentifierLookup;
6141 
6142 			/* Read the argument name, ignoring any matching variable */
6143 			save_IdentifierLookup = plpgsql_IdentifierLookup;
6144 			plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
6145 			yylex();
6146 			argname = yylval.str;
6147 			plpgsql_IdentifierLookup = save_IdentifierLookup;
6148 
6149 			/* Match argument name to cursor arguments */
6150 			for (argpos = 0; argpos < row->nfields; argpos++)
6151 			{
6152 				if (strcmp(row->fieldnames[argpos], argname) == 0)
6153 					break;
6154 			}
6155 			if (argpos == row->nfields)
6156 				ereport(ERROR,
6157 						(errcode(ERRCODE_SYNTAX_ERROR),
6158 						 errmsg("cursor \"%s\" has no argument named \"%s\"",
6159 								cursor->refname, argname),
6160 						 parser_errposition(yylloc)));
6161 
6162 			/*
6163 			 * Eat the ":=". We already peeked, so the error should never
6164 			 * happen.
6165 			 */
6166 			tok2 = yylex();
6167 			if (tok2 != COLON_EQUALS)
6168 				yyerror("syntax error");
6169 
6170 			any_named = true;
6171 		}
6172 		else
6173 			argpos = argc;
6174 
6175 		if (argv[argpos] != NULL)
6176 			ereport(ERROR,
6177 					(errcode(ERRCODE_SYNTAX_ERROR),
6178 					 errmsg("value for parameter \"%s\" of cursor \"%s\" specified more than once",
6179 							row->fieldnames[argpos], cursor->refname),
6180 					 parser_errposition(arglocation)));
6181 
6182 		/*
6183 		 * Read the value expression. To provide the user with meaningful
6184 		 * parse error positions, we check the syntax immediately, instead of
6185 		 * checking the final expression that may have the arguments
6186 		 * reordered. Trailing whitespace must not be trimmed, because
6187 		 * otherwise input of the form (param -- comment\n, param) would be
6188 		 * translated into a form where the second parameter is commented
6189 		 * out.
6190 		 */
6191 		item = read_sql_construct(',', ')', 0,
6192 								  ",\" or \")",
6193 								  sqlstart,
6194 								  true, true,
6195 								  false, /* do not trim */
6196 								  NULL, &endtoken);
6197 
6198 		argv[argpos] = item->query + strlen(sqlstart);
6199 
6200 		if (endtoken == ')' && !(argc == row->nfields - 1))
6201 			ereport(ERROR,
6202 					(errcode(ERRCODE_SYNTAX_ERROR),
6203 					 errmsg("not enough arguments for cursor \"%s\"",
6204 							cursor->refname),
6205 					 parser_errposition(yylloc)));
6206 
6207 		if (endtoken == ',' && (argc == row->nfields - 1))
6208 			ereport(ERROR,
6209 					(errcode(ERRCODE_SYNTAX_ERROR),
6210 					 errmsg("too many arguments for cursor \"%s\"",
6211 							cursor->refname),
6212 					 parser_errposition(yylloc)));
6213 	}
6214 
6215 	/* Make positional argument list */
6216 	initStringInfo(&ds);
6217 	appendStringInfoString(&ds, sqlstart);
6218 	for (argc = 0; argc < row->nfields; argc++)
6219 	{
6220 		Assert(argv[argc] != NULL);
6221 
6222 		/*
6223 		 * Because named notation allows permutated argument lists, include
6224 		 * the parameter name for meaningful runtime errors.
6225 		 */
6226 		appendStringInfoString(&ds, argv[argc]);
6227 		if (any_named)
6228 			appendStringInfo(&ds, " AS %s",
6229 							 quote_identifier(row->fieldnames[argc]));
6230 		if (argc < row->nfields - 1)
6231 			appendStringInfoString(&ds, ", ");
6232 	}
6233 	appendStringInfoChar(&ds, ';');
6234 
6235 	expr = palloc0(sizeof(PLpgSQL_expr));
6236 	expr->query			= pstrdup(ds.data);
6237 	expr->plan			= NULL;
6238 	expr->paramnos		= NULL;
6239 	expr->rwparam		= -1;
6240 	expr->ns            = plpgsql_ns_top();
6241 	pfree(ds.data);
6242 
6243 	/* Next we'd better find the until token */
6244 	tok = yylex();
6245 	if (tok != until)
6246 		yyerror("syntax error");
6247 
6248 	return expr;
6249 }
6250 
6251 /*
6252  * Parse RAISE ... USING options
6253  */
6254 static List *
read_raise_options(void)6255 read_raise_options(void)
6256 {
6257 	List	   *result = NIL;
6258 
6259 	for (;;)
6260 	{
6261 		PLpgSQL_raise_option *opt;
6262 		int		tok;
6263 
6264 		if ((tok = yylex()) == 0)
6265 			yyerror("unexpected end of function definition");
6266 
6267 		opt = (PLpgSQL_raise_option *) palloc(sizeof(PLpgSQL_raise_option));
6268 
6269 		if (tok_is_keyword(tok, &yylval,
6270 						   K_ERRCODE, "errcode"))
6271 			opt->opt_type = PLPGSQL_RAISEOPTION_ERRCODE;
6272 		else if (tok_is_keyword(tok, &yylval,
6273 								K_MESSAGE, "message"))
6274 			opt->opt_type = PLPGSQL_RAISEOPTION_MESSAGE;
6275 		else if (tok_is_keyword(tok, &yylval,
6276 								K_DETAIL, "detail"))
6277 			opt->opt_type = PLPGSQL_RAISEOPTION_DETAIL;
6278 		else if (tok_is_keyword(tok, &yylval,
6279 								K_HINT, "hint"))
6280 			opt->opt_type = PLPGSQL_RAISEOPTION_HINT;
6281 		else if (tok_is_keyword(tok, &yylval,
6282 								K_COLUMN, "column"))
6283 			opt->opt_type = PLPGSQL_RAISEOPTION_COLUMN;
6284 		else if (tok_is_keyword(tok, &yylval,
6285 								K_CONSTRAINT, "constraint"))
6286 			opt->opt_type = PLPGSQL_RAISEOPTION_CONSTRAINT;
6287 		else if (tok_is_keyword(tok, &yylval,
6288 								K_DATATYPE, "datatype"))
6289 			opt->opt_type = PLPGSQL_RAISEOPTION_DATATYPE;
6290 		else if (tok_is_keyword(tok, &yylval,
6291 								K_TABLE, "table"))
6292 			opt->opt_type = PLPGSQL_RAISEOPTION_TABLE;
6293 		else if (tok_is_keyword(tok, &yylval,
6294 								K_SCHEMA, "schema"))
6295 			opt->opt_type = PLPGSQL_RAISEOPTION_SCHEMA;
6296 		else
6297 			yyerror("unrecognized RAISE statement option");
6298 
6299 		tok = yylex();
6300 		if (tok != '=' && tok != COLON_EQUALS)
6301 			yyerror("syntax error, expected \"=\"");
6302 
6303 		opt->expr = read_sql_expression2(',', ';', ", or ;", &tok);
6304 
6305 		result = lappend(result, opt);
6306 
6307 		if (tok == ';')
6308 			break;
6309 	}
6310 
6311 	return result;
6312 }
6313 
6314 /*
6315  * Check that the number of parameter placeholders in the message matches the
6316  * number of parameters passed to it, if a message was given.
6317  */
6318 static void
check_raise_parameters(PLpgSQL_stmt_raise * stmt)6319 check_raise_parameters(PLpgSQL_stmt_raise *stmt)
6320 {
6321 	char	   *cp;
6322 	int			expected_nparams = 0;
6323 
6324 	if (stmt->message == NULL)
6325 		return;
6326 
6327 	for (cp = stmt->message; *cp; cp++)
6328 	{
6329 		if (cp[0] == '%')
6330 		{
6331 			/* ignore literal % characters */
6332 			if (cp[1] == '%')
6333 				cp++;
6334 			else
6335 				expected_nparams++;
6336 		}
6337 	}
6338 
6339 	if (expected_nparams < list_length(stmt->params))
6340 		ereport(ERROR,
6341 				(errcode(ERRCODE_SYNTAX_ERROR),
6342 				errmsg("too many parameters specified for RAISE")));
6343 	if (expected_nparams > list_length(stmt->params))
6344 		ereport(ERROR,
6345 				(errcode(ERRCODE_SYNTAX_ERROR),
6346 				errmsg("too few parameters specified for RAISE")));
6347 }
6348 
6349 /*
6350  * Fix up CASE statement
6351  */
6352 static PLpgSQL_stmt *
make_case(int location,PLpgSQL_expr * t_expr,List * case_when_list,List * else_stmts)6353 make_case(int location, PLpgSQL_expr *t_expr,
6354 		  List *case_when_list, List *else_stmts)
6355 {
6356 	PLpgSQL_stmt_case	*new;
6357 
6358 	new = palloc(sizeof(PLpgSQL_stmt_case));
6359 	new->cmd_type = PLPGSQL_STMT_CASE;
6360 	new->lineno = plpgsql_location_to_lineno(location);
6361 	new->t_expr = t_expr;
6362 	new->t_varno = 0;
6363 	new->case_when_list = case_when_list;
6364 	new->have_else = (else_stmts != NIL);
6365 	/* Get rid of list-with-NULL hack */
6366 	if (list_length(else_stmts) == 1 && linitial(else_stmts) == NULL)
6367 		new->else_stmts = NIL;
6368 	else
6369 		new->else_stmts = else_stmts;
6370 
6371 	/*
6372 	 * When test expression is present, we create a var for it and then
6373 	 * convert all the WHEN expressions to "VAR IN (original_expression)".
6374 	 * This is a bit klugy, but okay since we haven't yet done more than
6375 	 * read the expressions as text.  (Note that previous parsing won't
6376 	 * have complained if the WHEN ... THEN expression contained multiple
6377 	 * comma-separated values.)
6378 	 */
6379 	if (t_expr)
6380 	{
6381 		char	varname[32];
6382 		PLpgSQL_var *t_var;
6383 		ListCell *l;
6384 
6385 		/* use a name unlikely to collide with any user names */
6386 		snprintf(varname, sizeof(varname), "__Case__Variable_%d__",
6387 				 plpgsql_nDatums);
6388 
6389 		/*
6390 		 * We don't yet know the result datatype of t_expr.  Build the
6391 		 * variable as if it were INT4; we'll fix this at runtime if needed.
6392 		 */
6393 		t_var = (PLpgSQL_var *)
6394 			plpgsql_build_variable(varname, new->lineno,
6395 								   plpgsql_build_datatype(INT4OID,
6396 														  -1,
6397 														  InvalidOid,
6398 														  NULL),
6399 								   true);
6400 		new->t_varno = t_var->dno;
6401 
6402 		foreach(l, case_when_list)
6403 		{
6404 			PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
6405 			PLpgSQL_expr *expr = cwt->expr;
6406 			StringInfoData	ds;
6407 
6408 			/* copy expression query without SELECT keyword (expr->query + 7) */
6409 			Assert(strncmp(expr->query, "SELECT ", 7) == 0);
6410 
6411 			/* And do the string hacking */
6412 			initStringInfo(&ds);
6413 
6414 			appendStringInfo(&ds, "SELECT \"%s\" IN (%s)",
6415 							 varname, expr->query + 7);
6416 
6417 			pfree(expr->query);
6418 			expr->query = pstrdup(ds.data);
6419 			/* Adjust expr's namespace to include the case variable */
6420 			expr->ns = plpgsql_ns_top();
6421 
6422 			pfree(ds.data);
6423 		}
6424 	}
6425 
6426 	return (PLpgSQL_stmt *) new;
6427 }
6428