1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986-2021 Free Software Foundation, Inc.
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.
6 This file is part of GDB.
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.
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.
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/>. */
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.
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
36 times by the parser generator. */
41 #include "expression.h"
44 #include "parser-defs.h"
46 #include "bfd.h" /* Required by objfiles.h. */
47 #include "symfile.h" /* Required by objfiles.h. */
48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
52 #define parse_type(ps) builtin_type (ps->gdbarch ())
53 #define parse_m2_type(ps) builtin_m2_type (ps->gdbarch ())
55 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
57 #define GDB_YY_REMAP_PREFIX m2_
60 /* The state of the parser, used internally when we are parsing the
63 static struct parser_state *pstate = NULL;
67 static int yylex (void);
69 static void yyerror (const char *);
71 static int parse_number (int);
73 /* The sign of the number being parsed. */
74 static int number_sign = 1;
79 /* Although the yacc "value" of an expression is not used,
80 since the result is stored in the structure being created,
81 other node types do have values. */
92 const struct block *bval;
93 enum exp_opcode opcode;
94 struct internalvar *ivar;
100 %type <voidval> exp type_exp start set
101 %type <voidval> variable
106 %token <lval> INT HEX ERROR
107 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
110 /* Both NAME and TYPENAME tokens represent symbols in the input,
111 and both convey their data as strings.
112 But a TYPENAME is a string that happens to be defined as a typedef
113 or builtin type name (such as int or char)
114 and a NAME is any other symbol.
116 Contexts where this distinction is not important can use the
117 nonterminal "name", which matches either NAME or TYPENAME. */
120 %token <sval> NAME BLOCKNAME IDENT VARNAME
121 %token <sval> TYPENAME
123 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
125 %token INC DEC INCL EXCL
127 /* The GDB scope operator */
130 %token <sval> DOLLAR_VARIABLE
136 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
138 %left LOGICAL_AND '&'
141 %left '*' '/' DIV MOD
143 %right '^' DOT '[' '('
146 /* This is not an actual token ; it is used for precedence.
158 { pstate->push_new<type_operation> ($1); }
163 exp : exp '^' %prec UNARY
164 { pstate->wrap<unop_ind_operation> (); }
168 { number_sign = -1; }
171 pstate->wrap<unary_neg_operation> (); }
174 exp : '+' exp %prec UNARY
175 { pstate->wrap<unary_plus_operation> (); }
178 exp : not_exp exp %prec UNARY
179 { pstate->wrap<unary_logical_not_operation> (); }
186 exp : CAP '(' exp ')'
187 { error (_("CAP function is not implemented")); }
190 exp : ORD '(' exp ')'
191 { error (_("ORD function is not implemented")); }
194 exp : ABS '(' exp ')'
195 { error (_("ABS function is not implemented")); }
198 exp : HIGH '(' exp ')'
199 { pstate->wrap<m2_unop_high_operation> (); }
202 exp : MIN_FUNC '(' type ')'
203 { error (_("MIN function is not implemented")); }
206 exp : MAX_FUNC '(' type ')'
207 { error (_("MAX function is not implemented")); }
210 exp : FLOAT_FUNC '(' exp ')'
211 { error (_("FLOAT function is not implemented")); }
214 exp : VAL '(' type ',' exp ')'
215 { error (_("VAL function is not implemented")); }
218 exp : CHR '(' exp ')'
219 { error (_("CHR function is not implemented")); }
222 exp : ODD '(' exp ')'
223 { error (_("ODD function is not implemented")); }
226 exp : TRUNC '(' exp ')'
227 { error (_("TRUNC function is not implemented")); }
230 exp : TSIZE '(' exp ')'
231 { pstate->wrap<unop_sizeof_operation> (); }
234 exp : SIZE exp %prec UNARY
235 { pstate->wrap<unop_sizeof_operation> (); }
239 exp : INC '(' exp ')'
240 { pstate->wrap<preinc_operation> (); }
243 exp : INC '(' exp ',' exp ')'
245 operation_up rhs = pstate->pop ();
246 operation_up lhs = pstate->pop ();
247 pstate->push_new<assign_modify_operation>
248 (BINOP_ADD, std::move (lhs), std::move (rhs));
252 exp : DEC '(' exp ')'
253 { pstate->wrap<predec_operation> (); }
256 exp : DEC '(' exp ',' exp ')'
258 operation_up rhs = pstate->pop ();
259 operation_up lhs = pstate->pop ();
260 pstate->push_new<assign_modify_operation>
261 (BINOP_SUB, std::move (lhs), std::move (rhs));
267 pstate->push_new<structop_operation>
268 (pstate->pop (), copy_name ($3));
276 { error (_("Sets are not implemented."));}
279 exp : INCL '(' exp ',' exp ')'
280 { error (_("Sets are not implemented."));}
283 exp : EXCL '(' exp ',' exp ')'
284 { error (_("Sets are not implemented."));}
287 set : '{' arglist '}'
288 { error (_("Sets are not implemented."));}
289 | type '{' arglist '}'
290 { error (_("Sets are not implemented."));}
294 /* Modula-2 array subscript notation [a,b,c...]. */
296 /* This function just saves the number of arguments
297 that follow in the list. It is *not* specific to
299 { pstate->start_arglist(); }
300 non_empty_arglist ']' %prec DOT
302 gdb_assert (pstate->arglist_len > 0);
303 std::vector<operation_up> args
304 = pstate->pop_vector (pstate->end_arglist ());
305 pstate->push_new<multi_subscript_operation>
306 (pstate->pop (), std::move (args));
311 /* This is to save the value of arglist_len
312 being accumulated by an outer function call. */
313 { pstate->start_arglist (); }
314 arglist ')' %prec DOT
316 std::vector<operation_up> args
317 = pstate->pop_vector (pstate->end_arglist ());
318 pstate->push_new<funcall_operation>
319 (pstate->pop (), std::move (args));
327 { pstate->arglist_len = 1; }
330 arglist : arglist ',' exp %prec ABOVE_COMMA
331 { pstate->arglist_len++; }
336 { pstate->arglist_len = 1; }
340 : non_empty_arglist ',' exp %prec ABOVE_COMMA
341 { pstate->arglist_len++; }
345 exp : '{' type '}' exp %prec UNARY
347 pstate->push_new<unop_memval_operation>
348 (pstate->pop (), $2);
352 exp : type '(' exp ')' %prec UNARY
354 pstate->push_new<unop_cast_operation>
355 (pstate->pop (), $1);
363 /* Binary operators in order of decreasing precedence. Note that some
364 of these operators are overloaded! (ie. sets) */
368 { pstate->wrap2<repeat_operation> (); }
372 { pstate->wrap2<mul_operation> (); }
376 { pstate->wrap2<div_operation> (); }
380 { pstate->wrap2<intdiv_operation> (); }
384 { pstate->wrap2<rem_operation> (); }
388 { pstate->wrap2<add_operation> (); }
392 { pstate->wrap2<sub_operation> (); }
396 { pstate->wrap2<equal_operation> (); }
399 exp : exp NOTEQUAL exp
400 { pstate->wrap2<notequal_operation> (); }
402 { pstate->wrap2<notequal_operation> (); }
406 { pstate->wrap2<leq_operation> (); }
410 { pstate->wrap2<geq_operation> (); }
414 { pstate->wrap2<less_operation> (); }
418 { pstate->wrap2<gtr_operation> (); }
421 exp : exp LOGICAL_AND exp
422 { pstate->wrap2<logical_and_operation> (); }
426 { pstate->wrap2<logical_or_operation> (); }
430 { pstate->wrap2<assign_operation> (); }
437 { pstate->push_new<bool_operation> ($1); }
441 { pstate->push_new<bool_operation> ($1); }
446 pstate->push_new<long_const_operation>
447 (parse_m2_type (pstate)->builtin_int, $1);
453 pstate->push_new<long_const_operation>
454 (parse_m2_type (pstate)->builtin_card, $1);
460 pstate->push_new<long_const_operation>
461 (parse_m2_type (pstate)->builtin_char, $1);
469 std::copy (std::begin ($1), std::end ($1),
471 pstate->push_new<float_const_operation>
472 (parse_m2_type (pstate)->builtin_real, data);
479 exp : SIZE '(' type ')' %prec UNARY
481 pstate->push_new<long_const_operation>
482 (parse_m2_type (pstate)->builtin_int,
488 { error (_("strings are not implemented")); }
491 /* This will be used for extensions later. Like adding modules. */
493 { $$ = SYMBOL_BLOCK_VALUE($1); }
498 = lookup_symbol (copy_name ($1).c_str (),
499 pstate->expression_context_block,
500 VAR_DOMAIN, 0).symbol;
505 /* GDB scope operator */
506 fblock : block COLONCOLON BLOCKNAME
508 = lookup_symbol (copy_name ($3).c_str (), $1,
509 VAR_DOMAIN, 0).symbol;
510 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
511 error (_("No function \"%s\" in specified context."),
512 copy_name ($3).c_str ());
517 /* Useful for assigning to PROCEDURE variables */
520 block_symbol sym { $1, nullptr };
521 pstate->push_new<var_value_operation> (sym);
525 /* GDB internal ($foo) variable */
526 variable: DOLLAR_VARIABLE
527 { pstate->push_dollar ($1); }
530 /* GDB scope operator */
531 variable: block COLONCOLON NAME
532 { struct block_symbol sym
533 = lookup_symbol (copy_name ($3).c_str (), $1,
537 error (_("No symbol \"%s\" in specified context."),
538 copy_name ($3).c_str ());
539 if (symbol_read_needs_frame (sym.symbol))
540 pstate->block_tracker->update (sym);
542 pstate->push_new<var_value_operation> (sym);
546 /* Base case for variables. */
548 { struct block_symbol sym;
549 struct field_of_this_result is_a_field_of_this;
551 std::string name = copy_name ($1);
553 = lookup_symbol (name.c_str (),
554 pstate->expression_context_block,
556 &is_a_field_of_this);
558 pstate->push_symbol (name.c_str (), sym);
565 = lookup_typename (pstate->language (),
566 copy_name ($1).c_str (),
567 pstate->expression_context_block,
575 /* Take care of parsing a number (anything that starts with a digit).
576 Set yylval and return the token type; update lexptr.
577 LEN is the number of characters in it. */
579 /*** Needs some error checking for the float case ***/
582 parse_number (int olen)
584 const char *p = pstate->lexptr;
588 int base = input_radix;
590 int unsigned_p = number_sign == 1 ? 1 : 0;
597 else if(p[len-1] == 'C' || p[len-1] == 'B')
600 ischar = p[len-1] == 'C';
604 /* Scan the number */
605 for (c = 0; c < len; c++)
607 if (p[c] == '.' && base == 10)
609 /* It's a float since it contains a point. */
610 if (!parse_float (p, len,
611 parse_m2_type (pstate)->builtin_real,
615 pstate->lexptr += len;
618 if (p[c] == '.' && base != 10)
619 error (_("Floating point numbers must be base 10."));
620 if (base == 10 && (p[c] < '0' || p[c] > '9'))
621 error (_("Invalid digit \'%c\' in number."),p[c]);
628 if( base == 8 && (c == '8' || c == '9'))
629 error (_("Invalid digit \'%c\' in octal number."),c);
630 if (c >= '0' && c <= '9')
634 if (base == 16 && c >= 'A' && c <= 'F')
642 if(!unsigned_p && number_sign == 1 && (prevn >= n))
643 unsigned_p=1; /* Try something unsigned */
644 /* Don't do the range check if n==i and i==0, since that special
645 case will give an overflow error. */
646 if(RANGE_CHECK && n!=i && i)
648 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
649 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
650 range_error (_("Overflow on numeric constant."));
656 if(*p == 'B' || *p == 'C' || *p == 'H')
657 pstate->lexptr++; /* Advance past B,C or H */
664 else if ( unsigned_p && number_sign == 1)
669 else if((unsigned_p && (n<0))) {
670 range_error (_("Overflow on numeric constant -- number too large."));
671 /* But, this can return if range_check == range_warn. */
686 { {'<', '>'}, NOTEQUAL },
687 { {':', '='}, ASSIGN },
690 { {':', ':'}, COLONCOLON },
694 /* Some specific keywords */
701 static struct keyword keytab[] =
704 {"IN", IN },/* Note space after IN */
705 {"AND", LOGICAL_AND},
723 {"FLOAT", FLOAT_FUNC },
729 /* Depth of parentheses. */
730 static int paren_depth;
732 /* Read one token, getting characters through lexptr. */
734 /* This is where we will check to make sure that the language and the
735 operators used are compatible */
743 const char *tokstart;
748 pstate->prev_lexptr = pstate->lexptr;
750 tokstart = pstate->lexptr;
753 /* See if it is a special token of length 2 */
754 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
755 if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
758 return tokentab2[i].token;
761 switch (c = *tokstart)
778 if (paren_depth == 0)
785 if (pstate->comma_terminates && paren_depth == 0)
791 /* Might be a floating point number. */
792 if (pstate->lexptr[1] >= '0' && pstate->lexptr[1] <= '9')
793 break; /* Falls into number code. */
800 /* These are character tokens that appear as-is in the YACC grammar */
823 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
826 c = tokstart[++namelen];
827 if (c >= '0' && c <= '9')
829 c = tokstart[++namelen];
830 if (c >= '0' && c <= '9')
831 c = tokstart[++namelen];
835 error (_("Unterminated string or character constant."));
836 yylval.sval.ptr = tokstart + 1;
837 yylval.sval.length = namelen - 1;
838 pstate->lexptr += namelen + 1;
840 if(namelen == 2) /* Single character */
842 yylval.ulval = tokstart[1];
849 /* Is it a number? */
850 /* Note: We have already dealt with the case of the token '.'.
851 See case '.' above. */
852 if ((c >= '0' && c <= '9'))
855 int got_dot = 0, got_e = 0;
856 const char *p = tokstart;
861 if (!got_e && (*p == 'e' || *p == 'E'))
863 else if (!got_dot && *p == '.')
865 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
866 && (*p == '-' || *p == '+'))
867 /* This is the sign of the exponent, not the end of the
870 else if ((*p < '0' || *p > '9') &&
871 (*p < 'A' || *p > 'F') &&
872 (*p != 'H')) /* Modula-2 hexadecimal number */
875 toktype = parse_number (p - tokstart);
876 if (toktype == ERROR)
878 char *err_copy = (char *) alloca (p - tokstart + 1);
880 memcpy (err_copy, tokstart, p - tokstart);
881 err_copy[p - tokstart] = 0;
882 error (_("Invalid number \"%s\"."), err_copy);
888 if (!(c == '_' || c == '$'
889 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
890 /* We must have come across a bad character (e.g. ';'). */
891 error (_("Invalid character '%c' in expression."), c);
893 /* It's a name. See how long it is. */
895 for (c = tokstart[namelen];
896 (c == '_' || c == '$' || (c >= '0' && c <= '9')
897 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
898 c = tokstart[++namelen])
901 /* The token "if" terminates the expression and is NOT
902 removed from the input stream. */
903 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
908 pstate->lexptr += namelen;
910 /* Lookup special keywords */
911 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
912 if (namelen == strlen (keytab[i].keyw)
913 && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
914 return keytab[i].token;
916 yylval.sval.ptr = tokstart;
917 yylval.sval.length = namelen;
919 if (*tokstart == '$')
920 return DOLLAR_VARIABLE;
922 /* Use token-type BLOCKNAME for symbols that happen to be defined as
923 functions. If this is not so, then ...
924 Use token-type TYPENAME for symbols that happen to be defined
925 currently as names of types; NAME for other symbols.
926 The caller is not constrained to care about the distinction. */
928 std::string tmp = copy_name (yylval.sval);
931 if (lookup_symtab (tmp.c_str ()))
933 sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
934 VAR_DOMAIN, 0).symbol;
935 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
937 if (lookup_typename (pstate->language (),
938 tmp.c_str (), pstate->expression_context_block, 1))
943 switch(SYMBOL_CLASS (sym))
949 case LOC_REGPARM_ADDR:
952 case LOC_CONST_BYTES:
953 case LOC_OPTIMIZED_OUT:
964 error (_("internal: Undefined class in m2lex()"));
968 error (_("internal: Unforseen case in m2lex()"));
971 error (_("unhandled token in m2lex()"));
977 /* Built-in BOOLEAN type. This is sort of a hack. */
978 if (startswith (tokstart, "TRUE"))
983 else if (startswith (tokstart, "FALSE"))
990 /* Must be another type of name... */
996 m2_language::parser (struct parser_state *par_state) const
998 /* Setting up the parser state. */
999 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1000 gdb_assert (par_state != NULL);
1004 int result = yyparse ();
1006 pstate->set_operation (pstate->pop ());
1011 yyerror (const char *msg)
1013 if (pstate->prev_lexptr)
1014 pstate->lexptr = pstate->prev_lexptr;
1016 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);