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