2 /* YACC parser for Fortran expressions, for GDB.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 Contributed by Motorola. Adapted from the C parser by Farooq Butt
6 (fmbutt@engage.sps.mot.com).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* This was blantantly ripped off the C expression parser, please
24 be aware of that as you look at its basic structure -FMB */
26 /* Parse a F77 expression from text in a string,
27 and return the result as a struct expression pointer.
28 That structure contains arithmetic operations in reverse polish,
29 with constants represented by operations that are followed by special data.
30 See expression.h for the details of the format.
31 What is important here is that it can be built up sequentially
32 during the process of parsing; the lower levels of the tree always
33 come first in the result.
35 Note that malloc's and realloc's in this file are transformed to
36 xmalloc and xrealloc respectively by the same sed command in the
37 makefile that remaps any other malloc/realloc inserted by the parser
38 generator. Doing this with #defines and trying to control the interaction
39 with include files (<malloc.h> and <stdlib.h> for example) just became
40 too messy, particularly when such includes can be inserted at random
41 times by the parser generator. */
45 #include "expression.h"
47 #include "parser-defs.h"
53 #include "type-stack.h"
56 #define parse_type(ps) builtin_type (ps->gdbarch ())
57 #define parse_f_type(ps) builtin_f_type (ps->gdbarch ())
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
61 #define GDB_YY_REMAP_PREFIX f_
64 /* The state of the parser, used internally when we are parsing the
67 static struct parser_state
*pstate
= NULL
;
69 /* Depth of parentheses. */
70 static int paren_depth
;
72 /* The current type stack. */
73 static struct type_stack
*type_stack
;
77 static int yylex (void);
79 static void yyerror (const char *);
81 static void growbuf_by_size
(int);
83 static int match_string_literal
(void);
85 static void push_kind_type
(LONGEST val
, struct type
*type
);
87 static struct type
*convert_to_kind_type
(struct type
*basetype
, int kind
);
89 static void wrap_unop_intrinsic
(exp_opcode opcode
);
91 static void wrap_binop_intrinsic
(exp_opcode opcode
);
93 static void wrap_ternop_intrinsic
(exp_opcode opcode
);
96 static void fortran_wrap2_kind
(type
*base_type
);
99 static void fortran_wrap3_kind
(type
*base_type
);
101 using namespace expr
;
104 /* Although the yacc "value" of an expression is not used,
105 since the result is stored in the structure being created,
106 other node types do have values. */
123 struct symtoken ssym
;
125 enum exp_opcode opcode
;
126 struct internalvar
*ivar
;
133 /* YYSTYPE gets defined by %union */
134 static int parse_number
(struct parser_state
*, const char *, int,
138 %type
<voidval
> exp type_exp start variable
139 %type
<tval
> type typebase
140 %type
<tvec
> nonempty_typelist
141 /* %type <bval> block */
143 /* Fancy type parsing. */
144 %type
<voidval
> func_mod direct_abs_decl abs_decl
147 %token
<typed_val
> INT
148 %token
<typed_val_float
> FLOAT
150 /* Both NAME and TYPENAME tokens represent symbols in the input,
151 and both convey their data as strings.
152 But a TYPENAME is a string that happens to be defined as a typedef
153 or builtin type name (such as int or char)
154 and a NAME is any other symbol.
155 Contexts where this distinction is not important can use the
156 nonterminal "name", which matches either NAME or TYPENAME. */
158 %token
<sval
> STRING_LITERAL
159 %token
<lval
> BOOLEAN_LITERAL
161 %token
<tsym
> TYPENAME
162 %token
<voidval
> COMPLETE
164 %type
<ssym
> name_not_typename
166 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
167 but which would parse as a valid number in the current input radix.
168 E.g. "c" when input_radix==16. Depending on the parse, it will be
169 turned into a name or into a number. */
171 %token
<ssym
> NAME_OR_INT
176 /* Special type cases, put in to allow the parser to distinguish different
178 %token INT_S1_KEYWORD INT_S2_KEYWORD INT_KEYWORD INT_S4_KEYWORD INT_S8_KEYWORD
179 %token LOGICAL_S1_KEYWORD LOGICAL_S2_KEYWORD LOGICAL_KEYWORD LOGICAL_S4_KEYWORD
180 %token LOGICAL_S8_KEYWORD
181 %token REAL_KEYWORD REAL_S4_KEYWORD REAL_S8_KEYWORD REAL_S16_KEYWORD
182 %token COMPLEX_KEYWORD COMPLEX_S4_KEYWORD COMPLEX_S8_KEYWORD
183 %token COMPLEX_S16_KEYWORD
184 %token BOOL_AND BOOL_OR BOOL_NOT
185 %token SINGLE DOUBLE PRECISION
186 %token
<lval
> CHARACTER
188 %token
<sval
> DOLLAR_VARIABLE
190 %token
<opcode
> ASSIGN_MODIFY
191 %token
<opcode
> UNOP_INTRINSIC BINOP_INTRINSIC
192 %token
<opcode
> UNOP_OR_BINOP_INTRINSIC UNOP_OR_BINOP_OR_TERNOP_INTRINSIC
196 %right
'=' ASSIGN_MODIFY
205 %left LESSTHAN GREATERTHAN LEQ GEQ
223 { pstate
->push_new
<type_operation
> ($1); }
230 /* Expressions, not including the comma operator. */
231 exp
: '*' exp %prec UNARY
232 { pstate
->wrap
<unop_ind_operation
> (); }
235 exp
: '&' exp %prec UNARY
236 { pstate
->wrap
<unop_addr_operation
> (); }
239 exp
: '-' exp %prec UNARY
240 { pstate
->wrap
<unary_neg_operation
> (); }
243 exp
: BOOL_NOT exp %prec UNARY
244 { pstate
->wrap
<unary_logical_not_operation
> (); }
247 exp
: '~' exp %prec UNARY
248 { pstate
->wrap
<unary_complement_operation
> (); }
251 exp
: SIZEOF exp %prec UNARY
252 { pstate
->wrap
<unop_sizeof_operation
> (); }
255 exp
: KIND
'(' exp
')' %prec UNARY
256 { pstate
->wrap
<fortran_kind_operation
> (); }
259 /* No more explicit array operators, we treat everything in F77 as
260 a function call. The disambiguation as to whether we are
261 doing a subscript operation or a function call is done
265 { pstate
->start_arglist
(); }
268 std
::vector
<operation_up
> args
269 = pstate
->pop_vector
(pstate
->end_arglist
());
270 pstate
->push_new
<fortran_undetermined
>
271 (pstate
->pop
(), std
::move
(args
));
275 exp
: UNOP_INTRINSIC
'(' exp
')'
277 wrap_unop_intrinsic
($1);
281 exp
: BINOP_INTRINSIC
'(' exp
',' exp
')'
283 wrap_binop_intrinsic
($1);
287 exp
: UNOP_OR_BINOP_INTRINSIC
'('
288 { pstate
->start_arglist
(); }
291 const int n
= pstate
->end_arglist
();
296 wrap_unop_intrinsic
($1);
299 wrap_binop_intrinsic
($1);
302 gdb_assert_not_reached
303 ("wrong number of arguments for intrinsics");
307 exp
: UNOP_OR_BINOP_OR_TERNOP_INTRINSIC
'('
308 { pstate
->start_arglist
(); }
311 const int n
= pstate
->end_arglist
();
316 wrap_unop_intrinsic
($1);
319 wrap_binop_intrinsic
($1);
322 wrap_ternop_intrinsic
($1);
325 gdb_assert_not_reached
326 ("wrong number of arguments for intrinsics");
335 { pstate
->arglist_len
= 1; }
339 { pstate
->arglist_len
= 1; }
342 arglist
: arglist
',' exp %prec ABOVE_COMMA
343 { pstate
->arglist_len
++; }
346 arglist
: arglist
',' subrange %prec ABOVE_COMMA
347 { pstate
->arglist_len
++; }
350 /* There are four sorts of subrange types in F90. */
352 subrange: exp
':' exp %prec ABOVE_COMMA
354 operation_up high
= pstate
->pop
();
355 operation_up low
= pstate
->pop
();
356 pstate
->push_new
<fortran_range_operation
>
357 (RANGE_STANDARD
, std
::move
(low
),
358 std
::move
(high
), operation_up
());
362 subrange: exp
':' %prec ABOVE_COMMA
364 operation_up low
= pstate
->pop
();
365 pstate
->push_new
<fortran_range_operation
>
366 (RANGE_HIGH_BOUND_DEFAULT
, std
::move
(low
),
367 operation_up
(), operation_up
());
371 subrange: ':' exp %prec ABOVE_COMMA
373 operation_up high
= pstate
->pop
();
374 pstate
->push_new
<fortran_range_operation
>
375 (RANGE_LOW_BOUND_DEFAULT
, operation_up
(),
376 std
::move
(high
), operation_up
());
380 subrange: ':' %prec ABOVE_COMMA
382 pstate
->push_new
<fortran_range_operation
>
383 (RANGE_LOW_BOUND_DEFAULT
384 | RANGE_HIGH_BOUND_DEFAULT
,
385 operation_up
(), operation_up
(),
390 /* And each of the four subrange types can also have a stride. */
391 subrange: exp
':' exp
':' exp %prec ABOVE_COMMA
393 operation_up stride
= pstate
->pop
();
394 operation_up high
= pstate
->pop
();
395 operation_up low
= pstate
->pop
();
396 pstate
->push_new
<fortran_range_operation
>
397 (RANGE_STANDARD | RANGE_HAS_STRIDE
,
398 std
::move
(low
), std
::move
(high
),
403 subrange: exp
':' ':' exp %prec ABOVE_COMMA
405 operation_up stride
= pstate
->pop
();
406 operation_up low
= pstate
->pop
();
407 pstate
->push_new
<fortran_range_operation
>
408 (RANGE_HIGH_BOUND_DEFAULT
410 std
::move
(low
), operation_up
(),
415 subrange: ':' exp
':' exp %prec ABOVE_COMMA
417 operation_up stride
= pstate
->pop
();
418 operation_up high
= pstate
->pop
();
419 pstate
->push_new
<fortran_range_operation
>
420 (RANGE_LOW_BOUND_DEFAULT
422 operation_up
(), std
::move
(high
),
427 subrange: ':' ':' exp %prec ABOVE_COMMA
429 operation_up stride
= pstate
->pop
();
430 pstate
->push_new
<fortran_range_operation
>
431 (RANGE_LOW_BOUND_DEFAULT
432 | RANGE_HIGH_BOUND_DEFAULT
434 operation_up
(), operation_up
(),
439 complexnum: exp
',' exp
443 exp
: '(' complexnum
')'
445 operation_up rhs
= pstate
->pop
();
446 operation_up lhs
= pstate
->pop
();
447 pstate
->push_new
<complex_operation
>
448 (std
::move
(lhs
), std
::move
(rhs
),
449 parse_f_type
(pstate
)->builtin_complex_s16
);
453 exp
: '(' type
')' exp %prec UNARY
455 pstate
->push_new
<unop_cast_operation
>
456 (pstate
->pop
(), $2);
462 pstate
->push_new
<fortran_structop_operation
>
463 (pstate
->pop
(), copy_name
($3));
467 exp
: exp
'%' name COMPLETE
469 structop_base_operation
*op
470 = new fortran_structop_operation
(pstate
->pop
(),
472 pstate
->mark_struct_expression
(op
);
473 pstate
->push
(operation_up
(op
));
477 exp
: exp
'%' COMPLETE
479 structop_base_operation
*op
480 = new fortran_structop_operation
(pstate
->pop
(),
482 pstate
->mark_struct_expression
(op
);
483 pstate
->push
(operation_up
(op
));
487 /* Binary operators in order of decreasing precedence. */
490 { pstate
->wrap2
<repeat_operation
> (); }
493 exp
: exp STARSTAR exp
494 { pstate
->wrap2
<exp_operation
> (); }
498 { pstate
->wrap2
<mul_operation
> (); }
502 { pstate
->wrap2
<div_operation
> (); }
506 { pstate
->wrap2
<add_operation
> (); }
510 { pstate
->wrap2
<sub_operation
> (); }
514 { pstate
->wrap2
<lsh_operation
> (); }
518 { pstate
->wrap2
<rsh_operation
> (); }
522 { pstate
->wrap2
<equal_operation
> (); }
525 exp
: exp NOTEQUAL exp
526 { pstate
->wrap2
<notequal_operation
> (); }
530 { pstate
->wrap2
<leq_operation
> (); }
534 { pstate
->wrap2
<geq_operation
> (); }
537 exp
: exp LESSTHAN exp
538 { pstate
->wrap2
<less_operation
> (); }
541 exp
: exp GREATERTHAN exp
542 { pstate
->wrap2
<gtr_operation
> (); }
546 { pstate
->wrap2
<bitwise_and_operation
> (); }
550 { pstate
->wrap2
<bitwise_xor_operation
> (); }
554 { pstate
->wrap2
<bitwise_ior_operation
> (); }
557 exp
: exp BOOL_AND exp
558 { pstate
->wrap2
<logical_and_operation
> (); }
562 exp
: exp BOOL_OR exp
563 { pstate
->wrap2
<logical_or_operation
> (); }
567 { pstate
->wrap2
<assign_operation
> (); }
570 exp
: exp ASSIGN_MODIFY exp
572 operation_up rhs
= pstate
->pop
();
573 operation_up lhs
= pstate
->pop
();
574 pstate
->push_new
<assign_modify_operation
>
575 ($2, std
::move
(lhs
), std
::move
(rhs
));
581 pstate
->push_new
<long_const_operation
>
588 parse_number
(pstate
, $1.stoken.ptr
,
589 $1.stoken.length
, 0, &val
);
590 pstate
->push_new
<long_const_operation
>
599 std
::copy
(std
::begin
($1.val
), std
::end
($1.val
),
601 pstate
->push_new
<float_const_operation
> ($1.type
, data
);
608 exp
: DOLLAR_VARIABLE
609 { pstate
->push_dollar
($1); }
612 exp
: SIZEOF
'(' type
')' %prec UNARY
614 $3 = check_typedef
($3);
615 pstate
->push_new
<long_const_operation
>
616 (parse_f_type
(pstate
)->builtin_integer
,
621 exp
: BOOLEAN_LITERAL
622 { pstate
->push_new
<bool_operation
> ($1); }
627 pstate
->push_new
<string_operation
>
632 variable: name_not_typename
633 { struct block_symbol sym
= $1.sym
;
634 std
::string name
= copy_name
($1.stoken
);
635 pstate
->push_symbol
(name.c_str
(), sym
);
646 /* This is where the interesting stuff happens. */
649 struct type
*follow_type
= $1;
650 struct type
*range_type
;
653 switch
(type_stack
->pop
())
659 follow_type
= lookup_pointer_type
(follow_type
);
662 follow_type
= lookup_lvalue_reference_type
(follow_type
);
665 array_size
= type_stack
->pop_int
();
666 if
(array_size
!= -1)
668 struct type
*idx_type
669 = parse_f_type
(pstate
)->builtin_integer
;
670 type_allocator alloc
(idx_type
);
672 create_static_range_type
(alloc
, idx_type
,
674 follow_type
= create_array_type
(alloc
,
679 follow_type
= lookup_pointer_type
(follow_type
);
682 follow_type
= lookup_function_type
(follow_type
);
686 int kind_val
= type_stack
->pop_int
();
688 = convert_to_kind_type
(follow_type
, kind_val
);
697 { type_stack
->push
(tp_pointer
); $$
= 0; }
699 { type_stack
->push
(tp_pointer
); $$
= $2; }
701 { type_stack
->push
(tp_reference
); $$
= 0; }
703 { type_stack
->push
(tp_reference
); $$
= $2; }
707 direct_abs_decl: '(' abs_decl
')'
709 |
'(' KIND
'=' INT
')'
710 { push_kind_type
($4.val
, $4.type
); }
712 { push_kind_type
($2.val
, $2.type
); }
713 | direct_abs_decl func_mod
714 { type_stack
->push
(tp_function
); }
716 { type_stack
->push
(tp_function
); }
721 |
'(' nonempty_typelist
')'
722 { free
($2); $$
= 0; }
725 typebase
/* Implements (approximately): (type-qualifier)* type-specifier */
729 { $$
= parse_f_type
(pstate
)->builtin_integer_s1
; }
731 { $$
= parse_f_type
(pstate
)->builtin_integer_s2
; }
733 { $$
= parse_f_type
(pstate
)->builtin_integer
; }
735 { $$
= parse_f_type
(pstate
)->builtin_integer
; }
737 { $$
= parse_f_type
(pstate
)->builtin_integer_s8
; }
739 { $$
= parse_f_type
(pstate
)->builtin_character
; }
741 { $$
= parse_f_type
(pstate
)->builtin_logical_s1
; }
743 { $$
= parse_f_type
(pstate
)->builtin_logical_s2
; }
745 { $$
= parse_f_type
(pstate
)->builtin_logical
; }
747 { $$
= parse_f_type
(pstate
)->builtin_logical
; }
749 { $$
= parse_f_type
(pstate
)->builtin_logical_s8
; }
751 { $$
= parse_f_type
(pstate
)->builtin_real
; }
753 { $$
= parse_f_type
(pstate
)->builtin_real
; }
755 { $$
= parse_f_type
(pstate
)->builtin_real_s8
; }
757 { $$
= parse_f_type
(pstate
)->builtin_real_s16
;
758 if
($$
->code
() == TYPE_CODE_ERROR
)
759 error (_
("unsupported type %s"),
760 TYPE_SAFE_NAME
($$
));
763 { $$
= parse_f_type
(pstate
)->builtin_complex
; }
765 { $$
= parse_f_type
(pstate
)->builtin_complex
; }
767 { $$
= parse_f_type
(pstate
)->builtin_complex_s8
; }
768 | COMPLEX_S16_KEYWORD
769 { $$
= parse_f_type
(pstate
)->builtin_complex_s16
;
770 if
($$
->code
() == TYPE_CODE_ERROR
)
771 error (_
("unsupported type %s"),
772 TYPE_SAFE_NAME
($$
));
775 { $$
= parse_f_type
(pstate
)->builtin_real
;}
777 { $$
= parse_f_type
(pstate
)->builtin_real_s8
;}
778 | SINGLE COMPLEX_KEYWORD
779 { $$
= parse_f_type
(pstate
)->builtin_complex
;}
780 | DOUBLE COMPLEX_KEYWORD
781 { $$
= parse_f_type
(pstate
)->builtin_complex_s8
;}
786 { $$
= (struct type
**) malloc
(sizeof
(struct type
*) * 2);
787 $
<ivec
>$
[0] = 1; /* Number of types in vector */
790 | nonempty_typelist
',' type
791 { int len
= sizeof
(struct type
*) * (++($
<ivec
>1[0]) + 1);
792 $$
= (struct type
**) realloc
((char *) $1, len
);
793 $$
[$
<ivec
>$
[0]] = $3;
804 name_not_typename
: NAME
805 /* These would be useful if name_not_typename was useful, but it is just
806 a fake for "variable", so these cause reduce/reduce conflicts because
807 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
808 =exp) or just an exp. If name_not_typename was ever used in an lvalue
809 context where only a name could occur, this might be useful.
816 /* Called to match intrinsic function calls with one argument to their
817 respective implementation and push the operation. */
820 wrap_unop_intrinsic
(exp_opcode code
)
825 pstate
->wrap
<fortran_abs_operation
> ();
828 pstate
->wrap
<fortran_floor_operation_1arg
> ();
830 case FORTRAN_CEILING
:
831 pstate
->wrap
<fortran_ceil_operation_1arg
> ();
833 case UNOP_FORTRAN_ALLOCATED
:
834 pstate
->wrap
<fortran_allocated_operation
> ();
836 case UNOP_FORTRAN_RANK
:
837 pstate
->wrap
<fortran_rank_operation
> ();
839 case UNOP_FORTRAN_SHAPE
:
840 pstate
->wrap
<fortran_array_shape_operation
> ();
842 case UNOP_FORTRAN_LOC
:
843 pstate
->wrap
<fortran_loc_operation
> ();
845 case FORTRAN_ASSOCIATED
:
846 pstate
->wrap
<fortran_associated_1arg
> ();
848 case FORTRAN_ARRAY_SIZE
:
849 pstate
->wrap
<fortran_array_size_1arg
> ();
852 pstate
->wrap
<fortran_cmplx_operation_1arg
> ();
856 pstate
->push_new
<fortran_bound_1arg
> (code
, pstate
->pop
());
859 gdb_assert_not_reached
("unhandled intrinsic");
863 /* Called to match intrinsic function calls with two arguments to their
864 respective implementation and push the operation. */
867 wrap_binop_intrinsic
(exp_opcode code
)
872 fortran_wrap2_kind
<fortran_floor_operation_2arg
>
873 (parse_f_type
(pstate
)->builtin_integer
);
875 case FORTRAN_CEILING
:
876 fortran_wrap2_kind
<fortran_ceil_operation_2arg
>
877 (parse_f_type
(pstate
)->builtin_integer
);
880 pstate
->wrap2
<fortran_mod_operation
> ();
882 case BINOP_FORTRAN_MODULO
:
883 pstate
->wrap2
<fortran_modulo_operation
> ();
886 pstate
->wrap2
<fortran_cmplx_operation_2arg
> ();
888 case FORTRAN_ASSOCIATED
:
889 pstate
->wrap2
<fortran_associated_2arg
> ();
891 case FORTRAN_ARRAY_SIZE
:
892 pstate
->wrap2
<fortran_array_size_2arg
> ();
897 operation_up arg2
= pstate
->pop
();
898 operation_up arg1
= pstate
->pop
();
899 pstate
->push_new
<fortran_bound_2arg
> (code
, std
::move
(arg1
),
904 gdb_assert_not_reached
("unhandled intrinsic");
908 /* Called to match intrinsic function calls with three arguments to their
909 respective implementation and push the operation. */
912 wrap_ternop_intrinsic
(exp_opcode code
)
919 operation_up kind_arg
= pstate
->pop
();
920 operation_up arg2
= pstate
->pop
();
921 operation_up arg1
= pstate
->pop
();
923 value
*val
= kind_arg
->evaluate
(nullptr
, pstate
->expout.get
(),
924 EVAL_AVOID_SIDE_EFFECTS
);
925 gdb_assert
(val
!= nullptr
);
928 = convert_to_kind_type
(parse_f_type
(pstate
)->builtin_integer
,
929 value_as_long
(val
));
931 pstate
->push_new
<fortran_bound_3arg
> (code
, std
::move
(arg1
),
932 std
::move
(arg2
), follow_type
);
935 case FORTRAN_ARRAY_SIZE
:
936 fortran_wrap3_kind
<fortran_array_size_3arg
>
937 (parse_f_type
(pstate
)->builtin_integer
);
940 fortran_wrap3_kind
<fortran_cmplx_operation_3arg
>
941 (parse_f_type
(pstate
)->builtin_complex
);
944 gdb_assert_not_reached
("unhandled intrinsic");
948 /* A helper that pops two operations (similar to wrap2), evaluates the last one
949 assuming it is a kind parameter, and wraps them in some other operation
950 pushing it to the stack. */
954 fortran_wrap2_kind
(type
*base_type
)
956 operation_up kind_arg
= pstate
->pop
();
957 operation_up arg
= pstate
->pop
();
959 value
*val
= kind_arg
->evaluate
(nullptr
, pstate
->expout.get
(),
960 EVAL_AVOID_SIDE_EFFECTS
);
961 gdb_assert
(val
!= nullptr
);
963 type
*follow_type
= convert_to_kind_type
(base_type
, value_as_long
(val
));
965 pstate
->push_new
<T
> (std
::move
(arg
), follow_type
);
968 /* A helper that pops three operations, evaluates the last one assuming it is a
969 kind parameter, and wraps them in some other operation pushing it to the
974 fortran_wrap3_kind
(type
*base_type
)
976 operation_up kind_arg
= pstate
->pop
();
977 operation_up arg2
= pstate
->pop
();
978 operation_up arg1
= pstate
->pop
();
980 value
*val
= kind_arg
->evaluate
(nullptr
, pstate
->expout.get
(),
981 EVAL_AVOID_SIDE_EFFECTS
);
982 gdb_assert
(val
!= nullptr
);
984 type
*follow_type
= convert_to_kind_type
(base_type
, value_as_long
(val
));
986 pstate
->push_new
<T
> (std
::move
(arg1
), std
::move
(arg2
), follow_type
);
989 /* Take care of parsing a number (anything that starts with a digit).
990 Set yylval and return the token type; update lexptr.
991 LEN is the number of characters in it. */
993 /*** Needs some error checking for the float case ***/
996 parse_number
(struct parser_state
*par_state
,
997 const char *p
, int len
, int parsed_float
, YYSTYPE *putithere
)
1002 int base
= input_radix
;
1006 struct type
*signed_type
;
1007 struct type
*unsigned_type
;
1011 /* It's a float since it contains a point or an exponent. */
1012 /* [dD] is not understood as an exponent by parse_float,
1013 change it to 'e'. */
1017 for
(tmp2
= tmp
; *tmp2
; ++tmp2
)
1018 if
(*tmp2
== 'd' ||
*tmp2
== 'D')
1021 /* FIXME: Should this use different types? */
1022 putithere
->typed_val_float.type
= parse_f_type
(pstate
)->builtin_real_s8
;
1023 bool parsed
= parse_float
(tmp
, len
,
1024 putithere
->typed_val_float.type
,
1025 putithere
->typed_val_float.val
);
1027 return parsed? FLOAT
: ERROR
;
1030 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1031 if
(p
[0] == '0' && len
> 1)
1066 if
(len
== 0 && c
== 'l')
1068 else if
(len
== 0 && c
== 'u')
1073 if
(c
>= '0' && c
<= '9')
1075 else if
(c
>= 'a' && c
<= 'f')
1078 return ERROR
; /* Char not a digit */
1080 return ERROR
; /* Invalid digit in this base */
1084 /* Test for overflow. */
1085 if
(prevn
== 0 && n
== 0)
1087 else if
(RANGE_CHECK
&& prevn
>= n
)
1088 range_error
(_
("Overflow on numeric constant."));
1092 /* If the number is too big to be an int, or it's got an l suffix
1093 then it's a long. Work out if this has to be a long by
1094 shifting right and seeing if anything remains, and the
1095 target int size is different to the target long size.
1097 In the expression below, we could have tested
1098 (n >> gdbarch_int_bit (parse_gdbarch))
1099 to see if it was zero,
1100 but too many compilers warn about that, when ints and longs
1101 are the same size. So we shift it twice, with fewer bits
1102 each time, for the same result. */
1105 if
((gdbarch_int_bit
(par_state
->gdbarch
())
1106 != gdbarch_long_bit
(par_state
->gdbarch
())
1108 >> (gdbarch_int_bit
(par_state
->gdbarch
())-2))) /* Avoid
1112 bits_available
= gdbarch_long_bit
(par_state
->gdbarch
());
1113 unsigned_type
= parse_type
(par_state
)->builtin_unsigned_long
;
1114 signed_type
= parse_type
(par_state
)->builtin_long
;
1118 bits_available
= gdbarch_int_bit
(par_state
->gdbarch
());
1119 unsigned_type
= parse_type
(par_state
)->builtin_unsigned_int
;
1120 signed_type
= parse_type
(par_state
)->builtin_int
;
1122 high_bit
= ((ULONGEST
)1) << (bits_available
- 1);
1125 && ((n
>> 2) >> (bits_available
- 2)))
1126 range_error
(_
("Overflow on numeric constant."));
1128 putithere
->typed_val.val
= n
;
1130 /* If the high bit of the worked out type is set then this number
1131 has to be unsigned. */
1133 if
(unsigned_p ||
(n
& high_bit
))
1134 putithere
->typed_val.type
= unsigned_type
;
1136 putithere
->typed_val.type
= signed_type
;
1141 /* Called to setup the type stack when we encounter a '(kind=N)' type
1142 modifier, performs some bounds checking on 'N' and then pushes this to
1143 the type stack followed by the 'tp_kind' marker. */
1145 push_kind_type
(LONGEST val
, struct type
*type
)
1149 if
(type
->is_unsigned
())
1151 ULONGEST uval
= static_cast
<ULONGEST
> (val
);
1153 error (_
("kind value out of range"));
1154 ival
= static_cast
<int> (uval
);
1158 if
(val
> INT_MAX || val
< 0)
1159 error (_
("kind value out of range"));
1160 ival
= static_cast
<int> (val
);
1163 type_stack
->push
(ival
);
1164 type_stack
->push
(tp_kind
);
1167 /* Helper function for convert_to_kind_type. */
1168 static struct type
*
1169 convert_to_kind_type_1
(struct type
*basetype
, int kind
)
1171 if
(basetype
== parse_f_type
(pstate
)->builtin_character
)
1173 /* Character of kind 1 is a special case, this is the same as the
1174 base character type. */
1176 return parse_f_type
(pstate
)->builtin_character
;
1178 else if
(basetype
== parse_f_type
(pstate
)->builtin_complex
)
1181 return parse_f_type
(pstate
)->builtin_complex
;
1183 return parse_f_type
(pstate
)->builtin_complex_s8
;
1184 else if
(kind
== 16)
1185 return parse_f_type
(pstate
)->builtin_complex_s16
;
1187 else if
(basetype
== parse_f_type
(pstate
)->builtin_real
)
1190 return parse_f_type
(pstate
)->builtin_real
;
1192 return parse_f_type
(pstate
)->builtin_real_s8
;
1193 else if
(kind
== 16)
1194 return parse_f_type
(pstate
)->builtin_real_s16
;
1196 else if
(basetype
== parse_f_type
(pstate
)->builtin_logical
)
1199 return parse_f_type
(pstate
)->builtin_logical_s1
;
1201 return parse_f_type
(pstate
)->builtin_logical_s2
;
1203 return parse_f_type
(pstate
)->builtin_logical
;
1205 return parse_f_type
(pstate
)->builtin_logical_s8
;
1207 else if
(basetype
== parse_f_type
(pstate
)->builtin_integer
)
1210 return parse_f_type
(pstate
)->builtin_integer_s1
;
1212 return parse_f_type
(pstate
)->builtin_integer_s2
;
1214 return parse_f_type
(pstate
)->builtin_integer
;
1216 return parse_f_type
(pstate
)->builtin_integer_s8
;
1222 /* Called when a type has a '(kind=N)' modifier after it, for example
1223 'character(kind=1)'. The BASETYPE is the type described by 'character'
1224 in our example, and KIND is the integer '1'. This function returns a
1225 new type that represents the basetype of a specific kind. */
1226 static struct type
*
1227 convert_to_kind_type
(struct type
*basetype
, int kind
)
1229 struct type
*res
= convert_to_kind_type_1
(basetype
, kind
);
1231 if
(res
== nullptr || res
->code
() == TYPE_CODE_ERROR
)
1232 error (_
("unsupported kind %d for type %s"),
1233 kind
, TYPE_SAFE_NAME
(basetype
));
1240 /* The string to match against. */
1243 /* The lexer token to return. */
1246 /* The expression opcode to embed within the token. */
1247 enum exp_opcode opcode
;
1249 /* When this is true the string in OPER is matched exactly including
1250 case, when this is false OPER is matched case insensitively. */
1251 bool case_sensitive
;
1254 /* List of Fortran operators. */
1256 static const struct f_token fortran_operators
[] =
1258 { ".and.", BOOL_AND
, OP_NULL
, false
},
1259 { ".or.", BOOL_OR
, OP_NULL
, false
},
1260 { ".not.", BOOL_NOT
, OP_NULL
, false
},
1261 { ".eq.", EQUAL
, OP_NULL
, false
},
1262 { ".eqv.", EQUAL
, OP_NULL
, false
},
1263 { ".neqv.", NOTEQUAL
, OP_NULL
, false
},
1264 { ".xor.", NOTEQUAL
, OP_NULL
, false
},
1265 { "==", EQUAL
, OP_NULL
, false
},
1266 { ".ne.", NOTEQUAL
, OP_NULL
, false
},
1267 { "/=", NOTEQUAL
, OP_NULL
, false
},
1268 { ".le.", LEQ
, OP_NULL
, false
},
1269 { "<=", LEQ
, OP_NULL
, false
},
1270 { ".ge.", GEQ
, OP_NULL
, false
},
1271 { ">=", GEQ
, OP_NULL
, false
},
1272 { ".gt.", GREATERTHAN
, OP_NULL
, false
},
1273 { ">", GREATERTHAN
, OP_NULL
, false
},
1274 { ".lt.", LESSTHAN
, OP_NULL
, false
},
1275 { "<", LESSTHAN
, OP_NULL
, false
},
1276 { "**", STARSTAR
, BINOP_EXP
, false
},
1279 /* Holds the Fortran representation of a boolean, and the integer value we
1280 substitute in when one of the matching strings is parsed. */
1281 struct f77_boolean_val
1283 /* The string representing a Fortran boolean. */
1286 /* The integer value to replace it with. */
1290 /* The set of Fortran booleans. These are matched case insensitively. */
1291 static const struct f77_boolean_val boolean_values
[] =
1297 static const struct f_token f_intrinsics
[] =
1299 /* The following correspond to actual functions in Fortran and are case
1301 { "kind", KIND
, OP_NULL
, false
},
1302 { "abs", UNOP_INTRINSIC
, UNOP_ABS
, false
},
1303 { "mod", BINOP_INTRINSIC
, BINOP_MOD
, false
},
1304 { "floor", UNOP_OR_BINOP_INTRINSIC
, FORTRAN_FLOOR
, false
},
1305 { "ceiling", UNOP_OR_BINOP_INTRINSIC
, FORTRAN_CEILING
, false
},
1306 { "modulo", BINOP_INTRINSIC
, BINOP_FORTRAN_MODULO
, false
},
1307 { "cmplx", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC
, FORTRAN_CMPLX
, false
},
1308 { "lbound", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC
, FORTRAN_LBOUND
, false
},
1309 { "ubound", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC
, FORTRAN_UBOUND
, false
},
1310 { "allocated", UNOP_INTRINSIC
, UNOP_FORTRAN_ALLOCATED
, false
},
1311 { "associated", UNOP_OR_BINOP_INTRINSIC
, FORTRAN_ASSOCIATED
, false
},
1312 { "rank", UNOP_INTRINSIC
, UNOP_FORTRAN_RANK
, false
},
1313 { "size", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC
, FORTRAN_ARRAY_SIZE
, false
},
1314 { "shape", UNOP_INTRINSIC
, UNOP_FORTRAN_SHAPE
, false
},
1315 { "loc", UNOP_INTRINSIC
, UNOP_FORTRAN_LOC
, false
},
1316 { "sizeof", SIZEOF
, OP_NULL
, false
},
1319 static const f_token f_keywords
[] =
1321 /* Historically these have always been lowercase only in GDB. */
1322 { "character", CHARACTER
, OP_NULL
, true
},
1323 { "complex", COMPLEX_KEYWORD
, OP_NULL
, true
},
1324 { "complex_4", COMPLEX_S4_KEYWORD
, OP_NULL
, true
},
1325 { "complex_8", COMPLEX_S8_KEYWORD
, OP_NULL
, true
},
1326 { "complex_16", COMPLEX_S16_KEYWORD
, OP_NULL
, true
},
1327 { "integer_1", INT_S1_KEYWORD
, OP_NULL
, true
},
1328 { "integer_2", INT_S2_KEYWORD
, OP_NULL
, true
},
1329 { "integer_4", INT_S4_KEYWORD
, OP_NULL
, true
},
1330 { "integer", INT_KEYWORD
, OP_NULL
, true
},
1331 { "integer_8", INT_S8_KEYWORD
, OP_NULL
, true
},
1332 { "logical_1", LOGICAL_S1_KEYWORD
, OP_NULL
, true
},
1333 { "logical_2", LOGICAL_S2_KEYWORD
, OP_NULL
, true
},
1334 { "logical", LOGICAL_KEYWORD
, OP_NULL
, true
},
1335 { "logical_4", LOGICAL_S4_KEYWORD
, OP_NULL
, true
},
1336 { "logical_8", LOGICAL_S8_KEYWORD
, OP_NULL
, true
},
1337 { "real", REAL_KEYWORD
, OP_NULL
, true
},
1338 { "real_4", REAL_S4_KEYWORD
, OP_NULL
, true
},
1339 { "real_8", REAL_S8_KEYWORD
, OP_NULL
, true
},
1340 { "real_16", REAL_S16_KEYWORD
, OP_NULL
, true
},
1341 { "single", SINGLE
, OP_NULL
, true
},
1342 { "double", DOUBLE
, OP_NULL
, true
},
1343 { "precision", PRECISION
, OP_NULL
, true
},
1346 /* Implementation of a dynamically expandable buffer for processing input
1347 characters acquired through lexptr and building a value to return in
1348 yylval. Ripped off from ch-exp.y */
1350 static char *tempbuf
; /* Current buffer contents */
1351 static int tempbufsize
; /* Size of allocated buffer */
1352 static int tempbufindex
; /* Current index into buffer */
1354 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1356 #define CHECKBUF(size) \
1358 if
(tempbufindex
+ (size
) >= tempbufsize
) \
1360 growbuf_by_size
(size
); \
1365 /* Grow the static temp buffer if necessary, including allocating the
1366 first one on demand. */
1369 growbuf_by_size
(int count
)
1373 growby
= std
::max
(count
, GROWBY_MIN_SIZE
);
1374 tempbufsize
+= growby
;
1375 if
(tempbuf
== NULL
)
1376 tempbuf
= (char *) malloc
(tempbufsize
);
1378 tempbuf
= (char *) realloc
(tempbuf
, tempbufsize
);
1381 /* Blatantly ripped off from ch-exp.y. This routine recognizes F77
1384 Recognize a string literal. A string literal is a nonzero sequence
1385 of characters enclosed in matching single quotes, except that
1386 a single character inside single quotes is a character literal, which
1387 we reject as a string literal. To embed the terminator character inside
1388 a string, it is simply doubled (I.E. 'this''is''one''string') */
1391 match_string_literal
(void)
1393 const char *tokptr
= pstate
->lexptr
;
1395 for
(tempbufindex
= 0, tokptr
++; *tokptr
!= '\0'; tokptr
++)
1398 if
(*tokptr
== *pstate
->lexptr
)
1400 if
(*(tokptr
+ 1) == *pstate
->lexptr
)
1405 tempbuf
[tempbufindex
++] = *tokptr
;
1407 if
(*tokptr
== '\0' /* no terminator */
1408 || tempbufindex
== 0) /* no string */
1412 tempbuf
[tempbufindex
] = '\0';
1413 yylval.sval.ptr
= tempbuf
;
1414 yylval.sval.length
= tempbufindex
;
1415 pstate
->lexptr
= ++tokptr
;
1416 return STRING_LITERAL
;
1420 /* This is set if a NAME token appeared at the very end of the input
1421 string, with no whitespace separating the name from the EOF. This
1422 is used only when parsing to do field name completion. */
1423 static bool saw_name_at_eof
;
1425 /* This is set if the previously-returned token was a structure
1427 static bool last_was_structop
;
1429 /* Read one token, getting characters through lexptr. */
1437 const char *tokstart
;
1438 bool saw_structop
= last_was_structop
;
1440 last_was_structop
= false
;
1444 pstate
->prev_lexptr
= pstate
->lexptr
;
1446 tokstart
= pstate
->lexptr
;
1448 /* First of all, let us make sure we are not dealing with the
1449 special tokens .true. and .false. which evaluate to 1 and 0. */
1451 if
(*pstate
->lexptr
== '.')
1453 for
(const auto
&candidate
: boolean_values
)
1455 if
(strncasecmp
(tokstart
, candidate.name
,
1456 strlen
(candidate.name
)) == 0)
1458 pstate
->lexptr
+= strlen
(candidate.name
);
1459 yylval.lval
= candidate.value
;
1460 return BOOLEAN_LITERAL
;
1465 /* See if it is a Fortran operator. */
1466 for
(const auto
&candidate
: fortran_operators
)
1467 if
(strncasecmp
(tokstart
, candidate.oper
,
1468 strlen
(candidate.oper
)) == 0)
1470 gdb_assert
(!candidate.case_sensitive
);
1471 pstate
->lexptr
+= strlen
(candidate.oper
);
1472 yylval.opcode
= candidate.opcode
;
1473 return candidate.token
;
1476 switch
(c
= *tokstart
)
1479 if
(saw_name_at_eof
)
1481 saw_name_at_eof
= false
;
1484 else if
(pstate
->parse_completion
&& saw_structop
)
1495 token
= match_string_literal
();
1506 if
(paren_depth
== 0)
1513 if
(pstate
->comma_terminates
&& paren_depth
== 0)
1519 /* Might be a floating point number. */
1520 if
(pstate
->lexptr
[1] < '0' || pstate
->lexptr
[1] > '9')
1521 goto symbol
; /* Nope, must be a symbol. */
1535 /* It's a number. */
1536 int got_dot
= 0, got_e
= 0, got_d
= 0, toktype
;
1537 const char *p
= tokstart
;
1538 int hex
= input_radix
> 10;
1540 if
(c
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
1545 else if
(c
== '0' && (p
[1]=='t' || p
[1]=='T'
1546 || p
[1]=='d' || p
[1]=='D'))
1554 if
(!hex
&& !got_e
&& (*p
== 'e' ||
*p
== 'E'))
1555 got_dot
= got_e
= 1;
1556 else if
(!hex
&& !got_d
&& (*p
== 'd' ||
*p
== 'D'))
1557 got_dot
= got_d
= 1;
1558 else if
(!hex
&& !got_dot
&& *p
== '.')
1560 else if
(((got_e
&& (p
[-1] == 'e' || p
[-1] == 'E'))
1561 ||
(got_d
&& (p
[-1] == 'd' || p
[-1] == 'D')))
1562 && (*p
== '-' ||
*p
== '+'))
1563 /* This is the sign of the exponent, not the end of the
1566 /* We will take any letters or digits. parse_number will
1567 complain if past the radix, or if L or U are not final. */
1568 else if
((*p
< '0' ||
*p
> '9')
1569 && ((*p
< 'a' ||
*p
> 'z')
1570 && (*p
< 'A' ||
*p
> 'Z')))
1573 toktype
= parse_number
(pstate
, tokstart
, p
- tokstart
,
1574 got_dot|got_e|got_d
,
1576 if
(toktype
== ERROR
)
1577 error (_
("Invalid number \"%.*s\"."), (int) (p
- tokstart
),
1584 last_was_structop
= true
;
1610 if
(!(c
== '_' || c
== '$' || c
==':'
1611 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z')))
1612 /* We must have come across a bad character (e.g. ';'). */
1613 error (_
("Invalid character '%c' in expression."), c
);
1616 for
(c
= tokstart
[namelen
];
1617 (c
== '_' || c
== '$' || c
== ':' ||
(c
>= '0' && c
<= '9')
1618 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z'));
1619 c
= tokstart
[++namelen
]);
1621 /* The token "if" terminates the expression and is NOT
1622 removed from the input stream. */
1624 if
(namelen
== 2 && tokstart
[0] == 'i' && tokstart
[1] == 'f')
1627 pstate
->lexptr
+= namelen
;
1629 /* Catch specific keywords. */
1631 for
(const auto
&keyword
: f_keywords
)
1632 if
(strlen
(keyword.oper
) == namelen
1633 && ((!keyword.case_sensitive
1634 && strncasecmp
(tokstart
, keyword.oper
, namelen
) == 0)
1635 ||
(keyword.case_sensitive
1636 && strncmp
(tokstart
, keyword.oper
, namelen
) == 0)))
1638 yylval.opcode
= keyword.opcode
;
1639 return keyword.token
;
1642 yylval.sval.ptr
= tokstart
;
1643 yylval.sval.length
= namelen
;
1645 if
(*tokstart
== '$')
1646 return DOLLAR_VARIABLE
;
1648 /* Use token-type TYPENAME for symbols that happen to be defined
1649 currently as names of types; NAME for other symbols.
1650 The caller is not constrained to care about the distinction. */
1652 std
::string tmp
= copy_name
(yylval.sval
);
1653 struct block_symbol result
;
1654 const domain_search_flags lookup_domains
[] =
1656 SEARCH_STRUCT_DOMAIN
,
1658 SEARCH_MODULE_DOMAIN
1662 for
(const auto
&domain
: lookup_domains
)
1664 result
= lookup_symbol
(tmp.c_str
(), pstate
->expression_context_block
,
1666 if
(result.symbol
&& result.symbol
->aclass
() == LOC_TYPEDEF
)
1668 yylval.tsym.type
= result.symbol
->type
();
1677 = language_lookup_primitive_type
(pstate
->language
(),
1678 pstate
->gdbarch
(), tmp.c_str
());
1679 if
(yylval.tsym.type
!= NULL
)
1682 /* This is post the symbol search as symbols can hide intrinsics. Also,
1683 give Fortran intrinsics priority over C symbols. This prevents
1684 non-Fortran symbols from hiding intrinsics, for example abs. */
1685 if
(!result.symbol || result.symbol
->language
() != language_fortran
)
1686 for
(const auto
&intrinsic
: f_intrinsics
)
1688 gdb_assert
(!intrinsic.case_sensitive
);
1689 if
(strlen
(intrinsic.oper
) == namelen
1690 && strncasecmp
(tokstart
, intrinsic.oper
, namelen
) == 0)
1692 yylval.opcode
= intrinsic.opcode
;
1693 return intrinsic.token
;
1697 /* Input names that aren't symbols but ARE valid hex numbers,
1698 when the input radix permits them, can be names or numbers
1699 depending on the parse. Note we support radixes > 16 here. */
1701 && ((tokstart
[0] >= 'a' && tokstart
[0] < 'a' + input_radix
- 10)
1702 ||
(tokstart
[0] >= 'A' && tokstart
[0] < 'A' + input_radix
- 10)))
1704 YYSTYPE newlval
; /* Its value is ignored. */
1705 hextype
= parse_number
(pstate
, tokstart
, namelen
, 0, &newlval
);
1708 yylval.ssym.sym
= result
;
1709 yylval.ssym.is_a_field_of_this
= false
;
1714 if
(pstate
->parse_completion
&& *pstate
->lexptr
== '\0')
1715 saw_name_at_eof
= true
;
1717 /* Any other kind of symbol */
1718 yylval.ssym.sym
= result
;
1719 yylval.ssym.is_a_field_of_this
= false
;
1725 f_language::parser
(struct parser_state
*par_state
) const
1727 /* Setting up the parser state. */
1728 scoped_restore pstate_restore
= make_scoped_restore
(&pstate
);
1729 scoped_restore restore_yydebug
= make_scoped_restore
(&yydebug,
1731 gdb_assert
(par_state
!= NULL
);
1733 last_was_structop
= false
;
1734 saw_name_at_eof
= false
;
1737 struct type_stack stack
;
1738 scoped_restore restore_type_stack
= make_scoped_restore
(&type_stack
,
1741 int result
= yyparse ();
1743 pstate
->set_operation
(pstate
->pop
());
1748 yyerror (const char *msg
)
1750 pstate
->parse_error
(msg
);