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