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