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