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