2 /* YACC parser for Fortran expressions, for GDB.
3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
5 Contributed by Motorola. Adapted from the C parser by Farooq Butt
6 (fmbutt@engage.sps.mot.com).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* This was blantantly ripped off the C expression parser, please
24 be aware of that as you look at its basic structure -FMB */
26 /* Parse a F77 expression from text in a string,
27 and return the result as a struct expression pointer.
28 That structure contains arithmetic operations in reverse polish,
29 with constants represented by operations that are followed by special data.
30 See expression.h for the details of the format.
31 What is important here is that it can be built up sequentially
32 during the process of parsing; the lower levels of the tree always
33 come first in the result.
35 Note that malloc's and realloc's in this file are transformed to
36 xmalloc and xrealloc respectively by the same sed command in the
37 makefile that remaps any other malloc/realloc inserted by the parser
38 generator. Doing this with #defines and trying to control the interaction
39 with include files (<malloc.h> and <stdlib.h> for example) just became
40 too messy, particularly when such includes can be inserted at random
41 times by the parser generator. */
46 #include "expression.h"
48 #include "parser-defs.h"
51 #include "bfd.h" /* Required by objfiles.h. */
52 #include "symfile.h" /* Required by objfiles.h. */
53 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
58 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
59 #define parse_f_type(ps) builtin_f_type (parse_gdbarch (ps))
61 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
63 #define GDB_YY_REMAP_PREFIX f_
66 /* The state of the parser, used internally when we are parsing the
69 static struct parser_state *pstate = NULL;
73 static int yylex (void);
75 static void yyerror (const char *);
77 static void growbuf_by_size (int);
79 static int match_string_literal (void);
81 static void push_kind_type (LONGEST val, struct type *type);
83 static struct type *convert_to_kind_type (struct type *basetype, int kind);
87 /* Although the yacc "value" of an expression is not used,
88 since the result is stored in the structure being created,
89 other node types do have values. */
106 struct symtoken ssym;
109 enum exp_opcode opcode;
110 struct internalvar *ivar;
117 /* YYSTYPE gets defined by %union */
118 static int parse_number (struct parser_state *, const char *, int,
122 %type <voidval> exp type_exp start variable
123 %type <tval> type typebase
124 %type <tvec> nonempty_typelist
125 /* %type <bval> block */
127 /* Fancy type parsing. */
128 %type <voidval> func_mod direct_abs_decl abs_decl
131 %token <typed_val> INT
132 %token <typed_val_float> FLOAT
134 /* Both NAME and TYPENAME tokens represent symbols in the input,
135 and both convey their data as strings.
136 But a TYPENAME is a string that happens to be defined as a typedef
137 or builtin type name (such as int or char)
138 and a NAME is any other symbol.
139 Contexts where this distinction is not important can use the
140 nonterminal "name", which matches either NAME or TYPENAME. */
142 %token <sval> STRING_LITERAL
143 %token <lval> BOOLEAN_LITERAL
145 %token <tsym> TYPENAME
147 %type <ssym> name_not_typename
149 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
150 but which would parse as a valid number in the current input radix.
151 E.g. "c" when input_radix==16. Depending on the parse, it will be
152 turned into a name or into a number. */
154 %token <ssym> NAME_OR_INT
159 /* Special type cases, put in to allow the parser to distinguish different
161 %token INT_KEYWORD INT_S2_KEYWORD LOGICAL_S1_KEYWORD LOGICAL_S2_KEYWORD
162 %token LOGICAL_S8_KEYWORD
163 %token LOGICAL_KEYWORD REAL_KEYWORD REAL_S8_KEYWORD REAL_S16_KEYWORD
164 %token COMPLEX_S8_KEYWORD COMPLEX_S16_KEYWORD COMPLEX_S32_KEYWORD
165 %token BOOL_AND BOOL_OR BOOL_NOT
166 %token <lval> CHARACTER
168 %token <voidval> DOLLAR_VARIABLE
170 %token <opcode> ASSIGN_MODIFY
171 %token <opcode> UNOP_INTRINSIC
175 %right '=' ASSIGN_MODIFY
184 %left LESSTHAN GREATERTHAN LEQ GEQ
202 { write_exp_elt_opcode (pstate, OP_TYPE);
203 write_exp_elt_type (pstate, $1);
204 write_exp_elt_opcode (pstate, OP_TYPE); }
211 /* Expressions, not including the comma operator. */
212 exp : '*' exp %prec UNARY
213 { write_exp_elt_opcode (pstate, UNOP_IND); }
216 exp : '&' exp %prec UNARY
217 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
220 exp : '-' exp %prec UNARY
221 { write_exp_elt_opcode (pstate, UNOP_NEG); }
224 exp : BOOL_NOT exp %prec UNARY
225 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
228 exp : '~' exp %prec UNARY
229 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
232 exp : SIZEOF exp %prec UNARY
233 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
236 exp : KIND '(' exp ')' %prec UNARY
237 { write_exp_elt_opcode (pstate, UNOP_KIND); }
240 /* No more explicit array operators, we treat everything in F77 as
241 a function call. The disambiguation as to whether we are
242 doing a subscript operation or a function call is done
246 { start_arglist (); }
248 { write_exp_elt_opcode (pstate,
249 OP_F77_UNDETERMINED_ARGLIST);
250 write_exp_elt_longcst (pstate,
251 (LONGEST) end_arglist ());
252 write_exp_elt_opcode (pstate,
253 OP_F77_UNDETERMINED_ARGLIST); }
256 exp : UNOP_INTRINSIC '(' exp ')'
257 { write_exp_elt_opcode (pstate, $1); }
271 arglist : arglist ',' exp %prec ABOVE_COMMA
275 /* There are four sorts of subrange types in F90. */
277 subrange: exp ':' exp %prec ABOVE_COMMA
278 { write_exp_elt_opcode (pstate, OP_RANGE);
279 write_exp_elt_longcst (pstate, NONE_BOUND_DEFAULT);
280 write_exp_elt_opcode (pstate, OP_RANGE); }
283 subrange: exp ':' %prec ABOVE_COMMA
284 { write_exp_elt_opcode (pstate, OP_RANGE);
285 write_exp_elt_longcst (pstate, HIGH_BOUND_DEFAULT);
286 write_exp_elt_opcode (pstate, OP_RANGE); }
289 subrange: ':' exp %prec ABOVE_COMMA
290 { write_exp_elt_opcode (pstate, OP_RANGE);
291 write_exp_elt_longcst (pstate, LOW_BOUND_DEFAULT);
292 write_exp_elt_opcode (pstate, OP_RANGE); }
295 subrange: ':' %prec ABOVE_COMMA
296 { write_exp_elt_opcode (pstate, OP_RANGE);
297 write_exp_elt_longcst (pstate, BOTH_BOUND_DEFAULT);
298 write_exp_elt_opcode (pstate, OP_RANGE); }
301 complexnum: exp ',' exp
305 exp : '(' complexnum ')'
306 { write_exp_elt_opcode (pstate, OP_COMPLEX);
307 write_exp_elt_type (pstate,
308 parse_f_type (pstate)
309 ->builtin_complex_s16);
310 write_exp_elt_opcode (pstate, OP_COMPLEX); }
313 exp : '(' type ')' exp %prec UNARY
314 { write_exp_elt_opcode (pstate, UNOP_CAST);
315 write_exp_elt_type (pstate, $2);
316 write_exp_elt_opcode (pstate, UNOP_CAST); }
320 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
321 write_exp_string (pstate, $3);
322 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
325 /* Binary operators in order of decreasing precedence. */
328 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
331 exp : exp STARSTAR exp
332 { write_exp_elt_opcode (pstate, BINOP_EXP); }
336 { write_exp_elt_opcode (pstate, BINOP_MUL); }
340 { write_exp_elt_opcode (pstate, BINOP_DIV); }
344 { write_exp_elt_opcode (pstate, BINOP_ADD); }
348 { write_exp_elt_opcode (pstate, BINOP_SUB); }
352 { write_exp_elt_opcode (pstate, BINOP_LSH); }
356 { write_exp_elt_opcode (pstate, BINOP_RSH); }
360 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
363 exp : exp NOTEQUAL exp
364 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
368 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
372 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
375 exp : exp LESSTHAN exp
376 { write_exp_elt_opcode (pstate, BINOP_LESS); }
379 exp : exp GREATERTHAN exp
380 { write_exp_elt_opcode (pstate, BINOP_GTR); }
384 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
388 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
392 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
395 exp : exp BOOL_AND exp
396 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
400 exp : exp BOOL_OR exp
401 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
405 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
408 exp : exp ASSIGN_MODIFY exp
409 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
410 write_exp_elt_opcode (pstate, $2);
411 write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
415 { write_exp_elt_opcode (pstate, OP_LONG);
416 write_exp_elt_type (pstate, $1.type);
417 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
418 write_exp_elt_opcode (pstate, OP_LONG); }
423 parse_number (pstate, $1.stoken.ptr,
424 $1.stoken.length, 0, &val);
425 write_exp_elt_opcode (pstate, OP_LONG);
426 write_exp_elt_type (pstate, val.typed_val.type);
427 write_exp_elt_longcst (pstate,
428 (LONGEST)val.typed_val.val);
429 write_exp_elt_opcode (pstate, OP_LONG); }
433 { write_exp_elt_opcode (pstate, OP_FLOAT);
434 write_exp_elt_type (pstate, $1.type);
435 write_exp_elt_floatcst (pstate, $1.val);
436 write_exp_elt_opcode (pstate, OP_FLOAT); }
442 exp : DOLLAR_VARIABLE
445 exp : SIZEOF '(' type ')' %prec UNARY
446 { write_exp_elt_opcode (pstate, OP_LONG);
447 write_exp_elt_type (pstate,
448 parse_f_type (pstate)
450 $3 = check_typedef ($3);
451 write_exp_elt_longcst (pstate,
452 (LONGEST) TYPE_LENGTH ($3));
453 write_exp_elt_opcode (pstate, OP_LONG); }
456 exp : BOOLEAN_LITERAL
457 { write_exp_elt_opcode (pstate, OP_BOOL);
458 write_exp_elt_longcst (pstate, (LONGEST) $1);
459 write_exp_elt_opcode (pstate, OP_BOOL);
465 write_exp_elt_opcode (pstate, OP_STRING);
466 write_exp_string (pstate, $1);
467 write_exp_elt_opcode (pstate, OP_STRING);
471 variable: name_not_typename
472 { struct block_symbol sym = $1.sym;
476 if (symbol_read_needs_frame (sym.symbol))
477 innermost_block.update (sym);
478 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
479 write_exp_elt_block (pstate, sym.block);
480 write_exp_elt_sym (pstate, sym.symbol);
481 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
486 struct bound_minimal_symbol msymbol;
487 char *arg = copy_name ($1.stoken);
490 lookup_bound_minimal_symbol (arg);
491 if (msymbol.minsym != NULL)
492 write_exp_msymbol (pstate, msymbol);
493 else if (!have_full_symbols () && !have_partial_symbols ())
494 error (_("No symbol table is loaded. Use the \"file\" command."));
496 error (_("No symbol \"%s\" in current context."),
497 copy_name ($1.stoken));
509 /* This is where the interesting stuff happens. */
512 struct type *follow_type = $1;
513 struct type *range_type;
522 follow_type = lookup_pointer_type (follow_type);
525 follow_type = lookup_lvalue_reference_type (follow_type);
528 array_size = pop_type_int ();
529 if (array_size != -1)
532 create_static_range_type ((struct type *) NULL,
533 parse_f_type (pstate)
537 create_array_type ((struct type *) NULL,
538 follow_type, range_type);
541 follow_type = lookup_pointer_type (follow_type);
544 follow_type = lookup_function_type (follow_type);
548 int kind_val = pop_type_int ();
550 = convert_to_kind_type (follow_type, kind_val);
559 { push_type (tp_pointer); $$ = 0; }
561 { push_type (tp_pointer); $$ = $2; }
563 { push_type (tp_reference); $$ = 0; }
565 { push_type (tp_reference); $$ = $2; }
569 direct_abs_decl: '(' abs_decl ')'
571 | '(' KIND '=' INT ')'
572 { push_kind_type ($4.val, $4.type); }
573 | direct_abs_decl func_mod
574 { push_type (tp_function); }
576 { push_type (tp_function); }
581 | '(' nonempty_typelist ')'
582 { free ($2); $$ = 0; }
585 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
589 { $$ = parse_f_type (pstate)->builtin_integer; }
591 { $$ = parse_f_type (pstate)->builtin_integer_s2; }
593 { $$ = parse_f_type (pstate)->builtin_character; }
595 { $$ = parse_f_type (pstate)->builtin_logical_s8; }
597 { $$ = parse_f_type (pstate)->builtin_logical; }
599 { $$ = parse_f_type (pstate)->builtin_logical_s2; }
601 { $$ = parse_f_type (pstate)->builtin_logical_s1; }
603 { $$ = parse_f_type (pstate)->builtin_real; }
605 { $$ = parse_f_type (pstate)->builtin_real_s8; }
607 { $$ = parse_f_type (pstate)->builtin_real_s16; }
609 { $$ = parse_f_type (pstate)->builtin_complex_s8; }
610 | COMPLEX_S16_KEYWORD
611 { $$ = parse_f_type (pstate)->builtin_complex_s16; }
612 | COMPLEX_S32_KEYWORD
613 { $$ = parse_f_type (pstate)->builtin_complex_s32; }
618 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
619 $<ivec>$[0] = 1; /* Number of types in vector */
622 | nonempty_typelist ',' type
623 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
624 $$ = (struct type **) realloc ((char *) $1, len);
625 $$[$<ivec>$[0]] = $3;
633 name_not_typename : NAME
634 /* These would be useful if name_not_typename was useful, but it is just
635 a fake for "variable", so these cause reduce/reduce conflicts because
636 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
637 =exp) or just an exp. If name_not_typename was ever used in an lvalue
638 context where only a name could occur, this might be useful.
645 /* Take care of parsing a number (anything that starts with a digit).
646 Set yylval and return the token type; update lexptr.
647 LEN is the number of characters in it. */
649 /*** Needs some error checking for the float case ***/
652 parse_number (struct parser_state *par_state,
653 const char *p, int len, int parsed_float, YYSTYPE *putithere)
658 int base = input_radix;
662 struct type *signed_type;
663 struct type *unsigned_type;
667 /* It's a float since it contains a point or an exponent. */
668 /* [dD] is not understood as an exponent by parse_float,
673 for (tmp2 = tmp; *tmp2; ++tmp2)
674 if (*tmp2 == 'd' || *tmp2 == 'D')
677 /* FIXME: Should this use different types? */
678 putithere->typed_val_float.type = parse_f_type (pstate)->builtin_real_s8;
679 bool parsed = parse_float (tmp, len,
680 putithere->typed_val_float.type,
681 putithere->typed_val_float.val);
683 return parsed? FLOAT : ERROR;
686 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
722 if (len == 0 && c == 'l')
724 else if (len == 0 && c == 'u')
729 if (c >= '0' && c <= '9')
731 else if (c >= 'a' && c <= 'f')
734 return ERROR; /* Char not a digit */
736 return ERROR; /* Invalid digit in this base */
740 /* Portably test for overflow (only works for nonzero values, so make
741 a second check for zero). */
742 if ((prevn >= n) && n != 0)
743 unsigned_p=1; /* Try something unsigned */
744 /* If range checking enabled, portably test for unsigned overflow. */
745 if (RANGE_CHECK && n != 0)
747 if ((unsigned_p && (unsigned)prevn >= (unsigned)n))
748 range_error (_("Overflow on numeric constant."));
753 /* If the number is too big to be an int, or it's got an l suffix
754 then it's a long. Work out if this has to be a long by
755 shifting right and seeing if anything remains, and the
756 target int size is different to the target long size.
758 In the expression below, we could have tested
759 (n >> gdbarch_int_bit (parse_gdbarch))
760 to see if it was zero,
761 but too many compilers warn about that, when ints and longs
762 are the same size. So we shift it twice, with fewer bits
763 each time, for the same result. */
765 if ((gdbarch_int_bit (parse_gdbarch (par_state))
766 != gdbarch_long_bit (parse_gdbarch (par_state))
768 >> (gdbarch_int_bit (parse_gdbarch (par_state))-2))) /* Avoid
772 high_bit = ((ULONGEST)1)
773 << (gdbarch_long_bit (parse_gdbarch (par_state))-1);
774 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
775 signed_type = parse_type (par_state)->builtin_long;
780 ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
781 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
782 signed_type = parse_type (par_state)->builtin_int;
785 putithere->typed_val.val = n;
787 /* If the high bit of the worked out type is set then this number
788 has to be unsigned. */
790 if (unsigned_p || (n & high_bit))
791 putithere->typed_val.type = unsigned_type;
793 putithere->typed_val.type = signed_type;
798 /* Called to setup the type stack when we encounter a '(kind=N)' type
799 modifier, performs some bounds checking on 'N' and then pushes this to
800 the type stack followed by the 'tp_kind' marker. */
802 push_kind_type (LONGEST val, struct type *type)
806 if (TYPE_UNSIGNED (type))
808 ULONGEST uval = static_cast <ULONGEST> (val);
810 error (_("kind value out of range"));
811 ival = static_cast <int> (uval);
815 if (val > INT_MAX || val < 0)
816 error (_("kind value out of range"));
817 ival = static_cast <int> (val);
820 push_type_int (ival);
824 /* Called when a type has a '(kind=N)' modifier after it, for example
825 'character(kind=1)'. The BASETYPE is the type described by 'character'
826 in our example, and KIND is the integer '1'. This function returns a
827 new type that represents the basetype of a specific kind. */
829 convert_to_kind_type (struct type *basetype, int kind)
831 if (basetype == parse_f_type (pstate)->builtin_character)
833 /* Character of kind 1 is a special case, this is the same as the
834 base character type. */
836 return parse_f_type (pstate)->builtin_character;
838 else if (basetype == parse_f_type (pstate)->builtin_complex_s8)
841 return parse_f_type (pstate)->builtin_complex_s8;
843 return parse_f_type (pstate)->builtin_complex_s16;
845 return parse_f_type (pstate)->builtin_complex_s32;
847 else if (basetype == parse_f_type (pstate)->builtin_real)
850 return parse_f_type (pstate)->builtin_real;
852 return parse_f_type (pstate)->builtin_real_s8;
854 return parse_f_type (pstate)->builtin_real_s16;
856 else if (basetype == parse_f_type (pstate)->builtin_logical)
859 return parse_f_type (pstate)->builtin_logical_s1;
861 return parse_f_type (pstate)->builtin_logical_s2;
863 return parse_f_type (pstate)->builtin_logical;
865 return parse_f_type (pstate)->builtin_logical_s8;
867 else if (basetype == parse_f_type (pstate)->builtin_integer)
870 return parse_f_type (pstate)->builtin_integer_s2;
872 return parse_f_type (pstate)->builtin_integer;
874 return parse_f_type (pstate)->builtin_integer_s8;
877 error (_("unsupported kind %d for type %s"),
878 kind, TYPE_SAFE_NAME (basetype));
880 /* Should never get here. */
886 /* The string to match against. */
889 /* The lexer token to return. */
892 /* The expression opcode to embed within the token. */
893 enum exp_opcode opcode;
895 /* When this is true the string in OPER is matched exactly including
896 case, when this is false OPER is matched case insensitively. */
900 static const struct token dot_ops[] =
902 { ".and.", BOOL_AND, BINOP_END, false },
903 { ".or.", BOOL_OR, BINOP_END, false },
904 { ".not.", BOOL_NOT, BINOP_END, false },
905 { ".eq.", EQUAL, BINOP_END, false },
906 { ".eqv.", EQUAL, BINOP_END, false },
907 { ".neqv.", NOTEQUAL, BINOP_END, false },
908 { ".ne.", NOTEQUAL, BINOP_END, false },
909 { ".le.", LEQ, BINOP_END, false },
910 { ".ge.", GEQ, BINOP_END, false },
911 { ".gt.", GREATERTHAN, BINOP_END, false },
912 { ".lt.", LESSTHAN, BINOP_END, false },
915 /* Holds the Fortran representation of a boolean, and the integer value we
916 substitute in when one of the matching strings is parsed. */
917 struct f77_boolean_val
919 /* The string representing a Fortran boolean. */
922 /* The integer value to replace it with. */
926 /* The set of Fortran booleans. These are matched case insensitively. */
927 static const struct f77_boolean_val boolean_values[] =
933 static const struct token f77_keywords[] =
935 /* Historically these have always been lowercase only in GDB. */
936 { "complex_16", COMPLEX_S16_KEYWORD, BINOP_END, true },
937 { "complex_32", COMPLEX_S32_KEYWORD, BINOP_END, true },
938 { "character", CHARACTER, BINOP_END, true },
939 { "integer_2", INT_S2_KEYWORD, BINOP_END, true },
940 { "logical_1", LOGICAL_S1_KEYWORD, BINOP_END, true },
941 { "logical_2", LOGICAL_S2_KEYWORD, BINOP_END, true },
942 { "logical_8", LOGICAL_S8_KEYWORD, BINOP_END, true },
943 { "complex_8", COMPLEX_S8_KEYWORD, BINOP_END, true },
944 { "integer", INT_KEYWORD, BINOP_END, true },
945 { "logical", LOGICAL_KEYWORD, BINOP_END, true },
946 { "real_16", REAL_S16_KEYWORD, BINOP_END, true },
947 { "complex", COMPLEX_S8_KEYWORD, BINOP_END, true },
948 { "sizeof", SIZEOF, BINOP_END, true },
949 { "real_8", REAL_S8_KEYWORD, BINOP_END, true },
950 { "real", REAL_KEYWORD, BINOP_END, true },
951 /* The following correspond to actual functions in Fortran and are case
953 { "kind", KIND, BINOP_END, false },
954 { "abs", UNOP_INTRINSIC, UNOP_ABS, false }
957 /* Implementation of a dynamically expandable buffer for processing input
958 characters acquired through lexptr and building a value to return in
959 yylval. Ripped off from ch-exp.y */
961 static char *tempbuf; /* Current buffer contents */
962 static int tempbufsize; /* Size of allocated buffer */
963 static int tempbufindex; /* Current index into buffer */
965 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
967 #define CHECKBUF(size) \
969 if (tempbufindex + (size) >= tempbufsize) \
971 growbuf_by_size (size); \
976 /* Grow the static temp buffer if necessary, including allocating the
977 first one on demand. */
980 growbuf_by_size (int count)
984 growby = std::max (count, GROWBY_MIN_SIZE);
985 tempbufsize += growby;
987 tempbuf = (char *) malloc (tempbufsize);
989 tempbuf = (char *) realloc (tempbuf, tempbufsize);
992 /* Blatantly ripped off from ch-exp.y. This routine recognizes F77
995 Recognize a string literal. A string literal is a nonzero sequence
996 of characters enclosed in matching single quotes, except that
997 a single character inside single quotes is a character literal, which
998 we reject as a string literal. To embed the terminator character inside
999 a string, it is simply doubled (I.E. 'this''is''one''string') */
1002 match_string_literal (void)
1004 const char *tokptr = lexptr;
1006 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1009 if (*tokptr == *lexptr)
1011 if (*(tokptr + 1) == *lexptr)
1016 tempbuf[tempbufindex++] = *tokptr;
1018 if (*tokptr == '\0' /* no terminator */
1019 || tempbufindex == 0) /* no string */
1023 tempbuf[tempbufindex] = '\0';
1024 yylval.sval.ptr = tempbuf;
1025 yylval.sval.length = tempbufindex;
1027 return STRING_LITERAL;
1031 /* Read one token, getting characters through lexptr. */
1039 const char *tokstart;
1043 prev_lexptr = lexptr;
1047 /* First of all, let us make sure we are not dealing with the
1048 special tokens .true. and .false. which evaluate to 1 and 0. */
1052 for (int i = 0; i < ARRAY_SIZE (boolean_values); i++)
1054 if (strncasecmp (tokstart, boolean_values[i].name,
1055 strlen (boolean_values[i].name)) == 0)
1057 lexptr += strlen (boolean_values[i].name);
1058 yylval.lval = boolean_values[i].value;
1059 return BOOLEAN_LITERAL;
1064 /* See if it is a special .foo. operator. */
1065 for (int i = 0; i < ARRAY_SIZE (dot_ops); i++)
1066 if (strncasecmp (tokstart, dot_ops[i].oper,
1067 strlen (dot_ops[i].oper)) == 0)
1069 gdb_assert (!dot_ops[i].case_sensitive);
1070 lexptr += strlen (dot_ops[i].oper);
1071 yylval.opcode = dot_ops[i].opcode;
1072 return dot_ops[i].token;
1075 /* See if it is an exponentiation operator. */
1077 if (strncmp (tokstart, "**", 2) == 0)
1080 yylval.opcode = BINOP_EXP;
1084 switch (c = *tokstart)
1096 token = match_string_literal ();
1107 if (paren_depth == 0)
1114 if (comma_terminates && paren_depth == 0)
1120 /* Might be a floating point number. */
1121 if (lexptr[1] < '0' || lexptr[1] > '9')
1122 goto symbol; /* Nope, must be a symbol. */
1136 /* It's a number. */
1137 int got_dot = 0, got_e = 0, got_d = 0, toktype;
1138 const char *p = tokstart;
1139 int hex = input_radix > 10;
1141 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1146 else if (c == '0' && (p[1]=='t' || p[1]=='T'
1147 || p[1]=='d' || p[1]=='D'))
1155 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1156 got_dot = got_e = 1;
1157 else if (!hex && !got_d && (*p == 'd' || *p == 'D'))
1158 got_dot = got_d = 1;
1159 else if (!hex && !got_dot && *p == '.')
1161 else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
1162 || (got_d && (p[-1] == 'd' || p[-1] == 'D')))
1163 && (*p == '-' || *p == '+'))
1164 /* This is the sign of the exponent, not the end of the
1167 /* We will take any letters or digits. parse_number will
1168 complain if past the radix, or if L or U are not final. */
1169 else if ((*p < '0' || *p > '9')
1170 && ((*p < 'a' || *p > 'z')
1171 && (*p < 'A' || *p > 'Z')))
1174 toktype = parse_number (pstate, tokstart, p - tokstart,
1175 got_dot|got_e|got_d,
1177 if (toktype == ERROR)
1179 char *err_copy = (char *) alloca (p - tokstart + 1);
1181 memcpy (err_copy, tokstart, p - tokstart);
1182 err_copy[p - tokstart] = 0;
1183 error (_("Invalid number \"%s\"."), err_copy);
1214 if (!(c == '_' || c == '$' || c ==':'
1215 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1216 /* We must have come across a bad character (e.g. ';'). */
1217 error (_("Invalid character '%c' in expression."), c);
1220 for (c = tokstart[namelen];
1221 (c == '_' || c == '$' || c == ':' || (c >= '0' && c <= '9')
1222 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1223 c = tokstart[++namelen]);
1225 /* The token "if" terminates the expression and is NOT
1226 removed from the input stream. */
1228 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1233 /* Catch specific keywords. */
1235 for (int i = 0; i < ARRAY_SIZE (f77_keywords); i++)
1236 if (strlen (f77_keywords[i].oper) == namelen
1237 && ((!f77_keywords[i].case_sensitive
1238 && strncasecmp (tokstart, f77_keywords[i].oper, namelen) == 0)
1239 || (f77_keywords[i].case_sensitive
1240 && strncmp (tokstart, f77_keywords[i].oper, namelen) == 0)))
1242 yylval.opcode = f77_keywords[i].opcode;
1243 return f77_keywords[i].token;
1246 yylval.sval.ptr = tokstart;
1247 yylval.sval.length = namelen;
1249 if (*tokstart == '$')
1251 write_dollar_variable (pstate, yylval.sval);
1252 return DOLLAR_VARIABLE;
1255 /* Use token-type TYPENAME for symbols that happen to be defined
1256 currently as names of types; NAME for other symbols.
1257 The caller is not constrained to care about the distinction. */
1259 char *tmp = copy_name (yylval.sval);
1260 struct block_symbol result;
1261 struct field_of_this_result is_a_field_of_this;
1262 enum domain_enum_tag lookup_domains[] =
1270 for (int i = 0; i < ARRAY_SIZE (lookup_domains); ++i)
1272 /* Initialize this in case we *don't* use it in this call; that
1273 way we can refer to it unconditionally below. */
1274 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
1276 result = lookup_symbol (tmp, expression_context_block,
1278 parse_language (pstate)->la_language
1280 ? &is_a_field_of_this : NULL);
1281 if (result.symbol && SYMBOL_CLASS (result.symbol) == LOC_TYPEDEF)
1283 yylval.tsym.type = SYMBOL_TYPE (result.symbol);
1292 = language_lookup_primitive_type (parse_language (pstate),
1293 parse_gdbarch (pstate), tmp);
1294 if (yylval.tsym.type != NULL)
1297 /* Input names that aren't symbols but ARE valid hex numbers,
1298 when the input radix permits them, can be names or numbers
1299 depending on the parse. Note we support radixes > 16 here. */
1301 && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1302 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1304 YYSTYPE newlval; /* Its value is ignored. */
1305 hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
1308 yylval.ssym.sym = result;
1309 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1314 /* Any other kind of symbol */
1315 yylval.ssym.sym = result;
1316 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1322 f_parse (struct parser_state *par_state)
1324 /* Setting up the parser state. */
1325 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1326 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
1328 gdb_assert (par_state != NULL);
1335 yyerror (const char *msg)
1338 lexptr = prev_lexptr;
1340 error (_("A %s in expression, near `%s'."), msg, lexptr);