Do not build gdbserver with -Werror by default if development=false
[deliverable/binutils-gdb.git] / gdb / m2-exp.y
CommitLineData
c906108c 1/* YACC grammar for Modula-2 expressions, for GDB.
ecd75fc8 2 Copyright (C) 1986-2014 Free Software Foundation, Inc.
c906108c
SS
3 Generated from expread.y (now c-exp.y) and contributed by the Department
4 of Computer Science at the State University of New York at Buffalo, 1991.
5
5b1ba0e5 6 This file is part of GDB.
c906108c 7
5b1ba0e5
NS
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
c906108c 12
5b1ba0e5
NS
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
5b1ba0e5
NS
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21/* Parse a Modula-2 expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
29
30 Note that malloc's and realloc's in this file are transformed to
31 xmalloc and xrealloc respectively by the same sed command in the
32 makefile that remaps any other malloc/realloc inserted by the parser
33 generator. Doing this with #defines and trying to control the interaction
34 with include files (<malloc.h> and <stdlib.h> for example) just became
35 too messy, particularly when such includes can be inserted at random
025bb325 36 times by the parser generator. */
c906108c
SS
37
38%{
39
40#include "defs.h"
0e9f083f 41#include <string.h>
c906108c
SS
42#include "expression.h"
43#include "language.h"
44#include "value.h"
45#include "parser-defs.h"
46#include "m2-lang.h"
47#include "bfd.h" /* Required by objfiles.h. */
48#include "symfile.h" /* Required by objfiles.h. */
49#include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
fe898f56 50#include "block.h"
c906108c 51
410a0ff2
SDJ
52#define parse_type(ps) builtin_type (parse_gdbarch (ps))
53#define parse_m2_type(ps) builtin_m2_type (parse_gdbarch (ps))
3e79cecf 54
c906108c
SS
55/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
56 as well as gratuitiously global symbol names, so we can have multiple
57 yacc generated parsers in gdb. Note that these are only the variables
58 produced by yacc. If other parser generators (bison, byacc, etc) produce
59 additional global names that conflict at link time, then those parser
025bb325 60 generators need to be fixed instead of adding those names to this list. */
c906108c
SS
61
62#define yymaxdepth m2_maxdepth
410a0ff2 63#define yyparse m2_parse_internal
c906108c
SS
64#define yylex m2_lex
65#define yyerror m2_error
66#define yylval m2_lval
67#define yychar m2_char
68#define yydebug m2_debug
69#define yypact m2_pact
70#define yyr1 m2_r1
71#define yyr2 m2_r2
72#define yydef m2_def
73#define yychk m2_chk
74#define yypgo m2_pgo
75#define yyact m2_act
76#define yyexca m2_exca
77#define yyerrflag m2_errflag
78#define yynerrs m2_nerrs
79#define yyps m2_ps
80#define yypv m2_pv
81#define yys m2_s
82#define yy_yys m2_yys
83#define yystate m2_state
84#define yytmp m2_tmp
85#define yyv m2_v
86#define yy_yyv m2_yyv
87#define yyval m2_val
88#define yylloc m2_lloc
89#define yyreds m2_reds /* With YYDEBUG defined */
90#define yytoks m2_toks /* With YYDEBUG defined */
06891d83
JT
91#define yyname m2_name /* With YYDEBUG defined */
92#define yyrule m2_rule /* With YYDEBUG defined */
c906108c
SS
93#define yylhs m2_yylhs
94#define yylen m2_yylen
95#define yydefred m2_yydefred
96#define yydgoto m2_yydgoto
97#define yysindex m2_yysindex
98#define yyrindex m2_yyrindex
99#define yygindex m2_yygindex
100#define yytable m2_yytable
101#define yycheck m2_yycheck
a7aa5b8a
MK
102#define yyss m2_yyss
103#define yysslim m2_yysslim
104#define yyssp m2_yyssp
105#define yystacksize m2_yystacksize
106#define yyvs m2_yyvs
107#define yyvsp m2_yyvsp
c906108c
SS
108
109#ifndef YYDEBUG
f461f5cf 110#define YYDEBUG 1 /* Default to yydebug support */
c906108c
SS
111#endif
112
f461f5cf
PM
113#define YYFPRINTF parser_fprintf
114
410a0ff2
SDJ
115/* The state of the parser, used internally when we are parsing the
116 expression. */
117
118static struct parser_state *pstate = NULL;
119
a14ed312 120int yyparse (void);
c906108c 121
a14ed312 122static int yylex (void);
c906108c 123
a14ed312 124void yyerror (char *);
c906108c 125
a14ed312 126static int parse_number (int);
c906108c 127
025bb325 128/* The sign of the number being parsed. */
c906108c
SS
129static int number_sign = 1;
130
c906108c
SS
131%}
132
133/* Although the yacc "value" of an expression is not used,
134 since the result is stored in the structure being created,
135 other node types do have values. */
136
137%union
138 {
139 LONGEST lval;
140 ULONGEST ulval;
141 DOUBLEST dval;
142 struct symbol *sym;
143 struct type *tval;
144 struct stoken sval;
145 int voidval;
146 struct block *bval;
147 enum exp_opcode opcode;
148 struct internalvar *ivar;
149
150 struct type **tvec;
151 int *ivec;
152 }
153
154%type <voidval> exp type_exp start set
155%type <voidval> variable
156%type <tval> type
157%type <bval> block
158%type <sym> fblock
159
160%token <lval> INT HEX ERROR
161%token <ulval> UINT M2_TRUE M2_FALSE CHAR
162%token <dval> FLOAT
163
164/* Both NAME and TYPENAME tokens represent symbols in the input,
165 and both convey their data as strings.
166 But a TYPENAME is a string that happens to be defined as a typedef
167 or builtin type name (such as int or char)
168 and a NAME is any other symbol.
169
170 Contexts where this distinction is not important can use the
171 nonterminal "name", which matches either NAME or TYPENAME. */
172
173%token <sval> STRING
174%token <sval> NAME BLOCKNAME IDENT VARNAME
175%token <sval> TYPENAME
176
177%token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
844781a1 178%token TSIZE
c906108c
SS
179%token INC DEC INCL EXCL
180
181/* The GDB scope operator */
182%token COLONCOLON
183
184%token <voidval> INTERNAL_VAR
185
186/* M2 tokens */
187%left ','
188%left ABOVE_COMMA
189%nonassoc ASSIGN
190%left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
191%left OROR
192%left LOGICAL_AND '&'
193%left '@'
194%left '+' '-'
195%left '*' '/' DIV MOD
196%right UNARY
197%right '^' DOT '[' '('
198%right NOT '~'
199%left COLONCOLON QID
200/* This is not an actual token ; it is used for precedence.
201%right QID
202*/
203
204\f
205%%
206
207start : exp
208 | type_exp
209 ;
210
211type_exp: type
410a0ff2
SDJ
212 { write_exp_elt_opcode (pstate, OP_TYPE);
213 write_exp_elt_type (pstate, $1);
214 write_exp_elt_opcode (pstate, OP_TYPE);
c906108c
SS
215 }
216 ;
217
218/* Expressions */
219
220exp : exp '^' %prec UNARY
410a0ff2 221 { write_exp_elt_opcode (pstate, UNOP_IND); }
ef944135 222 ;
c906108c
SS
223
224exp : '-'
225 { number_sign = -1; }
226 exp %prec UNARY
227 { number_sign = 1;
410a0ff2 228 write_exp_elt_opcode (pstate, UNOP_NEG); }
c906108c
SS
229 ;
230
231exp : '+' exp %prec UNARY
410a0ff2 232 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
c906108c
SS
233 ;
234
235exp : not_exp exp %prec UNARY
410a0ff2 236 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
c906108c
SS
237 ;
238
239not_exp : NOT
240 | '~'
241 ;
242
243exp : CAP '(' exp ')'
410a0ff2 244 { write_exp_elt_opcode (pstate, UNOP_CAP); }
c906108c
SS
245 ;
246
247exp : ORD '(' exp ')'
410a0ff2 248 { write_exp_elt_opcode (pstate, UNOP_ORD); }
c906108c
SS
249 ;
250
251exp : ABS '(' exp ')'
410a0ff2 252 { write_exp_elt_opcode (pstate, UNOP_ABS); }
c906108c
SS
253 ;
254
255exp : HIGH '(' exp ')'
410a0ff2 256 { write_exp_elt_opcode (pstate, UNOP_HIGH); }
c906108c
SS
257 ;
258
259exp : MIN_FUNC '(' type ')'
410a0ff2
SDJ
260 { write_exp_elt_opcode (pstate, UNOP_MIN);
261 write_exp_elt_type (pstate, $3);
262 write_exp_elt_opcode (pstate, UNOP_MIN); }
c906108c
SS
263 ;
264
265exp : MAX_FUNC '(' type ')'
410a0ff2
SDJ
266 { write_exp_elt_opcode (pstate, UNOP_MAX);
267 write_exp_elt_type (pstate, $3);
268 write_exp_elt_opcode (pstate, UNOP_MAX); }
c906108c
SS
269 ;
270
271exp : FLOAT_FUNC '(' exp ')'
410a0ff2 272 { write_exp_elt_opcode (pstate, UNOP_FLOAT); }
c906108c
SS
273 ;
274
275exp : VAL '(' type ',' exp ')'
410a0ff2
SDJ
276 { write_exp_elt_opcode (pstate, BINOP_VAL);
277 write_exp_elt_type (pstate, $3);
278 write_exp_elt_opcode (pstate, BINOP_VAL); }
c906108c
SS
279 ;
280
281exp : CHR '(' exp ')'
410a0ff2 282 { write_exp_elt_opcode (pstate, UNOP_CHR); }
c906108c
SS
283 ;
284
285exp : ODD '(' exp ')'
410a0ff2 286 { write_exp_elt_opcode (pstate, UNOP_ODD); }
c906108c
SS
287 ;
288
289exp : TRUNC '(' exp ')'
410a0ff2 290 { write_exp_elt_opcode (pstate, UNOP_TRUNC); }
c906108c
SS
291 ;
292
844781a1 293exp : TSIZE '(' exp ')'
410a0ff2 294 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
844781a1
GM
295 ;
296
c906108c 297exp : SIZE exp %prec UNARY
410a0ff2 298 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
c906108c
SS
299 ;
300
301
302exp : INC '(' exp ')'
410a0ff2 303 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
c906108c
SS
304 ;
305
306exp : INC '(' exp ',' exp ')'
410a0ff2
SDJ
307 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
308 write_exp_elt_opcode (pstate, BINOP_ADD);
309 write_exp_elt_opcode (pstate,
310 BINOP_ASSIGN_MODIFY); }
c906108c
SS
311 ;
312
313exp : DEC '(' exp ')'
410a0ff2 314 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT);}
c906108c
SS
315 ;
316
317exp : DEC '(' exp ',' exp ')'
410a0ff2
SDJ
318 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
319 write_exp_elt_opcode (pstate, BINOP_SUB);
320 write_exp_elt_opcode (pstate,
321 BINOP_ASSIGN_MODIFY); }
c906108c
SS
322 ;
323
324exp : exp DOT NAME
410a0ff2
SDJ
325 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
326 write_exp_string (pstate, $3);
327 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
c906108c
SS
328 ;
329
330exp : set
331 ;
332
333exp : exp IN set
001083c6 334 { error (_("Sets are not implemented."));}
c906108c
SS
335 ;
336
337exp : INCL '(' exp ',' exp ')'
001083c6 338 { error (_("Sets are not implemented."));}
c906108c
SS
339 ;
340
341exp : EXCL '(' exp ',' exp ')'
001083c6 342 { error (_("Sets are not implemented."));}
ef944135 343 ;
c906108c
SS
344
345set : '{' arglist '}'
001083c6 346 { error (_("Sets are not implemented."));}
c906108c 347 | type '{' arglist '}'
001083c6 348 { error (_("Sets are not implemented."));}
c906108c
SS
349 ;
350
351
352/* Modula-2 array subscript notation [a,b,c...] */
353exp : exp '['
354 /* This function just saves the number of arguments
355 that follow in the list. It is *not* specific to
356 function types */
357 { start_arglist(); }
358 non_empty_arglist ']' %prec DOT
410a0ff2
SDJ
359 { write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
360 write_exp_elt_longcst (pstate,
361 (LONGEST) end_arglist());
362 write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT); }
c906108c
SS
363 ;
364
844781a1 365exp : exp '[' exp ']'
410a0ff2 366 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
844781a1
GM
367 ;
368
c906108c
SS
369exp : exp '('
370 /* This is to save the value of arglist_len
371 being accumulated by an outer function call. */
372 { start_arglist (); }
373 arglist ')' %prec DOT
410a0ff2
SDJ
374 { write_exp_elt_opcode (pstate, OP_FUNCALL);
375 write_exp_elt_longcst (pstate,
376 (LONGEST) end_arglist ());
377 write_exp_elt_opcode (pstate, OP_FUNCALL); }
c906108c
SS
378 ;
379
380arglist :
381 ;
382
383arglist : exp
384 { arglist_len = 1; }
385 ;
386
387arglist : arglist ',' exp %prec ABOVE_COMMA
388 { arglist_len++; }
389 ;
390
391non_empty_arglist
392 : exp
393 { arglist_len = 1; }
394 ;
395
396non_empty_arglist
397 : non_empty_arglist ',' exp %prec ABOVE_COMMA
398 { arglist_len++; }
399 ;
400
401/* GDB construct */
402exp : '{' type '}' exp %prec UNARY
410a0ff2
SDJ
403 { write_exp_elt_opcode (pstate, UNOP_MEMVAL);
404 write_exp_elt_type (pstate, $2);
405 write_exp_elt_opcode (pstate, UNOP_MEMVAL); }
c906108c
SS
406 ;
407
408exp : type '(' exp ')' %prec UNARY
410a0ff2
SDJ
409 { write_exp_elt_opcode (pstate, UNOP_CAST);
410 write_exp_elt_type (pstate, $1);
411 write_exp_elt_opcode (pstate, UNOP_CAST); }
c906108c
SS
412 ;
413
414exp : '(' exp ')'
415 { }
416 ;
417
418/* Binary operators in order of decreasing precedence. Note that some
419 of these operators are overloaded! (ie. sets) */
420
421/* GDB construct */
422exp : exp '@' exp
410a0ff2 423 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
c906108c
SS
424 ;
425
426exp : exp '*' exp
410a0ff2 427 { write_exp_elt_opcode (pstate, BINOP_MUL); }
c906108c
SS
428 ;
429
430exp : exp '/' exp
410a0ff2 431 { write_exp_elt_opcode (pstate, BINOP_DIV); }
c906108c
SS
432 ;
433
434exp : exp DIV exp
410a0ff2 435 { write_exp_elt_opcode (pstate, BINOP_INTDIV); }
c906108c
SS
436 ;
437
438exp : exp MOD exp
410a0ff2 439 { write_exp_elt_opcode (pstate, BINOP_REM); }
c906108c
SS
440 ;
441
442exp : exp '+' exp
410a0ff2 443 { write_exp_elt_opcode (pstate, BINOP_ADD); }
c906108c
SS
444 ;
445
446exp : exp '-' exp
410a0ff2 447 { write_exp_elt_opcode (pstate, BINOP_SUB); }
c906108c
SS
448 ;
449
450exp : exp '=' exp
410a0ff2 451 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
c906108c
SS
452 ;
453
454exp : exp NOTEQUAL exp
410a0ff2 455 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
c906108c 456 | exp '#' exp
410a0ff2 457 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
c906108c
SS
458 ;
459
460exp : exp LEQ exp
410a0ff2 461 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
c906108c
SS
462 ;
463
464exp : exp GEQ exp
410a0ff2 465 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
c906108c
SS
466 ;
467
468exp : exp '<' exp
410a0ff2 469 { write_exp_elt_opcode (pstate, BINOP_LESS); }
c906108c
SS
470 ;
471
472exp : exp '>' exp
410a0ff2 473 { write_exp_elt_opcode (pstate, BINOP_GTR); }
c906108c
SS
474 ;
475
476exp : exp LOGICAL_AND exp
410a0ff2 477 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
c906108c
SS
478 ;
479
480exp : exp OROR exp
410a0ff2 481 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
c906108c
SS
482 ;
483
484exp : exp ASSIGN exp
410a0ff2 485 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
c906108c
SS
486 ;
487
488
489/* Constants */
490
491exp : M2_TRUE
410a0ff2
SDJ
492 { write_exp_elt_opcode (pstate, OP_BOOL);
493 write_exp_elt_longcst (pstate, (LONGEST) $1);
494 write_exp_elt_opcode (pstate, OP_BOOL); }
c906108c
SS
495 ;
496
497exp : M2_FALSE
410a0ff2
SDJ
498 { write_exp_elt_opcode (pstate, OP_BOOL);
499 write_exp_elt_longcst (pstate, (LONGEST) $1);
500 write_exp_elt_opcode (pstate, OP_BOOL); }
c906108c
SS
501 ;
502
503exp : INT
410a0ff2
SDJ
504 { write_exp_elt_opcode (pstate, OP_LONG);
505 write_exp_elt_type (pstate,
506 parse_m2_type (pstate)->builtin_int);
507 write_exp_elt_longcst (pstate, (LONGEST) $1);
508 write_exp_elt_opcode (pstate, OP_LONG); }
c906108c
SS
509 ;
510
511exp : UINT
512 {
410a0ff2
SDJ
513 write_exp_elt_opcode (pstate, OP_LONG);
514 write_exp_elt_type (pstate,
515 parse_m2_type (pstate)
516 ->builtin_card);
517 write_exp_elt_longcst (pstate, (LONGEST) $1);
518 write_exp_elt_opcode (pstate, OP_LONG);
c906108c
SS
519 }
520 ;
521
522exp : CHAR
410a0ff2
SDJ
523 { write_exp_elt_opcode (pstate, OP_LONG);
524 write_exp_elt_type (pstate,
525 parse_m2_type (pstate)
526 ->builtin_char);
527 write_exp_elt_longcst (pstate, (LONGEST) $1);
528 write_exp_elt_opcode (pstate, OP_LONG); }
c906108c
SS
529 ;
530
531
532exp : FLOAT
410a0ff2
SDJ
533 { write_exp_elt_opcode (pstate, OP_DOUBLE);
534 write_exp_elt_type (pstate,
535 parse_m2_type (pstate)
536 ->builtin_real);
537 write_exp_elt_dblcst (pstate, $1);
538 write_exp_elt_opcode (pstate, OP_DOUBLE); }
c906108c
SS
539 ;
540
541exp : variable
542 ;
543
544exp : SIZE '(' type ')' %prec UNARY
410a0ff2
SDJ
545 { write_exp_elt_opcode (pstate, OP_LONG);
546 write_exp_elt_type (pstate,
547 parse_type (pstate)->builtin_int);
548 write_exp_elt_longcst (pstate,
549 (LONGEST) TYPE_LENGTH ($3));
550 write_exp_elt_opcode (pstate, OP_LONG); }
c906108c
SS
551 ;
552
553exp : STRING
410a0ff2
SDJ
554 { write_exp_elt_opcode (pstate, OP_M2_STRING);
555 write_exp_string (pstate, $1);
556 write_exp_elt_opcode (pstate, OP_M2_STRING); }
c906108c
SS
557 ;
558
025bb325 559/* This will be used for extensions later. Like adding modules. */
c906108c
SS
560block : fblock
561 { $$ = SYMBOL_BLOCK_VALUE($1); }
562 ;
563
564fblock : BLOCKNAME
565 { struct symbol *sym
410a0ff2
SDJ
566 = lookup_symbol (copy_name ($1),
567 expression_context_block,
2570f2b7 568 VAR_DOMAIN, 0);
c906108c
SS
569 $$ = sym;}
570 ;
571
572
573/* GDB scope operator */
574fblock : block COLONCOLON BLOCKNAME
575 { struct symbol *tem
576 = lookup_symbol (copy_name ($3), $1,
2570f2b7 577 VAR_DOMAIN, 0);
c906108c 578 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
001083c6 579 error (_("No function \"%s\" in specified context."),
c906108c
SS
580 copy_name ($3));
581 $$ = tem;
582 }
583 ;
584
585/* Useful for assigning to PROCEDURE variables */
586variable: fblock
410a0ff2
SDJ
587 { write_exp_elt_opcode (pstate, OP_VAR_VALUE);
588 write_exp_elt_block (pstate, NULL);
589 write_exp_elt_sym (pstate, $1);
590 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
c906108c
SS
591 ;
592
593/* GDB internal ($foo) variable */
594variable: INTERNAL_VAR
595 ;
596
597/* GDB scope operator */
598variable: block COLONCOLON NAME
599 { struct symbol *sym;
600 sym = lookup_symbol (copy_name ($3), $1,
2570f2b7 601 VAR_DOMAIN, 0);
c906108c 602 if (sym == 0)
001083c6 603 error (_("No symbol \"%s\" in specified context."),
c906108c 604 copy_name ($3));
72384ba3
PH
605 if (symbol_read_needs_frame (sym))
606 {
607 if (innermost_block == 0
608 || contained_in (block_found,
609 innermost_block))
610 innermost_block = block_found;
611 }
c906108c 612
410a0ff2 613 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
c906108c 614 /* block_found is set by lookup_symbol. */
410a0ff2
SDJ
615 write_exp_elt_block (pstate, block_found);
616 write_exp_elt_sym (pstate, sym);
617 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
c906108c
SS
618 ;
619
025bb325 620/* Base case for variables. */
c906108c
SS
621variable: NAME
622 { struct symbol *sym;
1993b719 623 struct field_of_this_result is_a_field_of_this;
c906108c
SS
624
625 sym = lookup_symbol (copy_name ($1),
626 expression_context_block,
176620f1 627 VAR_DOMAIN,
2570f2b7 628 &is_a_field_of_this);
c906108c
SS
629 if (sym)
630 {
631 if (symbol_read_needs_frame (sym))
632 {
633 if (innermost_block == 0 ||
634 contained_in (block_found,
635 innermost_block))
636 innermost_block = block_found;
637 }
638
410a0ff2 639 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
c906108c
SS
640 /* We want to use the selected frame, not
641 another more inner frame which happens to
642 be in the same block. */
410a0ff2
SDJ
643 write_exp_elt_block (pstate, NULL);
644 write_exp_elt_sym (pstate, sym);
645 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
c906108c
SS
646 }
647 else
648 {
7c7b6655 649 struct bound_minimal_symbol msymbol;
710122da 650 char *arg = copy_name ($1);
c906108c
SS
651
652 msymbol =
7c7b6655
TT
653 lookup_bound_minimal_symbol (arg);
654 if (msymbol.minsym != NULL)
410a0ff2 655 write_exp_msymbol (pstate, msymbol);
c906108c 656 else if (!have_full_symbols () && !have_partial_symbols ())
001083c6 657 error (_("No symbol table is loaded. Use the \"symbol-file\" command."));
c906108c 658 else
001083c6 659 error (_("No symbol \"%s\" in current context."),
c906108c
SS
660 copy_name ($1));
661 }
662 }
663 ;
664
665type
666 : TYPENAME
410a0ff2
SDJ
667 { $$ = lookup_typename (parse_language (pstate),
668 parse_gdbarch (pstate),
e6c014f2 669 copy_name ($1),
c906108c
SS
670 expression_context_block, 0); }
671
672 ;
673
674%%
675
c906108c
SS
676/* Take care of parsing a number (anything that starts with a digit).
677 Set yylval and return the token type; update lexptr.
678 LEN is the number of characters in it. */
679
680/*** Needs some error checking for the float case ***/
681
682static int
d04550a6 683parse_number (int olen)
c906108c 684{
d7561cbb 685 const char *p = lexptr;
710122da
DC
686 LONGEST n = 0;
687 LONGEST prevn = 0;
688 int c,i,ischar=0;
689 int base = input_radix;
690 int len = olen;
c906108c
SS
691 int unsigned_p = number_sign == 1 ? 1 : 0;
692
693 if(p[len-1] == 'H')
694 {
695 base = 16;
696 len--;
697 }
698 else if(p[len-1] == 'C' || p[len-1] == 'B')
699 {
700 base = 8;
701 ischar = p[len-1] == 'C';
702 len--;
703 }
704
705 /* Scan the number */
706 for (c = 0; c < len; c++)
707 {
708 if (p[c] == '.' && base == 10)
709 {
710 /* It's a float since it contains a point. */
711 yylval.dval = atof (p);
712 lexptr += len;
713 return FLOAT;
714 }
715 if (p[c] == '.' && base != 10)
001083c6 716 error (_("Floating point numbers must be base 10."));
c906108c 717 if (base == 10 && (p[c] < '0' || p[c] > '9'))
001083c6 718 error (_("Invalid digit \'%c\' in number."),p[c]);
c906108c
SS
719 }
720
721 while (len-- > 0)
722 {
723 c = *p++;
724 n *= base;
725 if( base == 8 && (c == '8' || c == '9'))
001083c6 726 error (_("Invalid digit \'%c\' in octal number."),c);
c906108c
SS
727 if (c >= '0' && c <= '9')
728 i = c - '0';
729 else
730 {
731 if (base == 16 && c >= 'A' && c <= 'F')
732 i = c - 'A' + 10;
733 else
734 return ERROR;
735 }
736 n+=i;
737 if(i >= base)
738 return ERROR;
739 if(!unsigned_p && number_sign == 1 && (prevn >= n))
740 unsigned_p=1; /* Try something unsigned */
741 /* Don't do the range check if n==i and i==0, since that special
025bb325 742 case will give an overflow error. */
c906108c
SS
743 if(RANGE_CHECK && n!=i && i)
744 {
745 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
746 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
001083c6 747 range_error (_("Overflow on numeric constant."));
c906108c
SS
748 }
749 prevn=n;
750 }
751
752 lexptr = p;
753 if(*p == 'B' || *p == 'C' || *p == 'H')
754 lexptr++; /* Advance past B,C or H */
755
756 if (ischar)
757 {
758 yylval.ulval = n;
759 return CHAR;
760 }
761 else if ( unsigned_p && number_sign == 1)
762 {
763 yylval.ulval = n;
764 return UINT;
765 }
766 else if((unsigned_p && (n<0))) {
001083c6 767 range_error (_("Overflow on numeric constant -- number too large."));
c906108c
SS
768 /* But, this can return if range_check == range_warn. */
769 }
770 yylval.lval = n;
771 return INT;
772}
773
774
775/* Some tokens */
776
777static struct
778{
779 char name[2];
780 int token;
781} tokentab2[] =
782{
783 { {'<', '>'}, NOTEQUAL },
784 { {':', '='}, ASSIGN },
785 { {'<', '='}, LEQ },
786 { {'>', '='}, GEQ },
787 { {':', ':'}, COLONCOLON },
788
789};
790
791/* Some specific keywords */
792
793struct keyword {
794 char keyw[10];
795 int token;
796};
797
798static struct keyword keytab[] =
799{
800 {"OR" , OROR },
801 {"IN", IN },/* Note space after IN */
802 {"AND", LOGICAL_AND},
803 {"ABS", ABS },
804 {"CHR", CHR },
805 {"DEC", DEC },
806 {"NOT", NOT },
807 {"DIV", DIV },
808 {"INC", INC },
809 {"MAX", MAX_FUNC },
810 {"MIN", MIN_FUNC },
811 {"MOD", MOD },
812 {"ODD", ODD },
813 {"CAP", CAP },
814 {"ORD", ORD },
815 {"VAL", VAL },
816 {"EXCL", EXCL },
817 {"HIGH", HIGH },
818 {"INCL", INCL },
819 {"SIZE", SIZE },
820 {"FLOAT", FLOAT_FUNC },
821 {"TRUNC", TRUNC },
844781a1 822 {"TSIZE", SIZE },
c906108c
SS
823};
824
825
826/* Read one token, getting characters through lexptr. */
827
410a0ff2
SDJ
828/* This is where we will check to make sure that the language and the
829 operators used are compatible */
c906108c
SS
830
831static int
eeae04df 832yylex (void)
c906108c 833{
710122da
DC
834 int c;
835 int namelen;
836 int i;
d7561cbb 837 const char *tokstart;
710122da 838 char quote;
c906108c
SS
839
840 retry:
841
065432a8
PM
842 prev_lexptr = lexptr;
843
c906108c
SS
844 tokstart = lexptr;
845
846
847 /* See if it is a special token of length 2 */
848 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
1e5e79d0 849 if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
c906108c
SS
850 {
851 lexptr += 2;
852 return tokentab2[i].token;
853 }
854
855 switch (c = *tokstart)
856 {
857 case 0:
858 return 0;
859
860 case ' ':
861 case '\t':
862 case '\n':
863 lexptr++;
864 goto retry;
865
866 case '(':
867 paren_depth++;
868 lexptr++;
869 return c;
870
871 case ')':
872 if (paren_depth == 0)
873 return 0;
874 paren_depth--;
875 lexptr++;
876 return c;
877
878 case ',':
879 if (comma_terminates && paren_depth == 0)
880 return 0;
881 lexptr++;
882 return c;
883
884 case '.':
885 /* Might be a floating point number. */
886 if (lexptr[1] >= '0' && lexptr[1] <= '9')
887 break; /* Falls into number code. */
888 else
889 {
890 lexptr++;
891 return DOT;
892 }
893
894/* These are character tokens that appear as-is in the YACC grammar */
895 case '+':
896 case '-':
897 case '*':
898 case '/':
899 case '^':
900 case '<':
901 case '>':
902 case '[':
903 case ']':
904 case '=':
905 case '{':
906 case '}':
907 case '#':
908 case '@':
909 case '~':
910 case '&':
911 lexptr++;
912 return c;
913
914 case '\'' :
915 case '"':
916 quote = c;
917 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
918 if (c == '\\')
919 {
920 c = tokstart[++namelen];
921 if (c >= '0' && c <= '9')
922 {
923 c = tokstart[++namelen];
924 if (c >= '0' && c <= '9')
925 c = tokstart[++namelen];
926 }
927 }
928 if(c != quote)
001083c6 929 error (_("Unterminated string or character constant."));
c906108c
SS
930 yylval.sval.ptr = tokstart + 1;
931 yylval.sval.length = namelen - 1;
932 lexptr += namelen + 1;
933
934 if(namelen == 2) /* Single character */
935 {
936 yylval.ulval = tokstart[1];
937 return CHAR;
938 }
939 else
940 return STRING;
941 }
942
943 /* Is it a number? */
944 /* Note: We have already dealt with the case of the token '.'.
945 See case '.' above. */
946 if ((c >= '0' && c <= '9'))
947 {
948 /* It's a number. */
949 int got_dot = 0, got_e = 0;
d7561cbb 950 const char *p = tokstart;
c906108c
SS
951 int toktype;
952
953 for (++p ;; ++p)
954 {
955 if (!got_e && (*p == 'e' || *p == 'E'))
956 got_dot = got_e = 1;
957 else if (!got_dot && *p == '.')
958 got_dot = 1;
959 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
960 && (*p == '-' || *p == '+'))
961 /* This is the sign of the exponent, not the end of the
962 number. */
963 continue;
964 else if ((*p < '0' || *p > '9') &&
965 (*p < 'A' || *p > 'F') &&
966 (*p != 'H')) /* Modula-2 hexadecimal number */
967 break;
968 }
969 toktype = parse_number (p - tokstart);
970 if (toktype == ERROR)
971 {
972 char *err_copy = (char *) alloca (p - tokstart + 1);
973
974 memcpy (err_copy, tokstart, p - tokstart);
975 err_copy[p - tokstart] = 0;
001083c6 976 error (_("Invalid number \"%s\"."), err_copy);
c906108c
SS
977 }
978 lexptr = p;
979 return toktype;
980 }
981
982 if (!(c == '_' || c == '$'
983 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
984 /* We must have come across a bad character (e.g. ';'). */
001083c6 985 error (_("Invalid character '%c' in expression."), c);
c906108c
SS
986
987 /* It's a name. See how long it is. */
988 namelen = 0;
989 for (c = tokstart[namelen];
990 (c == '_' || c == '$' || (c >= '0' && c <= '9')
991 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
992 c = tokstart[++namelen])
993 ;
994
995 /* The token "if" terminates the expression and is NOT
996 removed from the input stream. */
997 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
998 {
999 return 0;
1000 }
1001
1002 lexptr += namelen;
1003
1004 /* Lookup special keywords */
1005 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
1e5e79d0
MD
1006 if (namelen == strlen (keytab[i].keyw)
1007 && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
c906108c
SS
1008 return keytab[i].token;
1009
1010 yylval.sval.ptr = tokstart;
1011 yylval.sval.length = namelen;
1012
1013 if (*tokstart == '$')
1014 {
410a0ff2 1015 write_dollar_variable (pstate, yylval.sval);
c906108c
SS
1016 return INTERNAL_VAR;
1017 }
1018
1019 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1020 functions. If this is not so, then ...
1021 Use token-type TYPENAME for symbols that happen to be defined
1022 currently as names of types; NAME for other symbols.
1023 The caller is not constrained to care about the distinction. */
1024 {
1025
1026
1027 char *tmp = copy_name (yylval.sval);
1028 struct symbol *sym;
1029
ccefe4c4 1030 if (lookup_symtab (tmp))
c906108c 1031 return BLOCKNAME;
2570f2b7 1032 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 0);
c906108c
SS
1033 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1034 return BLOCKNAME;
410a0ff2
SDJ
1035 if (lookup_typename (parse_language (pstate), parse_gdbarch (pstate),
1036 copy_name (yylval.sval),
1037 expression_context_block, 1))
c906108c
SS
1038 return TYPENAME;
1039
1040 if(sym)
1041 {
712f90be 1042 switch(SYMBOL_CLASS (sym))
c906108c
SS
1043 {
1044 case LOC_STATIC:
1045 case LOC_REGISTER:
1046 case LOC_ARG:
1047 case LOC_REF_ARG:
c906108c
SS
1048 case LOC_REGPARM_ADDR:
1049 case LOC_LOCAL:
c906108c
SS
1050 case LOC_CONST:
1051 case LOC_CONST_BYTES:
1052 case LOC_OPTIMIZED_OUT:
4c2df51b 1053 case LOC_COMPUTED:
c906108c
SS
1054 return NAME;
1055
1056 case LOC_TYPEDEF:
1057 return TYPENAME;
1058
1059 case LOC_BLOCK:
1060 return BLOCKNAME;
1061
1062 case LOC_UNDEF:
001083c6 1063 error (_("internal: Undefined class in m2lex()"));
c906108c
SS
1064
1065 case LOC_LABEL:
1066 case LOC_UNRESOLVED:
001083c6 1067 error (_("internal: Unforseen case in m2lex()"));
c4093a6a
JM
1068
1069 default:
001083c6 1070 error (_("unhandled token in m2lex()"));
c4093a6a 1071 break;
c906108c
SS
1072 }
1073 }
1074 else
1075 {
025bb325 1076 /* Built-in BOOLEAN type. This is sort of a hack. */
1e5e79d0 1077 if (strncmp (tokstart, "TRUE", 4) == 0)
c906108c
SS
1078 {
1079 yylval.ulval = 1;
1080 return M2_TRUE;
1081 }
1e5e79d0 1082 else if (strncmp (tokstart, "FALSE", 5) == 0)
c906108c
SS
1083 {
1084 yylval.ulval = 0;
1085 return M2_FALSE;
1086 }
1087 }
1088
025bb325 1089 /* Must be another type of name... */
c906108c
SS
1090 return NAME;
1091 }
1092}
1093
410a0ff2
SDJ
1094int
1095m2_parse (struct parser_state *par_state)
1096{
1097 int result;
1098 struct cleanup *c = make_cleanup_clear_parser_state (&pstate);
1099
1100 /* Setting up the parser state. */
1101 gdb_assert (par_state != NULL);
1102 pstate = par_state;
1103
1104 result = yyparse ();
1105 do_cleanups (c);
1106
1107 return result;
1108}
1109
c906108c 1110void
d04550a6 1111yyerror (char *msg)
c906108c 1112{
065432a8
PM
1113 if (prev_lexptr)
1114 lexptr = prev_lexptr;
1115
001083c6 1116 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
c906108c 1117}
This page took 1.201518 seconds and 4 git commands to generate.