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