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