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