gdb/breakpoint: add flags to 'condition' and 'break' commands to force condition
[deliverable/binutils-gdb.git] / gdb / f-exp.y
CommitLineData
0c9c3474 1
c906108c 2/* YACC parser for Fortran expressions, for GDB.
b811d2c2 3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4fcf66da 4
c906108c
SS
5 Contributed by Motorola. Adapted from the C parser by Farooq Butt
6 (fmbutt@engage.sps.mot.com).
7
5b1ba0e5 8 This file is part of GDB.
c906108c 9
5b1ba0e5
NS
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.
c906108c 14
5b1ba0e5
NS
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.
c906108c 19
5b1ba0e5
NS
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/>. */
c906108c
SS
22
23/* This was blantantly ripped off the C expression parser, please
24 be aware of that as you look at its basic structure -FMB */
25
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.
34
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. */
42
43%{
44
45#include "defs.h"
c906108c
SS
46#include "expression.h"
47#include "value.h"
48#include "parser-defs.h"
49#include "language.h"
50#include "f-lang.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 */
fe898f56 54#include "block.h"
0f6e1ba6 55#include <ctype.h>
325fac50 56#include <algorithm>
dac43e32 57#include "type-stack.h"
c906108c 58
fa9f5be6
TT
59#define parse_type(ps) builtin_type (ps->gdbarch ())
60#define parse_f_type(ps) builtin_f_type (ps->gdbarch ())
3e79cecf 61
b3f11165
PA
62/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
63 etc). */
64#define GDB_YY_REMAP_PREFIX f_
65#include "yy-remap.h"
f461f5cf 66
410a0ff2
SDJ
67/* The state of the parser, used internally when we are parsing the
68 expression. */
69
70static struct parser_state *pstate = NULL;
71
28aaf3fd
TT
72/* Depth of parentheses. */
73static int paren_depth;
74
dac43e32
TT
75/* The current type stack. */
76static struct type_stack *type_stack;
77
a14ed312 78int yyparse (void);
c906108c 79
a14ed312 80static int yylex (void);
c906108c 81
69d340c6 82static void yyerror (const char *);
c906108c 83
a14ed312 84static void growbuf_by_size (int);
c906108c 85
a14ed312 86static int match_string_literal (void);
c906108c 87
4d00f5d8
AB
88static void push_kind_type (LONGEST val, struct type *type);
89
90static struct type *convert_to_kind_type (struct type *basetype, int kind);
91
c906108c
SS
92%}
93
94/* Although the yacc "value" of an expression is not used,
95 since the result is stored in the structure being created,
96 other node types do have values. */
97
98%union
99 {
100 LONGEST lval;
101 struct {
102 LONGEST val;
103 struct type *type;
104 } typed_val;
edd079d9
UW
105 struct {
106 gdb_byte val[16];
107 struct type *type;
108 } typed_val_float;
c906108c
SS
109 struct symbol *sym;
110 struct type *tval;
111 struct stoken sval;
112 struct ttype tsym;
113 struct symtoken ssym;
114 int voidval;
c906108c
SS
115 enum exp_opcode opcode;
116 struct internalvar *ivar;
117
118 struct type **tvec;
119 int *ivec;
120 }
121
122%{
123/* YYSTYPE gets defined by %union */
410a0ff2
SDJ
124static int parse_number (struct parser_state *, const char *, int,
125 int, YYSTYPE *);
c906108c
SS
126%}
127
128%type <voidval> exp type_exp start variable
129%type <tval> type typebase
130%type <tvec> nonempty_typelist
131/* %type <bval> block */
132
133/* Fancy type parsing. */
134%type <voidval> func_mod direct_abs_decl abs_decl
135%type <tval> ptype
136
137%token <typed_val> INT
edd079d9 138%token <typed_val_float> FLOAT
c906108c
SS
139
140/* Both NAME and TYPENAME tokens represent symbols in the input,
141 and both convey their data as strings.
142 But a TYPENAME is a string that happens to be defined as a typedef
143 or builtin type name (such as int or char)
144 and a NAME is any other symbol.
145 Contexts where this distinction is not important can use the
146 nonterminal "name", which matches either NAME or TYPENAME. */
147
148%token <sval> STRING_LITERAL
149%token <lval> BOOLEAN_LITERAL
150%token <ssym> NAME
151%token <tsym> TYPENAME
2a5e440c 152%type <sval> name
c906108c 153%type <ssym> name_not_typename
c906108c
SS
154
155/* A NAME_OR_INT is a symbol which is not known in the symbol table,
156 but which would parse as a valid number in the current input radix.
157 E.g. "c" when input_radix==16. Depending on the parse, it will be
158 turned into a name or into a number. */
159
160%token <ssym> NAME_OR_INT
161
4d00f5d8 162%token SIZEOF KIND
c906108c
SS
163%token ERROR
164
165/* Special type cases, put in to allow the parser to distinguish different
166 legal basetypes. */
167%token INT_KEYWORD INT_S2_KEYWORD LOGICAL_S1_KEYWORD LOGICAL_S2_KEYWORD
ce4b0682 168%token LOGICAL_S8_KEYWORD
c906108c 169%token LOGICAL_KEYWORD REAL_KEYWORD REAL_S8_KEYWORD REAL_S16_KEYWORD
36c8fb93 170%token COMPLEX_KEYWORD
c906108c
SS
171%token COMPLEX_S8_KEYWORD COMPLEX_S16_KEYWORD COMPLEX_S32_KEYWORD
172%token BOOL_AND BOOL_OR BOOL_NOT
36c8fb93 173%token SINGLE DOUBLE PRECISION
c906108c
SS
174%token <lval> CHARACTER
175
cfeadda5 176%token <voidval> DOLLAR_VARIABLE
c906108c
SS
177
178%token <opcode> ASSIGN_MODIFY
b6d03bb2 179%token <opcode> UNOP_INTRINSIC BINOP_INTRINSIC
c906108c
SS
180
181%left ','
182%left ABOVE_COMMA
183%right '=' ASSIGN_MODIFY
184%right '?'
185%left BOOL_OR
186%right BOOL_NOT
187%left BOOL_AND
188%left '|'
189%left '^'
190%left '&'
191%left EQUAL NOTEQUAL
192%left LESSTHAN GREATERTHAN LEQ GEQ
193%left LSH RSH
194%left '@'
195%left '+' '-'
2a5e440c 196%left '*' '/'
bd49c137 197%right STARSTAR
2a5e440c 198%right '%'
c906108c
SS
199%right UNARY
200%right '('
201
202\f
203%%
204
205start : exp
206 | type_exp
207 ;
208
209type_exp: type
410a0ff2
SDJ
210 { write_exp_elt_opcode (pstate, OP_TYPE);
211 write_exp_elt_type (pstate, $1);
212 write_exp_elt_opcode (pstate, OP_TYPE); }
c906108c
SS
213 ;
214
215exp : '(' exp ')'
216 { }
217 ;
218
219/* Expressions, not including the comma operator. */
220exp : '*' exp %prec UNARY
410a0ff2 221 { write_exp_elt_opcode (pstate, UNOP_IND); }
ef944135 222 ;
c906108c
SS
223
224exp : '&' exp %prec UNARY
410a0ff2 225 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
ef944135 226 ;
c906108c
SS
227
228exp : '-' exp %prec UNARY
410a0ff2 229 { write_exp_elt_opcode (pstate, UNOP_NEG); }
c906108c
SS
230 ;
231
232exp : BOOL_NOT exp %prec UNARY
410a0ff2 233 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
c906108c
SS
234 ;
235
236exp : '~' exp %prec UNARY
410a0ff2 237 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
c906108c
SS
238 ;
239
240exp : SIZEOF exp %prec UNARY
410a0ff2 241 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
c906108c
SS
242 ;
243
4d00f5d8 244exp : KIND '(' exp ')' %prec UNARY
83228e93 245 { write_exp_elt_opcode (pstate, UNOP_FORTRAN_KIND); }
4d00f5d8
AB
246 ;
247
c906108c
SS
248/* No more explicit array operators, we treat everything in F77 as
249 a function call. The disambiguation as to whether we are
250 doing a subscript operation or a function call is done
251 later in eval.c. */
252
253exp : exp '('
43476f0b 254 { pstate->start_arglist (); }
c906108c 255 arglist ')'
410a0ff2
SDJ
256 { write_exp_elt_opcode (pstate,
257 OP_F77_UNDETERMINED_ARGLIST);
258 write_exp_elt_longcst (pstate,
43476f0b 259 pstate->end_arglist ());
410a0ff2
SDJ
260 write_exp_elt_opcode (pstate,
261 OP_F77_UNDETERMINED_ARGLIST); }
c906108c
SS
262 ;
263
0841c79a
AB
264exp : UNOP_INTRINSIC '(' exp ')'
265 { write_exp_elt_opcode (pstate, $1); }
266 ;
267
b6d03bb2
AB
268exp : BINOP_INTRINSIC '(' exp ',' exp ')'
269 { write_exp_elt_opcode (pstate, $1); }
270 ;
271
c906108c
SS
272arglist :
273 ;
274
275arglist : exp
43476f0b 276 { pstate->arglist_len = 1; }
c906108c
SS
277 ;
278
0b4e1325 279arglist : subrange
43476f0b 280 { pstate->arglist_len = 1; }
ef944135 281 ;
c906108c
SS
282
283arglist : arglist ',' exp %prec ABOVE_COMMA
43476f0b 284 { pstate->arglist_len++; }
c906108c
SS
285 ;
286
6b4c676c
AB
287arglist : arglist ',' subrange %prec ABOVE_COMMA
288 { pstate->arglist_len++; }
289 ;
290
0b4e1325
WZ
291/* There are four sorts of subrange types in F90. */
292
293subrange: exp ':' exp %prec ABOVE_COMMA
2f1b18db
AB
294 { write_exp_elt_opcode (pstate, OP_RANGE);
295 write_exp_elt_longcst (pstate, RANGE_STANDARD);
01739a3b 296 write_exp_elt_opcode (pstate, OP_RANGE); }
0b4e1325
WZ
297 ;
298
299subrange: exp ':' %prec ABOVE_COMMA
01739a3b 300 { write_exp_elt_opcode (pstate, OP_RANGE);
2f1b18db
AB
301 write_exp_elt_longcst (pstate,
302 RANGE_HIGH_BOUND_DEFAULT);
01739a3b 303 write_exp_elt_opcode (pstate, OP_RANGE); }
c906108c
SS
304 ;
305
0b4e1325 306subrange: ':' exp %prec ABOVE_COMMA
01739a3b 307 { write_exp_elt_opcode (pstate, OP_RANGE);
2f1b18db
AB
308 write_exp_elt_longcst (pstate,
309 RANGE_LOW_BOUND_DEFAULT);
01739a3b 310 write_exp_elt_opcode (pstate, OP_RANGE); }
0b4e1325
WZ
311 ;
312
313subrange: ':' %prec ABOVE_COMMA
01739a3b 314 { write_exp_elt_opcode (pstate, OP_RANGE);
2f1b18db
AB
315 write_exp_elt_longcst (pstate,
316 (RANGE_LOW_BOUND_DEFAULT
317 | RANGE_HIGH_BOUND_DEFAULT));
01739a3b 318 write_exp_elt_opcode (pstate, OP_RANGE); }
0b4e1325 319 ;
c906108c 320
6b4c676c
AB
321/* And each of the four subrange types can also have a stride. */
322subrange: exp ':' exp ':' exp %prec ABOVE_COMMA
323 { write_exp_elt_opcode (pstate, OP_RANGE);
324 write_exp_elt_longcst (pstate, RANGE_HAS_STRIDE);
325 write_exp_elt_opcode (pstate, OP_RANGE); }
326 ;
327
328subrange: exp ':' ':' exp %prec ABOVE_COMMA
329 { write_exp_elt_opcode (pstate, OP_RANGE);
330 write_exp_elt_longcst (pstate,
331 (RANGE_HIGH_BOUND_DEFAULT
332 | RANGE_HAS_STRIDE));
333 write_exp_elt_opcode (pstate, OP_RANGE); }
334 ;
335
336subrange: ':' exp ':' exp %prec ABOVE_COMMA
337 { write_exp_elt_opcode (pstate, OP_RANGE);
338 write_exp_elt_longcst (pstate,
339 (RANGE_LOW_BOUND_DEFAULT
340 | RANGE_HAS_STRIDE));
341 write_exp_elt_opcode (pstate, OP_RANGE); }
342 ;
343
344subrange: ':' ':' exp %prec ABOVE_COMMA
345 { write_exp_elt_opcode (pstate, OP_RANGE);
346 write_exp_elt_longcst (pstate,
347 (RANGE_LOW_BOUND_DEFAULT
348 | RANGE_HIGH_BOUND_DEFAULT
349 | RANGE_HAS_STRIDE));
350 write_exp_elt_opcode (pstate, OP_RANGE); }
351 ;
352
c906108c
SS
353complexnum: exp ',' exp
354 { }
355 ;
356
357exp : '(' complexnum ')'
410a0ff2
SDJ
358 { write_exp_elt_opcode (pstate, OP_COMPLEX);
359 write_exp_elt_type (pstate,
360 parse_f_type (pstate)
361 ->builtin_complex_s16);
362 write_exp_elt_opcode (pstate, OP_COMPLEX); }
c906108c
SS
363 ;
364
365exp : '(' type ')' exp %prec UNARY
410a0ff2
SDJ
366 { write_exp_elt_opcode (pstate, UNOP_CAST);
367 write_exp_elt_type (pstate, $2);
368 write_exp_elt_opcode (pstate, UNOP_CAST); }
c906108c
SS
369 ;
370
2a5e440c 371exp : exp '%' name
410a0ff2
SDJ
372 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
373 write_exp_string (pstate, $3);
374 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
2a5e440c
WZ
375 ;
376
c906108c
SS
377/* Binary operators in order of decreasing precedence. */
378
379exp : exp '@' exp
410a0ff2 380 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
c906108c
SS
381 ;
382
bd49c137 383exp : exp STARSTAR exp
410a0ff2 384 { write_exp_elt_opcode (pstate, BINOP_EXP); }
bd49c137
WZ
385 ;
386
c906108c 387exp : exp '*' exp
410a0ff2 388 { write_exp_elt_opcode (pstate, BINOP_MUL); }
c906108c
SS
389 ;
390
391exp : exp '/' exp
410a0ff2 392 { write_exp_elt_opcode (pstate, BINOP_DIV); }
c906108c
SS
393 ;
394
c906108c 395exp : exp '+' exp
410a0ff2 396 { write_exp_elt_opcode (pstate, BINOP_ADD); }
c906108c
SS
397 ;
398
399exp : exp '-' exp
410a0ff2 400 { write_exp_elt_opcode (pstate, BINOP_SUB); }
c906108c
SS
401 ;
402
403exp : exp LSH exp
410a0ff2 404 { write_exp_elt_opcode (pstate, BINOP_LSH); }
c906108c
SS
405 ;
406
407exp : exp RSH exp
410a0ff2 408 { write_exp_elt_opcode (pstate, BINOP_RSH); }
c906108c
SS
409 ;
410
411exp : exp EQUAL exp
410a0ff2 412 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
c906108c
SS
413 ;
414
415exp : exp NOTEQUAL exp
410a0ff2 416 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
c906108c
SS
417 ;
418
419exp : exp LEQ exp
410a0ff2 420 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
c906108c
SS
421 ;
422
423exp : exp GEQ exp
410a0ff2 424 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
c906108c
SS
425 ;
426
427exp : exp LESSTHAN exp
410a0ff2 428 { write_exp_elt_opcode (pstate, BINOP_LESS); }
c906108c
SS
429 ;
430
431exp : exp GREATERTHAN exp
410a0ff2 432 { write_exp_elt_opcode (pstate, BINOP_GTR); }
c906108c
SS
433 ;
434
435exp : exp '&' exp
410a0ff2 436 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
c906108c
SS
437 ;
438
439exp : exp '^' exp
410a0ff2 440 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
c906108c
SS
441 ;
442
443exp : exp '|' exp
410a0ff2 444 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
c906108c
SS
445 ;
446
447exp : exp BOOL_AND exp
410a0ff2 448 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
c906108c
SS
449 ;
450
451
452exp : exp BOOL_OR exp
410a0ff2 453 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
c906108c
SS
454 ;
455
456exp : exp '=' exp
410a0ff2 457 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
c906108c
SS
458 ;
459
460exp : exp ASSIGN_MODIFY exp
410a0ff2
SDJ
461 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
462 write_exp_elt_opcode (pstate, $2);
463 write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
c906108c
SS
464 ;
465
466exp : INT
410a0ff2
SDJ
467 { write_exp_elt_opcode (pstate, OP_LONG);
468 write_exp_elt_type (pstate, $1.type);
469 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
470 write_exp_elt_opcode (pstate, OP_LONG); }
c906108c
SS
471 ;
472
473exp : NAME_OR_INT
474 { YYSTYPE val;
410a0ff2
SDJ
475 parse_number (pstate, $1.stoken.ptr,
476 $1.stoken.length, 0, &val);
477 write_exp_elt_opcode (pstate, OP_LONG);
478 write_exp_elt_type (pstate, val.typed_val.type);
479 write_exp_elt_longcst (pstate,
480 (LONGEST)val.typed_val.val);
481 write_exp_elt_opcode (pstate, OP_LONG); }
c906108c
SS
482 ;
483
484exp : FLOAT
edd079d9
UW
485 { write_exp_elt_opcode (pstate, OP_FLOAT);
486 write_exp_elt_type (pstate, $1.type);
487 write_exp_elt_floatcst (pstate, $1.val);
488 write_exp_elt_opcode (pstate, OP_FLOAT); }
c906108c
SS
489 ;
490
491exp : variable
492 ;
493
cfeadda5 494exp : DOLLAR_VARIABLE
c906108c
SS
495 ;
496
497exp : SIZEOF '(' type ')' %prec UNARY
410a0ff2
SDJ
498 { write_exp_elt_opcode (pstate, OP_LONG);
499 write_exp_elt_type (pstate,
500 parse_f_type (pstate)
501 ->builtin_integer);
f168693b 502 $3 = check_typedef ($3);
410a0ff2
SDJ
503 write_exp_elt_longcst (pstate,
504 (LONGEST) TYPE_LENGTH ($3));
505 write_exp_elt_opcode (pstate, OP_LONG); }
c906108c
SS
506 ;
507
508exp : BOOLEAN_LITERAL
410a0ff2
SDJ
509 { write_exp_elt_opcode (pstate, OP_BOOL);
510 write_exp_elt_longcst (pstate, (LONGEST) $1);
511 write_exp_elt_opcode (pstate, OP_BOOL);
c906108c
SS
512 }
513 ;
514
515exp : STRING_LITERAL
516 {
410a0ff2
SDJ
517 write_exp_elt_opcode (pstate, OP_STRING);
518 write_exp_string (pstate, $1);
519 write_exp_elt_opcode (pstate, OP_STRING);
c906108c
SS
520 }
521 ;
522
523variable: name_not_typename
d12307c1 524 { struct block_symbol sym = $1.sym;
c906108c 525
d12307c1 526 if (sym.symbol)
c906108c 527 {
d12307c1 528 if (symbol_read_needs_frame (sym.symbol))
699bd4cf 529 pstate->block_tracker->update (sym);
410a0ff2 530 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
63e43d3a 531 write_exp_elt_block (pstate, sym.block);
d12307c1 532 write_exp_elt_sym (pstate, sym.symbol);
410a0ff2 533 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
c906108c
SS
534 break;
535 }
536 else
537 {
7c7b6655 538 struct bound_minimal_symbol msymbol;
61f4b350 539 std::string arg = copy_name ($1.stoken);
c906108c
SS
540
541 msymbol =
61f4b350 542 lookup_bound_minimal_symbol (arg.c_str ());
7c7b6655 543 if (msymbol.minsym != NULL)
410a0ff2 544 write_exp_msymbol (pstate, msymbol);
c906108c 545 else if (!have_full_symbols () && !have_partial_symbols ())
001083c6 546 error (_("No symbol table is loaded. Use the \"file\" command."));
c906108c 547 else
001083c6 548 error (_("No symbol \"%s\" in current context."),
61f4b350 549 arg.c_str ());
c906108c
SS
550 }
551 }
552 ;
553
554
555type : ptype
556 ;
557
558ptype : typebase
559 | typebase abs_decl
560 {
561 /* This is where the interesting stuff happens. */
562 int done = 0;
563 int array_size;
564 struct type *follow_type = $1;
565 struct type *range_type;
566
567 while (!done)
dac43e32 568 switch (type_stack->pop ())
c906108c
SS
569 {
570 case tp_end:
571 done = 1;
572 break;
573 case tp_pointer:
574 follow_type = lookup_pointer_type (follow_type);
575 break;
576 case tp_reference:
3b224330 577 follow_type = lookup_lvalue_reference_type (follow_type);
c906108c
SS
578 break;
579 case tp_array:
dac43e32 580 array_size = type_stack->pop_int ();
c906108c
SS
581 if (array_size != -1)
582 {
583 range_type =
0c9c3474
SA
584 create_static_range_type ((struct type *) NULL,
585 parse_f_type (pstate)
586 ->builtin_integer,
587 0, array_size - 1);
c906108c
SS
588 follow_type =
589 create_array_type ((struct type *) NULL,
590 follow_type, range_type);
591 }
592 else
593 follow_type = lookup_pointer_type (follow_type);
594 break;
595 case tp_function:
596 follow_type = lookup_function_type (follow_type);
597 break;
4d00f5d8
AB
598 case tp_kind:
599 {
dac43e32 600 int kind_val = type_stack->pop_int ();
4d00f5d8
AB
601 follow_type
602 = convert_to_kind_type (follow_type, kind_val);
603 }
604 break;
c906108c
SS
605 }
606 $$ = follow_type;
607 }
608 ;
609
610abs_decl: '*'
dac43e32 611 { type_stack->push (tp_pointer); $$ = 0; }
c906108c 612 | '*' abs_decl
dac43e32 613 { type_stack->push (tp_pointer); $$ = $2; }
c906108c 614 | '&'
dac43e32 615 { type_stack->push (tp_reference); $$ = 0; }
c906108c 616 | '&' abs_decl
dac43e32 617 { type_stack->push (tp_reference); $$ = $2; }
c906108c
SS
618 | direct_abs_decl
619 ;
620
621direct_abs_decl: '(' abs_decl ')'
622 { $$ = $2; }
4d00f5d8
AB
623 | '(' KIND '=' INT ')'
624 { push_kind_type ($4.val, $4.type); }
efbecbc1
AB
625 | '*' INT
626 { push_kind_type ($2.val, $2.type); }
c906108c 627 | direct_abs_decl func_mod
dac43e32 628 { type_stack->push (tp_function); }
c906108c 629 | func_mod
dac43e32 630 { type_stack->push (tp_function); }
c906108c
SS
631 ;
632
633func_mod: '(' ')'
634 { $$ = 0; }
635 | '(' nonempty_typelist ')'
8dbb1c65 636 { free ($2); $$ = 0; }
c906108c
SS
637 ;
638
639typebase /* Implements (approximately): (type-qualifier)* type-specifier */
640 : TYPENAME
641 { $$ = $1.type; }
642 | INT_KEYWORD
410a0ff2 643 { $$ = parse_f_type (pstate)->builtin_integer; }
c906108c 644 | INT_S2_KEYWORD
410a0ff2 645 { $$ = parse_f_type (pstate)->builtin_integer_s2; }
c906108c 646 | CHARACTER
410a0ff2 647 { $$ = parse_f_type (pstate)->builtin_character; }
ce4b0682 648 | LOGICAL_S8_KEYWORD
410a0ff2 649 { $$ = parse_f_type (pstate)->builtin_logical_s8; }
c906108c 650 | LOGICAL_KEYWORD
410a0ff2 651 { $$ = parse_f_type (pstate)->builtin_logical; }
c906108c 652 | LOGICAL_S2_KEYWORD
410a0ff2 653 { $$ = parse_f_type (pstate)->builtin_logical_s2; }
c906108c 654 | LOGICAL_S1_KEYWORD
410a0ff2 655 { $$ = parse_f_type (pstate)->builtin_logical_s1; }
c906108c 656 | REAL_KEYWORD
410a0ff2 657 { $$ = parse_f_type (pstate)->builtin_real; }
c906108c 658 | REAL_S8_KEYWORD
410a0ff2 659 { $$ = parse_f_type (pstate)->builtin_real_s8; }
c906108c 660 | REAL_S16_KEYWORD
410a0ff2 661 { $$ = parse_f_type (pstate)->builtin_real_s16; }
36c8fb93
AB
662 | COMPLEX_KEYWORD
663 { $$ = parse_f_type (pstate)->builtin_complex_s8; }
c906108c 664 | COMPLEX_S8_KEYWORD
410a0ff2 665 { $$ = parse_f_type (pstate)->builtin_complex_s8; }
c906108c 666 | COMPLEX_S16_KEYWORD
410a0ff2 667 { $$ = parse_f_type (pstate)->builtin_complex_s16; }
c906108c 668 | COMPLEX_S32_KEYWORD
410a0ff2 669 { $$ = parse_f_type (pstate)->builtin_complex_s32; }
36c8fb93
AB
670 | SINGLE PRECISION
671 { $$ = parse_f_type (pstate)->builtin_real;}
672 | DOUBLE PRECISION
673 { $$ = parse_f_type (pstate)->builtin_real_s8;}
674 | SINGLE COMPLEX_KEYWORD
675 { $$ = parse_f_type (pstate)->builtin_complex_s8;}
676 | DOUBLE COMPLEX_KEYWORD
677 { $$ = parse_f_type (pstate)->builtin_complex_s16;}
c906108c
SS
678 ;
679
c906108c
SS
680nonempty_typelist
681 : type
682 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
683 $<ivec>$[0] = 1; /* Number of types in vector */
684 $$[1] = $1;
685 }
686 | nonempty_typelist ',' type
687 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
688 $$ = (struct type **) realloc ((char *) $1, len);
689 $$[$<ivec>$[0]] = $3;
690 }
691 ;
692
2a5e440c
WZ
693name : NAME
694 { $$ = $1.stoken; }
695 ;
696
c906108c
SS
697name_not_typename : NAME
698/* These would be useful if name_not_typename was useful, but it is just
699 a fake for "variable", so these cause reduce/reduce conflicts because
700 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
701 =exp) or just an exp. If name_not_typename was ever used in an lvalue
702 context where only a name could occur, this might be useful.
703 | NAME_OR_INT
704 */
705 ;
706
707%%
708
709/* Take care of parsing a number (anything that starts with a digit).
710 Set yylval and return the token type; update lexptr.
711 LEN is the number of characters in it. */
712
713/*** Needs some error checking for the float case ***/
714
715static int
410a0ff2
SDJ
716parse_number (struct parser_state *par_state,
717 const char *p, int len, int parsed_float, YYSTYPE *putithere)
c906108c 718{
710122da
DC
719 LONGEST n = 0;
720 LONGEST prevn = 0;
721 int c;
722 int base = input_radix;
c906108c
SS
723 int unsigned_p = 0;
724 int long_p = 0;
725 ULONGEST high_bit;
726 struct type *signed_type;
727 struct type *unsigned_type;
728
729 if (parsed_float)
730 {
731 /* It's a float since it contains a point or an exponent. */
edd079d9
UW
732 /* [dD] is not understood as an exponent by parse_float,
733 change it to 'e'. */
c906108c
SS
734 char *tmp, *tmp2;
735
4fcf66da 736 tmp = xstrdup (p);
c906108c
SS
737 for (tmp2 = tmp; *tmp2; ++tmp2)
738 if (*tmp2 == 'd' || *tmp2 == 'D')
739 *tmp2 = 'e';
edd079d9
UW
740
741 /* FIXME: Should this use different types? */
742 putithere->typed_val_float.type = parse_f_type (pstate)->builtin_real_s8;
743 bool parsed = parse_float (tmp, len,
744 putithere->typed_val_float.type,
745 putithere->typed_val_float.val);
c906108c 746 free (tmp);
edd079d9 747 return parsed? FLOAT : ERROR;
c906108c
SS
748 }
749
750 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
751 if (p[0] == '0')
752 switch (p[1])
753 {
754 case 'x':
755 case 'X':
756 if (len >= 3)
757 {
758 p += 2;
759 base = 16;
760 len -= 2;
761 }
762 break;
763
764 case 't':
765 case 'T':
766 case 'd':
767 case 'D':
768 if (len >= 3)
769 {
770 p += 2;
771 base = 10;
772 len -= 2;
773 }
774 break;
775
776 default:
777 base = 8;
778 break;
779 }
780
781 while (len-- > 0)
782 {
783 c = *p++;
0f6e1ba6
AC
784 if (isupper (c))
785 c = tolower (c);
786 if (len == 0 && c == 'l')
787 long_p = 1;
788 else if (len == 0 && c == 'u')
789 unsigned_p = 1;
c906108c
SS
790 else
791 {
0f6e1ba6
AC
792 int i;
793 if (c >= '0' && c <= '9')
794 i = c - '0';
795 else if (c >= 'a' && c <= 'f')
796 i = c - 'a' + 10;
c906108c
SS
797 else
798 return ERROR; /* Char not a digit */
0f6e1ba6
AC
799 if (i >= base)
800 return ERROR; /* Invalid digit in this base */
801 n *= base;
802 n += i;
c906108c 803 }
c906108c
SS
804 /* Portably test for overflow (only works for nonzero values, so make
805 a second check for zero). */
806 if ((prevn >= n) && n != 0)
807 unsigned_p=1; /* Try something unsigned */
808 /* If range checking enabled, portably test for unsigned overflow. */
809 if (RANGE_CHECK && n != 0)
810 {
811 if ((unsigned_p && (unsigned)prevn >= (unsigned)n))
001083c6 812 range_error (_("Overflow on numeric constant."));
c906108c
SS
813 }
814 prevn = n;
815 }
816
817 /* If the number is too big to be an int, or it's got an l suffix
818 then it's a long. Work out if this has to be a long by
7a9dd1b2 819 shifting right and seeing if anything remains, and the
c906108c
SS
820 target int size is different to the target long size.
821
822 In the expression below, we could have tested
3e79cecf 823 (n >> gdbarch_int_bit (parse_gdbarch))
c906108c
SS
824 to see if it was zero,
825 but too many compilers warn about that, when ints and longs
826 are the same size. So we shift it twice, with fewer bits
827 each time, for the same result. */
828
fa9f5be6
TT
829 if ((gdbarch_int_bit (par_state->gdbarch ())
830 != gdbarch_long_bit (par_state->gdbarch ())
9a76efb6 831 && ((n >> 2)
fa9f5be6 832 >> (gdbarch_int_bit (par_state->gdbarch ())-2))) /* Avoid
410a0ff2 833 shift warning */
c906108c
SS
834 || long_p)
835 {
410a0ff2 836 high_bit = ((ULONGEST)1)
fa9f5be6 837 << (gdbarch_long_bit (par_state->gdbarch ())-1);
410a0ff2
SDJ
838 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
839 signed_type = parse_type (par_state)->builtin_long;
c906108c
SS
840 }
841 else
842 {
410a0ff2 843 high_bit =
fa9f5be6 844 ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
410a0ff2
SDJ
845 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
846 signed_type = parse_type (par_state)->builtin_int;
c906108c
SS
847 }
848
849 putithere->typed_val.val = n;
850
851 /* If the high bit of the worked out type is set then this number
0963b4bd 852 has to be unsigned. */
c906108c
SS
853
854 if (unsigned_p || (n & high_bit))
855 putithere->typed_val.type = unsigned_type;
856 else
857 putithere->typed_val.type = signed_type;
858
859 return INT;
860}
861
4d00f5d8
AB
862/* Called to setup the type stack when we encounter a '(kind=N)' type
863 modifier, performs some bounds checking on 'N' and then pushes this to
864 the type stack followed by the 'tp_kind' marker. */
865static void
866push_kind_type (LONGEST val, struct type *type)
867{
868 int ival;
869
c6d940a9 870 if (type->is_unsigned ())
4d00f5d8
AB
871 {
872 ULONGEST uval = static_cast <ULONGEST> (val);
873 if (uval > INT_MAX)
874 error (_("kind value out of range"));
875 ival = static_cast <int> (uval);
876 }
877 else
878 {
879 if (val > INT_MAX || val < 0)
880 error (_("kind value out of range"));
881 ival = static_cast <int> (val);
882 }
883
dac43e32
TT
884 type_stack->push (ival);
885 type_stack->push (tp_kind);
4d00f5d8
AB
886}
887
888/* Called when a type has a '(kind=N)' modifier after it, for example
889 'character(kind=1)'. The BASETYPE is the type described by 'character'
890 in our example, and KIND is the integer '1'. This function returns a
891 new type that represents the basetype of a specific kind. */
892static struct type *
893convert_to_kind_type (struct type *basetype, int kind)
894{
895 if (basetype == parse_f_type (pstate)->builtin_character)
896 {
897 /* Character of kind 1 is a special case, this is the same as the
898 base character type. */
899 if (kind == 1)
900 return parse_f_type (pstate)->builtin_character;
901 }
3be47f7a
AB
902 else if (basetype == parse_f_type (pstate)->builtin_complex_s8)
903 {
904 if (kind == 4)
905 return parse_f_type (pstate)->builtin_complex_s8;
906 else if (kind == 8)
907 return parse_f_type (pstate)->builtin_complex_s16;
908 else if (kind == 16)
909 return parse_f_type (pstate)->builtin_complex_s32;
910 }
911 else if (basetype == parse_f_type (pstate)->builtin_real)
912 {
913 if (kind == 4)
914 return parse_f_type (pstate)->builtin_real;
915 else if (kind == 8)
916 return parse_f_type (pstate)->builtin_real_s8;
917 else if (kind == 16)
918 return parse_f_type (pstate)->builtin_real_s16;
919 }
920 else if (basetype == parse_f_type (pstate)->builtin_logical)
921 {
922 if (kind == 1)
923 return parse_f_type (pstate)->builtin_logical_s1;
924 else if (kind == 2)
925 return parse_f_type (pstate)->builtin_logical_s2;
926 else if (kind == 4)
927 return parse_f_type (pstate)->builtin_logical;
928 else if (kind == 8)
929 return parse_f_type (pstate)->builtin_logical_s8;
930 }
931 else if (basetype == parse_f_type (pstate)->builtin_integer)
932 {
933 if (kind == 2)
934 return parse_f_type (pstate)->builtin_integer_s2;
935 else if (kind == 4)
936 return parse_f_type (pstate)->builtin_integer;
067630bd
AB
937 else if (kind == 8)
938 return parse_f_type (pstate)->builtin_integer_s8;
3be47f7a 939 }
4d00f5d8
AB
940
941 error (_("unsupported kind %d for type %s"),
942 kind, TYPE_SAFE_NAME (basetype));
943
944 /* Should never get here. */
945 return nullptr;
946}
947
c906108c
SS
948struct token
949{
c8f91604 950 /* The string to match against. */
a121b7c1 951 const char *oper;
c8f91604
AB
952
953 /* The lexer token to return. */
c906108c 954 int token;
c8f91604
AB
955
956 /* The expression opcode to embed within the token. */
c906108c 957 enum exp_opcode opcode;
c8f91604
AB
958
959 /* When this is true the string in OPER is matched exactly including
960 case, when this is false OPER is matched case insensitively. */
961 bool case_sensitive;
c906108c
SS
962};
963
964static const struct token dot_ops[] =
965{
c8f91604
AB
966 { ".and.", BOOL_AND, BINOP_END, false },
967 { ".or.", BOOL_OR, BINOP_END, false },
968 { ".not.", BOOL_NOT, BINOP_END, false },
969 { ".eq.", EQUAL, BINOP_END, false },
970 { ".eqv.", EQUAL, BINOP_END, false },
971 { ".neqv.", NOTEQUAL, BINOP_END, false },
972 { ".ne.", NOTEQUAL, BINOP_END, false },
973 { ".le.", LEQ, BINOP_END, false },
974 { ".ge.", GEQ, BINOP_END, false },
975 { ".gt.", GREATERTHAN, BINOP_END, false },
976 { ".lt.", LESSTHAN, BINOP_END, false },
c906108c
SS
977};
978
dd9f2c76
AB
979/* Holds the Fortran representation of a boolean, and the integer value we
980 substitute in when one of the matching strings is parsed. */
981struct f77_boolean_val
c906108c 982{
dd9f2c76 983 /* The string representing a Fortran boolean. */
a121b7c1 984 const char *name;
dd9f2c76
AB
985
986 /* The integer value to replace it with. */
c906108c 987 int value;
dd9f2c76 988};
c906108c 989
dd9f2c76
AB
990/* The set of Fortran booleans. These are matched case insensitively. */
991static const struct f77_boolean_val boolean_values[] =
c906108c
SS
992{
993 { ".true.", 1 },
dd9f2c76 994 { ".false.", 0 }
c906108c
SS
995};
996
c8f91604 997static const struct token f77_keywords[] =
c906108c 998{
c8f91604
AB
999 /* Historically these have always been lowercase only in GDB. */
1000 { "complex_16", COMPLEX_S16_KEYWORD, BINOP_END, true },
1001 { "complex_32", COMPLEX_S32_KEYWORD, BINOP_END, true },
1002 { "character", CHARACTER, BINOP_END, true },
1003 { "integer_2", INT_S2_KEYWORD, BINOP_END, true },
1004 { "logical_1", LOGICAL_S1_KEYWORD, BINOP_END, true },
1005 { "logical_2", LOGICAL_S2_KEYWORD, BINOP_END, true },
1006 { "logical_8", LOGICAL_S8_KEYWORD, BINOP_END, true },
1007 { "complex_8", COMPLEX_S8_KEYWORD, BINOP_END, true },
1008 { "integer", INT_KEYWORD, BINOP_END, true },
1009 { "logical", LOGICAL_KEYWORD, BINOP_END, true },
1010 { "real_16", REAL_S16_KEYWORD, BINOP_END, true },
36c8fb93 1011 { "complex", COMPLEX_KEYWORD, BINOP_END, true },
c8f91604
AB
1012 { "sizeof", SIZEOF, BINOP_END, true },
1013 { "real_8", REAL_S8_KEYWORD, BINOP_END, true },
1014 { "real", REAL_KEYWORD, BINOP_END, true },
36c8fb93
AB
1015 { "single", SINGLE, BINOP_END, true },
1016 { "double", DOUBLE, BINOP_END, true },
1017 { "precision", PRECISION, BINOP_END, true },
4d00f5d8
AB
1018 /* The following correspond to actual functions in Fortran and are case
1019 insensitive. */
0841c79a 1020 { "kind", KIND, BINOP_END, false },
b6d03bb2
AB
1021 { "abs", UNOP_INTRINSIC, UNOP_ABS, false },
1022 { "mod", BINOP_INTRINSIC, BINOP_MOD, false },
1023 { "floor", UNOP_INTRINSIC, UNOP_FORTRAN_FLOOR, false },
1024 { "ceiling", UNOP_INTRINSIC, UNOP_FORTRAN_CEILING, false },
1025 { "modulo", BINOP_INTRINSIC, BINOP_FORTRAN_MODULO, false },
1026 { "cmplx", BINOP_INTRINSIC, BINOP_FORTRAN_CMPLX, false },
c8f91604 1027};
c906108c
SS
1028
1029/* Implementation of a dynamically expandable buffer for processing input
1030 characters acquired through lexptr and building a value to return in
0963b4bd 1031 yylval. Ripped off from ch-exp.y */
c906108c
SS
1032
1033static char *tempbuf; /* Current buffer contents */
1034static int tempbufsize; /* Size of allocated buffer */
1035static int tempbufindex; /* Current index into buffer */
1036
1037#define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1038
1039#define CHECKBUF(size) \
1040 do { \
1041 if (tempbufindex + (size) >= tempbufsize) \
1042 { \
1043 growbuf_by_size (size); \
1044 } \
1045 } while (0);
1046
1047
0963b4bd
MS
1048/* Grow the static temp buffer if necessary, including allocating the
1049 first one on demand. */
c906108c
SS
1050
1051static void
d04550a6 1052growbuf_by_size (int count)
c906108c
SS
1053{
1054 int growby;
1055
325fac50 1056 growby = std::max (count, GROWBY_MIN_SIZE);
c906108c
SS
1057 tempbufsize += growby;
1058 if (tempbuf == NULL)
1059 tempbuf = (char *) malloc (tempbufsize);
1060 else
1061 tempbuf = (char *) realloc (tempbuf, tempbufsize);
1062}
1063
1064/* Blatantly ripped off from ch-exp.y. This routine recognizes F77
0963b4bd 1065 string-literals.
c906108c
SS
1066
1067 Recognize a string literal. A string literal is a nonzero sequence
1068 of characters enclosed in matching single quotes, except that
1069 a single character inside single quotes is a character literal, which
1070 we reject as a string literal. To embed the terminator character inside
1071 a string, it is simply doubled (I.E. 'this''is''one''string') */
1072
1073static int
eeae04df 1074match_string_literal (void)
c906108c 1075{
5776fca3 1076 const char *tokptr = pstate->lexptr;
c906108c
SS
1077
1078 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1079 {
1080 CHECKBUF (1);
5776fca3 1081 if (*tokptr == *pstate->lexptr)
c906108c 1082 {
5776fca3 1083 if (*(tokptr + 1) == *pstate->lexptr)
c906108c
SS
1084 tokptr++;
1085 else
1086 break;
1087 }
1088 tempbuf[tempbufindex++] = *tokptr;
1089 }
1090 if (*tokptr == '\0' /* no terminator */
1091 || tempbufindex == 0) /* no string */
1092 return 0;
1093 else
1094 {
1095 tempbuf[tempbufindex] = '\0';
1096 yylval.sval.ptr = tempbuf;
1097 yylval.sval.length = tempbufindex;
5776fca3 1098 pstate->lexptr = ++tokptr;
c906108c
SS
1099 return STRING_LITERAL;
1100 }
1101}
1102
1103/* Read one token, getting characters through lexptr. */
1104
1105static int
eeae04df 1106yylex (void)
c906108c
SS
1107{
1108 int c;
1109 int namelen;
b926417a 1110 unsigned int token;
d7561cbb 1111 const char *tokstart;
c906108c
SS
1112
1113 retry:
065432a8 1114
5776fca3 1115 pstate->prev_lexptr = pstate->lexptr;
065432a8 1116
5776fca3 1117 tokstart = pstate->lexptr;
dd9f2c76
AB
1118
1119 /* First of all, let us make sure we are not dealing with the
c906108c 1120 special tokens .true. and .false. which evaluate to 1 and 0. */
dd9f2c76 1121
5776fca3 1122 if (*pstate->lexptr == '.')
dd9f2c76
AB
1123 {
1124 for (int i = 0; i < ARRAY_SIZE (boolean_values); i++)
c906108c 1125 {
dd9f2c76
AB
1126 if (strncasecmp (tokstart, boolean_values[i].name,
1127 strlen (boolean_values[i].name)) == 0)
c906108c 1128 {
5776fca3 1129 pstate->lexptr += strlen (boolean_values[i].name);
dd9f2c76 1130 yylval.lval = boolean_values[i].value;
c906108c
SS
1131 return BOOLEAN_LITERAL;
1132 }
1133 }
1134 }
c8f91604 1135
bd49c137 1136 /* See if it is a special .foo. operator. */
c8f91604
AB
1137 for (int i = 0; i < ARRAY_SIZE (dot_ops); i++)
1138 if (strncasecmp (tokstart, dot_ops[i].oper,
1139 strlen (dot_ops[i].oper)) == 0)
c906108c 1140 {
c8f91604 1141 gdb_assert (!dot_ops[i].case_sensitive);
5776fca3 1142 pstate->lexptr += strlen (dot_ops[i].oper);
c906108c
SS
1143 yylval.opcode = dot_ops[i].opcode;
1144 return dot_ops[i].token;
1145 }
c8f91604 1146
bd49c137
WZ
1147 /* See if it is an exponentiation operator. */
1148
1149 if (strncmp (tokstart, "**", 2) == 0)
1150 {
5776fca3 1151 pstate->lexptr += 2;
bd49c137
WZ
1152 yylval.opcode = BINOP_EXP;
1153 return STARSTAR;
1154 }
1155
c906108c
SS
1156 switch (c = *tokstart)
1157 {
1158 case 0:
1159 return 0;
1160
1161 case ' ':
1162 case '\t':
1163 case '\n':
5776fca3 1164 pstate->lexptr++;
c906108c
SS
1165 goto retry;
1166
1167 case '\'':
1168 token = match_string_literal ();
1169 if (token != 0)
1170 return (token);
1171 break;
1172
1173 case '(':
1174 paren_depth++;
5776fca3 1175 pstate->lexptr++;
c906108c
SS
1176 return c;
1177
1178 case ')':
1179 if (paren_depth == 0)
1180 return 0;
1181 paren_depth--;
5776fca3 1182 pstate->lexptr++;
c906108c
SS
1183 return c;
1184
1185 case ',':
8621b685 1186 if (pstate->comma_terminates && paren_depth == 0)
c906108c 1187 return 0;
5776fca3 1188 pstate->lexptr++;
c906108c
SS
1189 return c;
1190
1191 case '.':
1192 /* Might be a floating point number. */
5776fca3 1193 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
0963b4bd 1194 goto symbol; /* Nope, must be a symbol. */
86a73007 1195 /* FALL THRU. */
c906108c
SS
1196
1197 case '0':
1198 case '1':
1199 case '2':
1200 case '3':
1201 case '4':
1202 case '5':
1203 case '6':
1204 case '7':
1205 case '8':
1206 case '9':
1207 {
1208 /* It's a number. */
1209 int got_dot = 0, got_e = 0, got_d = 0, toktype;
d7561cbb 1210 const char *p = tokstart;
c906108c
SS
1211 int hex = input_radix > 10;
1212
1213 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1214 {
1215 p += 2;
1216 hex = 1;
1217 }
0963b4bd
MS
1218 else if (c == '0' && (p[1]=='t' || p[1]=='T'
1219 || p[1]=='d' || p[1]=='D'))
c906108c
SS
1220 {
1221 p += 2;
1222 hex = 0;
1223 }
1224
1225 for (;; ++p)
1226 {
1227 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1228 got_dot = got_e = 1;
1229 else if (!hex && !got_d && (*p == 'd' || *p == 'D'))
1230 got_dot = got_d = 1;
1231 else if (!hex && !got_dot && *p == '.')
1232 got_dot = 1;
1233 else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
1234 || (got_d && (p[-1] == 'd' || p[-1] == 'D')))
1235 && (*p == '-' || *p == '+'))
1236 /* This is the sign of the exponent, not the end of the
1237 number. */
1238 continue;
1239 /* We will take any letters or digits. parse_number will
1240 complain if past the radix, or if L or U are not final. */
1241 else if ((*p < '0' || *p > '9')
1242 && ((*p < 'a' || *p > 'z')
1243 && (*p < 'A' || *p > 'Z')))
1244 break;
1245 }
410a0ff2
SDJ
1246 toktype = parse_number (pstate, tokstart, p - tokstart,
1247 got_dot|got_e|got_d,
c906108c
SS
1248 &yylval);
1249 if (toktype == ERROR)
1250 {
1251 char *err_copy = (char *) alloca (p - tokstart + 1);
1252
1253 memcpy (err_copy, tokstart, p - tokstart);
1254 err_copy[p - tokstart] = 0;
001083c6 1255 error (_("Invalid number \"%s\"."), err_copy);
c906108c 1256 }
5776fca3 1257 pstate->lexptr = p;
c906108c
SS
1258 return toktype;
1259 }
1260
1261 case '+':
1262 case '-':
1263 case '*':
1264 case '/':
1265 case '%':
1266 case '|':
1267 case '&':
1268 case '^':
1269 case '~':
1270 case '!':
1271 case '@':
1272 case '<':
1273 case '>':
1274 case '[':
1275 case ']':
1276 case '?':
1277 case ':':
1278 case '=':
1279 case '{':
1280 case '}':
1281 symbol:
5776fca3 1282 pstate->lexptr++;
c906108c
SS
1283 return c;
1284 }
1285
f55ee35c 1286 if (!(c == '_' || c == '$' || c ==':'
c906108c
SS
1287 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1288 /* We must have come across a bad character (e.g. ';'). */
001083c6 1289 error (_("Invalid character '%c' in expression."), c);
c906108c
SS
1290
1291 namelen = 0;
1292 for (c = tokstart[namelen];
f55ee35c 1293 (c == '_' || c == '$' || c == ':' || (c >= '0' && c <= '9')
c906108c
SS
1294 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1295 c = tokstart[++namelen]);
1296
1297 /* The token "if" terminates the expression and is NOT
1298 removed from the input stream. */
1299
1300 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1301 return 0;
1302
5776fca3 1303 pstate->lexptr += namelen;
c906108c
SS
1304
1305 /* Catch specific keywords. */
c8f91604
AB
1306
1307 for (int i = 0; i < ARRAY_SIZE (f77_keywords); i++)
fe978cb0 1308 if (strlen (f77_keywords[i].oper) == namelen
c8f91604
AB
1309 && ((!f77_keywords[i].case_sensitive
1310 && strncasecmp (tokstart, f77_keywords[i].oper, namelen) == 0)
1311 || (f77_keywords[i].case_sensitive
1312 && strncmp (tokstart, f77_keywords[i].oper, namelen) == 0)))
c906108c 1313 {
c906108c
SS
1314 yylval.opcode = f77_keywords[i].opcode;
1315 return f77_keywords[i].token;
1316 }
c8f91604 1317
c906108c
SS
1318 yylval.sval.ptr = tokstart;
1319 yylval.sval.length = namelen;
1320
1321 if (*tokstart == '$')
1322 {
410a0ff2 1323 write_dollar_variable (pstate, yylval.sval);
cfeadda5 1324 return DOLLAR_VARIABLE;
c906108c
SS
1325 }
1326
1327 /* Use token-type TYPENAME for symbols that happen to be defined
1328 currently as names of types; NAME for other symbols.
1329 The caller is not constrained to care about the distinction. */
1330 {
61f4b350 1331 std::string tmp = copy_name (yylval.sval);
d12307c1 1332 struct block_symbol result;
530e8392
KB
1333 enum domain_enum_tag lookup_domains[] =
1334 {
1335 STRUCT_DOMAIN,
1336 VAR_DOMAIN,
1337 MODULE_DOMAIN
1338 };
c906108c 1339 int hextype;
7f9b20bb 1340
b926417a 1341 for (int i = 0; i < ARRAY_SIZE (lookup_domains); ++i)
c906108c 1342 {
61f4b350 1343 result = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
43771869 1344 lookup_domains[i], NULL);
d12307c1 1345 if (result.symbol && SYMBOL_CLASS (result.symbol) == LOC_TYPEDEF)
7f9b20bb 1346 {
d12307c1 1347 yylval.tsym.type = SYMBOL_TYPE (result.symbol);
7f9b20bb
KB
1348 return TYPENAME;
1349 }
1350
d12307c1 1351 if (result.symbol)
7f9b20bb 1352 break;
c906108c 1353 }
7f9b20bb 1354
54a5b07d 1355 yylval.tsym.type
73923d7e 1356 = language_lookup_primitive_type (pstate->language (),
61f4b350 1357 pstate->gdbarch (), tmp.c_str ());
54a5b07d 1358 if (yylval.tsym.type != NULL)
c906108c
SS
1359 return TYPENAME;
1360
1361 /* Input names that aren't symbols but ARE valid hex numbers,
1362 when the input radix permits them, can be names or numbers
1363 depending on the parse. Note we support radixes > 16 here. */
d12307c1 1364 if (!result.symbol
c906108c
SS
1365 && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1366 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1367 {
1368 YYSTYPE newlval; /* Its value is ignored. */
410a0ff2 1369 hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
c906108c
SS
1370 if (hextype == INT)
1371 {
d12307c1 1372 yylval.ssym.sym = result;
43771869 1373 yylval.ssym.is_a_field_of_this = false;
c906108c
SS
1374 return NAME_OR_INT;
1375 }
1376 }
1377
1378 /* Any other kind of symbol */
d12307c1 1379 yylval.ssym.sym = result;
43771869 1380 yylval.ssym.is_a_field_of_this = false;
c906108c
SS
1381 return NAME;
1382 }
1383}
1384
410a0ff2 1385int
1a0ea399 1386f_language::parser (struct parser_state *par_state) const
410a0ff2 1387{
410a0ff2 1388 /* Setting up the parser state. */
eae49211 1389 scoped_restore pstate_restore = make_scoped_restore (&pstate);
e454224f
AB
1390 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
1391 parser_debug);
410a0ff2
SDJ
1392 gdb_assert (par_state != NULL);
1393 pstate = par_state;
28aaf3fd 1394 paren_depth = 0;
410a0ff2 1395
dac43e32
TT
1396 struct type_stack stack;
1397 scoped_restore restore_type_stack = make_scoped_restore (&type_stack,
1398 &stack);
1399
eae49211 1400 return yyparse ();
410a0ff2
SDJ
1401}
1402
69d340c6 1403static void
a121b7c1 1404yyerror (const char *msg)
c906108c 1405{
5776fca3
TT
1406 if (pstate->prev_lexptr)
1407 pstate->lexptr = pstate->prev_lexptr;
065432a8 1408
5776fca3 1409 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
c906108c 1410}
This page took 1.517758 seconds and 4 git commands to generate.