1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Parse a C expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result. */
37 #include "expression.h"
38 #include "parser-defs.h"
42 /* Ensure that if the generated parser contains any calls to malloc/realloc,
43 that they get mapped to xmalloc/xrealloc. */
45 #define malloc xmalloc
46 #define realloc xrealloc
48 /* These MUST be included in any grammar file!!!!
49 Please choose unique names! */
50 #define yymaxdepth c_maxdepth
51 #define yyparse c_parse
53 #define yyerror c_error
56 #define yydebug c_debug
65 #define yyerrflag c_errflag
66 #define yynerrs c_nerrs
71 #define yystate c_state
79 __yy_bcopy PARAMS ((char *, char *, int));
82 yyparse PARAMS ((void));
85 yylex PARAMS ((void));
88 yyerror PARAMS ((char *));
90 /* #define YYDEBUG 1 */
94 /* Although the yacc "value" of an expression is not used,
95 since the result is stored in the structure being created,
96 other node types do have values. */
101 unsigned LONGEST ulval;
107 struct symtoken ssym;
110 enum exp_opcode opcode;
111 struct internalvar *ivar;
118 /* YYSTYPE gets defined by %union */
120 parse_number PARAMS ((char *, int, int, YYSTYPE *));
123 %type <voidval> exp exp1 type_exp start variable qualified_name
124 %type <tval> type typebase
125 %type <tvec> nonempty_typelist
126 /* %type <bval> block */
128 /* Fancy type parsing. */
129 %type <voidval> func_mod direct_abs_decl abs_decl
131 %type <lval> array_mod
133 %token <lval> INT CHAR
137 /* Both NAME and TYPENAME tokens represent symbols in the input,
138 and both convey their data as strings.
139 But a TYPENAME is a string that happens to be defined as a typedef
140 or builtin type name (such as int or char)
141 and a NAME is any other symbol.
142 Contexts where this distinction is not important can use the
143 nonterminal "name", which matches either NAME or TYPENAME. */
146 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
147 %token <tsym> TYPENAME
149 %type <ssym> name_not_typename
150 %type <tsym> typename
152 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
153 but which would parse as a valid number in the current input radix.
154 E.g. "c" when input_radix==16. Depending on the parse, it will be
155 turned into a name or into a number. NAME_OR_UINT ditto. */
157 %token <ssym> NAME_OR_INT NAME_OR_UINT
159 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
163 /* Special type cases, put in to allow the parser to distinguish different
165 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD
167 %token <lval> LAST REGNAME
169 %token <ivar> VARIABLE
171 %token <opcode> ASSIGN_MODIFY
178 %right '=' ASSIGN_MODIFY
186 %left '<' '>' LEQ GEQ
191 %right UNARY INCREMENT DECREMENT
192 %right ARROW '.' '[' '('
193 %token <ssym> BLOCKNAME
204 { write_exp_elt_opcode(OP_TYPE);
205 write_exp_elt_type($1);
206 write_exp_elt_opcode(OP_TYPE);}
209 /* Expressions, including the comma operator. */
212 { write_exp_elt_opcode (BINOP_COMMA); }
215 /* Expressions, not including the comma operator. */
216 exp : '*' exp %prec UNARY
217 { write_exp_elt_opcode (UNOP_IND); }
219 exp : '&' exp %prec UNARY
220 { write_exp_elt_opcode (UNOP_ADDR); }
222 exp : '-' exp %prec UNARY
223 { write_exp_elt_opcode (UNOP_NEG); }
226 exp : '!' exp %prec UNARY
227 { write_exp_elt_opcode (UNOP_ZEROP); }
230 exp : '~' exp %prec UNARY
231 { write_exp_elt_opcode (UNOP_LOGNOT); }
234 exp : INCREMENT exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
238 exp : DECREMENT exp %prec UNARY
239 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
242 exp : exp INCREMENT %prec UNARY
243 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
246 exp : exp DECREMENT %prec UNARY
247 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
250 exp : SIZEOF exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_SIZEOF); }
255 { write_exp_elt_opcode (STRUCTOP_PTR);
256 write_exp_string ($3);
257 write_exp_elt_opcode (STRUCTOP_PTR); }
260 exp : exp ARROW qualified_name
261 { /* exp->type::name becomes exp->*(&type::name) */
262 /* Note: this doesn't work if name is a
263 static member! FIXME */
264 write_exp_elt_opcode (UNOP_ADDR);
265 write_exp_elt_opcode (STRUCTOP_MPTR); }
267 exp : exp ARROW '*' exp
268 { write_exp_elt_opcode (STRUCTOP_MPTR); }
272 { write_exp_elt_opcode (STRUCTOP_STRUCT);
273 write_exp_string ($3);
274 write_exp_elt_opcode (STRUCTOP_STRUCT); }
277 exp : exp '.' qualified_name
278 { /* exp.type::name becomes exp.*(&type::name) */
279 /* Note: this doesn't work if name is a
280 static member! FIXME */
281 write_exp_elt_opcode (UNOP_ADDR);
282 write_exp_elt_opcode (STRUCTOP_MEMBER); }
285 exp : exp '.' '*' exp
286 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
289 exp : exp '[' exp1 ']'
290 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
294 /* This is to save the value of arglist_len
295 being accumulated by an outer function call. */
296 { start_arglist (); }
297 arglist ')' %prec ARROW
298 { write_exp_elt_opcode (OP_FUNCALL);
299 write_exp_elt_longcst ((LONGEST) end_arglist ());
300 write_exp_elt_opcode (OP_FUNCALL); }
310 arglist : arglist ',' exp %prec ABOVE_COMMA
314 exp : '{' type '}' exp %prec UNARY
315 { write_exp_elt_opcode (UNOP_MEMVAL);
316 write_exp_elt_type ($2);
317 write_exp_elt_opcode (UNOP_MEMVAL); }
320 exp : '(' type ')' exp %prec UNARY
321 { write_exp_elt_opcode (UNOP_CAST);
322 write_exp_elt_type ($2);
323 write_exp_elt_opcode (UNOP_CAST); }
330 /* Binary operators in order of decreasing precedence. */
333 { write_exp_elt_opcode (BINOP_REPEAT); }
337 { write_exp_elt_opcode (BINOP_MUL); }
341 { write_exp_elt_opcode (BINOP_DIV); }
345 { write_exp_elt_opcode (BINOP_REM); }
349 { write_exp_elt_opcode (BINOP_ADD); }
353 { write_exp_elt_opcode (BINOP_SUB); }
357 { write_exp_elt_opcode (BINOP_LSH); }
361 { write_exp_elt_opcode (BINOP_RSH); }
365 { write_exp_elt_opcode (BINOP_EQUAL); }
368 exp : exp NOTEQUAL exp
369 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
373 { write_exp_elt_opcode (BINOP_LEQ); }
377 { write_exp_elt_opcode (BINOP_GEQ); }
381 { write_exp_elt_opcode (BINOP_LESS); }
385 { write_exp_elt_opcode (BINOP_GTR); }
389 { write_exp_elt_opcode (BINOP_LOGAND); }
393 { write_exp_elt_opcode (BINOP_LOGXOR); }
397 { write_exp_elt_opcode (BINOP_LOGIOR); }
401 { write_exp_elt_opcode (BINOP_AND); }
405 { write_exp_elt_opcode (BINOP_OR); }
408 exp : exp '?' exp ':' exp %prec '?'
409 { write_exp_elt_opcode (TERNOP_COND); }
413 { write_exp_elt_opcode (BINOP_ASSIGN); }
416 exp : exp ASSIGN_MODIFY exp
417 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
418 write_exp_elt_opcode ($2);
419 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
423 { write_exp_elt_opcode (OP_LONG);
424 if ($1 == (int) $1 || $1 == (unsigned int) $1)
425 write_exp_elt_type (builtin_type_int);
427 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
428 write_exp_elt_longcst ((LONGEST) $1);
429 write_exp_elt_opcode (OP_LONG); }
434 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
435 write_exp_elt_opcode (OP_LONG);
436 if (val.lval == (int) val.lval ||
437 val.lval == (unsigned int) val.lval)
438 write_exp_elt_type (builtin_type_int);
440 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
441 write_exp_elt_longcst (val.lval);
442 write_exp_elt_opcode (OP_LONG); }
447 write_exp_elt_opcode (OP_LONG);
448 if ($1 == (unsigned int) $1)
449 write_exp_elt_type (builtin_type_unsigned_int);
451 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
452 write_exp_elt_longcst ((LONGEST) $1);
453 write_exp_elt_opcode (OP_LONG);
459 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
460 write_exp_elt_opcode (OP_LONG);
461 if (val.ulval == (unsigned int) val.ulval)
462 write_exp_elt_type (builtin_type_unsigned_int);
464 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
465 write_exp_elt_longcst ((LONGEST)val.ulval);
466 write_exp_elt_opcode (OP_LONG);
471 { write_exp_elt_opcode (OP_LONG);
472 write_exp_elt_type (builtin_type_char);
473 write_exp_elt_longcst ((LONGEST) $1);
474 write_exp_elt_opcode (OP_LONG); }
478 { write_exp_elt_opcode (OP_DOUBLE);
479 write_exp_elt_type (builtin_type_double);
480 write_exp_elt_dblcst ($1);
481 write_exp_elt_opcode (OP_DOUBLE); }
488 { write_exp_elt_opcode (OP_LAST);
489 write_exp_elt_longcst ((LONGEST) $1);
490 write_exp_elt_opcode (OP_LAST); }
494 { write_exp_elt_opcode (OP_REGISTER);
495 write_exp_elt_longcst ((LONGEST) $1);
496 write_exp_elt_opcode (OP_REGISTER); }
500 { write_exp_elt_opcode (OP_INTERNALVAR);
501 write_exp_elt_intern ($1);
502 write_exp_elt_opcode (OP_INTERNALVAR); }
505 exp : SIZEOF '(' type ')' %prec UNARY
506 { write_exp_elt_opcode (OP_LONG);
507 write_exp_elt_type (builtin_type_int);
508 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
509 write_exp_elt_opcode (OP_LONG); }
513 { write_exp_elt_opcode (OP_STRING);
514 write_exp_string ($1);
515 write_exp_elt_opcode (OP_STRING); }
520 { write_exp_elt_opcode (OP_THIS);
521 write_exp_elt_opcode (OP_THIS); }
529 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
533 lookup_symtab (copy_name ($1.stoken));
535 $$ = BLOCKVECTOR_BLOCK
536 (BLOCKVECTOR (tem), STATIC_BLOCK);
538 error ("No file or function \"%s\".",
539 copy_name ($1.stoken));
544 block : block COLONCOLON name
546 = lookup_symbol (copy_name ($3), $1,
547 VAR_NAMESPACE, 0, NULL);
548 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
549 error ("No function \"%s\" in specified context.",
551 $$ = SYMBOL_BLOCK_VALUE (tem); }
554 variable: block COLONCOLON name
555 { struct symbol *sym;
556 sym = lookup_symbol (copy_name ($3), $1,
557 VAR_NAMESPACE, 0, NULL);
559 error ("No symbol \"%s\" in specified context.",
562 write_exp_elt_opcode (OP_VAR_VALUE);
563 write_exp_elt_sym (sym);
564 write_exp_elt_opcode (OP_VAR_VALUE); }
567 qualified_name: typebase COLONCOLON name
569 struct type *type = $1;
570 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
571 && TYPE_CODE (type) != TYPE_CODE_UNION)
572 error ("`%s' is not defined as an aggregate type.",
575 write_exp_elt_opcode (OP_SCOPE);
576 write_exp_elt_type (type);
577 write_exp_string ($3);
578 write_exp_elt_opcode (OP_SCOPE);
580 | typebase COLONCOLON '~' name
582 struct type *type = $1;
583 struct stoken tmp_token;
584 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
585 && TYPE_CODE (type) != TYPE_CODE_UNION)
586 error ("`%s' is not defined as an aggregate type.",
589 if (strcmp (type_name_no_tag (type), $4.ptr))
590 error ("invalid destructor `%s::~%s'",
591 type_name_no_tag (type), $4.ptr);
593 tmp_token.ptr = (char*) alloca ($4.length + 2);
594 tmp_token.length = $4.length + 1;
595 tmp_token.ptr[0] = '~';
596 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
597 tmp_token.ptr[tmp_token.length] = 0;
598 write_exp_elt_opcode (OP_SCOPE);
599 write_exp_elt_type (type);
600 write_exp_string (tmp_token);
601 write_exp_elt_opcode (OP_SCOPE);
605 variable: qualified_name
608 char *name = copy_name ($2);
610 struct minimal_symbol *msymbol;
613 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
616 write_exp_elt_opcode (OP_VAR_VALUE);
617 write_exp_elt_sym (sym);
618 write_exp_elt_opcode (OP_VAR_VALUE);
622 msymbol = lookup_minimal_symbol (name,
623 (struct objfile *) NULL);
626 write_exp_elt_opcode (OP_LONG);
627 write_exp_elt_type (builtin_type_int);
628 write_exp_elt_longcst ((LONGEST) msymbol -> address);
629 write_exp_elt_opcode (OP_LONG);
630 write_exp_elt_opcode (UNOP_MEMVAL);
631 if (msymbol -> type == mst_data ||
632 msymbol -> type == mst_bss)
633 write_exp_elt_type (builtin_type_int);
634 else if (msymbol -> type == mst_text)
635 write_exp_elt_type (lookup_function_type (builtin_type_int));
637 write_exp_elt_type (builtin_type_char);
638 write_exp_elt_opcode (UNOP_MEMVAL);
641 if (!have_full_symbols () && !have_partial_symbols ())
642 error ("No symbol table is loaded. Use the \"file\" command.");
644 error ("No symbol \"%s\" in current context.", name);
648 variable: name_not_typename
649 { struct symbol *sym = $1.sym;
653 switch (SYMBOL_CLASS (sym))
661 if (innermost_block == 0 ||
662 contained_in (block_found,
664 innermost_block = block_found;
671 case LOC_CONST_BYTES:
673 /* In this case the expression can
674 be evaluated regardless of what
675 frame we are in, so there is no
676 need to check for the
677 innermost_block. These cases are
678 listed so that gcc -Wall will
679 report types that may not have
684 write_exp_elt_opcode (OP_VAR_VALUE);
685 write_exp_elt_sym (sym);
686 write_exp_elt_opcode (OP_VAR_VALUE);
688 else if ($1.is_a_field_of_this)
690 /* C++: it hangs off of `this'. Must
691 not inadvertently convert from a method call
693 if (innermost_block == 0 ||
694 contained_in (block_found, innermost_block))
695 innermost_block = block_found;
696 write_exp_elt_opcode (OP_THIS);
697 write_exp_elt_opcode (OP_THIS);
698 write_exp_elt_opcode (STRUCTOP_PTR);
699 write_exp_string ($1.stoken);
700 write_exp_elt_opcode (STRUCTOP_PTR);
704 struct minimal_symbol *msymbol;
705 register char *arg = copy_name ($1.stoken);
707 msymbol = lookup_minimal_symbol (arg,
708 (struct objfile *) NULL);
711 write_exp_elt_opcode (OP_LONG);
712 write_exp_elt_type (builtin_type_int);
713 write_exp_elt_longcst ((LONGEST) msymbol -> address);
714 write_exp_elt_opcode (OP_LONG);
715 write_exp_elt_opcode (UNOP_MEMVAL);
716 if (msymbol -> type == mst_data ||
717 msymbol -> type == mst_bss)
718 write_exp_elt_type (builtin_type_int);
719 else if (msymbol -> type == mst_text)
720 write_exp_elt_type (lookup_function_type (builtin_type_int));
722 write_exp_elt_type (builtin_type_char);
723 write_exp_elt_opcode (UNOP_MEMVAL);
725 else if (!have_full_symbols () && !have_partial_symbols ())
726 error ("No symbol table is loaded. Use the \"file\" command.");
728 error ("No symbol \"%s\" in current context.",
729 copy_name ($1.stoken));
738 /* This is where the interesting stuff happens. */
741 struct type *follow_type = $1;
750 follow_type = lookup_pointer_type (follow_type);
753 follow_type = lookup_reference_type (follow_type);
756 array_size = pop_type_int ();
757 if (array_size != -1)
758 follow_type = create_array_type (follow_type,
761 follow_type = lookup_pointer_type (follow_type);
764 follow_type = lookup_function_type (follow_type);
772 { push_type (tp_pointer); $$ = 0; }
774 { push_type (tp_pointer); $$ = $2; }
776 { push_type (tp_reference); $$ = 0; }
778 { push_type (tp_reference); $$ = $2; }
782 direct_abs_decl: '(' abs_decl ')'
784 | direct_abs_decl array_mod
787 push_type (tp_array);
792 push_type (tp_array);
795 | direct_abs_decl func_mod
796 { push_type (tp_function); }
798 { push_type (tp_function); }
809 | '(' nonempty_typelist ')'
810 { free ($2); $$ = 0; }
814 | typebase COLONCOLON '*'
815 { $$ = lookup_member_type (builtin_type_int, $1); }
816 | type '(' typebase COLONCOLON '*' ')'
817 { $$ = lookup_member_type ($1, $3); }
818 | type '(' typebase COLONCOLON '*' ')' '(' ')'
819 { $$ = lookup_member_type
820 (lookup_function_type ($1), $3); }
821 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
822 { $$ = lookup_member_type
823 (lookup_function_type ($1), $3);
831 { $$ = builtin_type_int; }
833 { $$ = builtin_type_long; }
835 { $$ = builtin_type_short; }
837 { $$ = builtin_type_long; }
838 | UNSIGNED LONG INT_KEYWORD
839 { $$ = builtin_type_unsigned_long; }
841 { $$ = builtin_type_long_long; }
842 | LONG LONG INT_KEYWORD
843 { $$ = builtin_type_long_long; }
845 { $$ = builtin_type_unsigned_long_long; }
846 | UNSIGNED LONG LONG INT_KEYWORD
847 { $$ = builtin_type_unsigned_long_long; }
849 { $$ = builtin_type_short; }
850 | UNSIGNED SHORT INT_KEYWORD
851 { $$ = builtin_type_unsigned_short; }
853 { $$ = lookup_struct (copy_name ($2),
854 expression_context_block); }
856 { $$ = lookup_union (copy_name ($2),
857 expression_context_block); }
859 { $$ = lookup_enum (copy_name ($2),
860 expression_context_block); }
862 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
864 { $$ = builtin_type_unsigned_int; }
865 | SIGNED_KEYWORD typename
868 { $$ = builtin_type_int; }
869 | TEMPLATE name '<' type '>'
870 { $$ = lookup_template_type(copy_name($2), $4,
871 expression_context_block);
878 $$.stoken.ptr = "int";
879 $$.stoken.length = 3;
880 $$.type = builtin_type_int;
884 $$.stoken.ptr = "long";
885 $$.stoken.length = 4;
886 $$.type = builtin_type_long;
890 $$.stoken.ptr = "short";
891 $$.stoken.length = 5;
892 $$.type = builtin_type_short;
898 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
899 $$[0] = (struct type *)0;
902 | nonempty_typelist ',' type
903 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
904 $$ = (struct type **)xrealloc ((char *) $1, len);
905 $$[$<ivec>$[0]] = $3;
909 name : NAME { $$ = $1.stoken; }
910 | BLOCKNAME { $$ = $1.stoken; }
911 | TYPENAME { $$ = $1.stoken; }
912 | NAME_OR_INT { $$ = $1.stoken; }
913 | NAME_OR_UINT { $$ = $1.stoken; }
916 name_not_typename : NAME
918 /* These would be useful if name_not_typename was useful, but it is just
919 a fake for "variable", so these cause reduce/reduce conflicts because
920 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
921 =exp) or just an exp. If name_not_typename was ever used in an lvalue
922 context where only a name could occur, this might be useful.
930 /* Take care of parsing a number (anything that starts with a digit).
931 Set yylval and return the token type; update lexptr.
932 LEN is the number of characters in it. */
934 /*** Needs some error checking for the float case ***/
937 parse_number (p, len, parsed_float, putithere)
943 register LONGEST n = 0;
944 register LONGEST prevn = 0;
947 register int base = input_radix;
952 /* It's a float since it contains a point or an exponent. */
953 putithere->dval = atof (p);
957 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
991 if (c >= 'A' && c <= 'Z')
993 if (c != 'l' && c != 'u')
995 if (c >= '0' && c <= '9')
999 if (base > 10 && c >= 'a' && c <= 'f')
1000 n += i = c - 'a' + 10;
1001 else if (len == 0 && c == 'l')
1003 else if (len == 0 && c == 'u')
1006 return ERROR; /* Char not a digit */
1009 return ERROR; /* Invalid digit in this base */
1010 /* Portably test for overflow (only works for nonzero values, so make
1011 a second check for zero). */
1012 if((prevn >= n) && n != 0)
1013 unsigned_p=1; /* Try something unsigned */
1014 /* If range checking enabled, portably test for unsigned overflow. */
1015 if(RANGE_CHECK && n!=0)
1017 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
1018 range_error("Overflow on numeric constant.");
1025 putithere->ulval = n;
1030 putithere->lval = n;
1039 enum exp_opcode opcode;
1042 const static struct token tokentab3[] =
1044 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1045 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1048 const static struct token tokentab2[] =
1050 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1051 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1052 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1053 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1054 {"%=", ASSIGN_MODIFY, BINOP_REM},
1055 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1056 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1057 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1058 {"++", INCREMENT, BINOP_END},
1059 {"--", DECREMENT, BINOP_END},
1060 {"->", ARROW, BINOP_END},
1061 {"&&", ANDAND, BINOP_END},
1062 {"||", OROR, BINOP_END},
1063 {"::", COLONCOLON, BINOP_END},
1064 {"<<", LSH, BINOP_END},
1065 {">>", RSH, BINOP_END},
1066 {"==", EQUAL, BINOP_END},
1067 {"!=", NOTEQUAL, BINOP_END},
1068 {"<=", LEQ, BINOP_END},
1069 {">=", GEQ, BINOP_END}
1072 /* Read one token, getting characters through lexptr. */
1078 register int namelen;
1079 register unsigned i;
1080 register char *tokstart;
1085 /* See if it is a special token of length 3. */
1086 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1087 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1090 yylval.opcode = tokentab3[i].opcode;
1091 return tokentab3[i].token;
1094 /* See if it is a special token of length 2. */
1095 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1096 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1099 yylval.opcode = tokentab2[i].opcode;
1100 return tokentab2[i].token;
1103 switch (c = *tokstart)
1118 c = parse_escape (&lexptr);
1122 error ("Invalid character constant.");
1131 if (paren_depth == 0)
1138 if (comma_terminates && paren_depth == 0)
1144 /* Might be a floating point number. */
1145 if (lexptr[1] < '0' || lexptr[1] > '9')
1146 goto symbol; /* Nope, must be a symbol. */
1147 /* FALL THRU into number case. */
1160 /* It's a number. */
1161 int got_dot = 0, got_e = 0, toktype;
1162 register char *p = tokstart;
1163 int hex = input_radix > 10;
1165 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1170 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1178 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1179 got_dot = got_e = 1;
1180 else if (!hex && !got_dot && *p == '.')
1182 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1183 && (*p == '-' || *p == '+'))
1184 /* This is the sign of the exponent, not the end of the
1187 /* We will take any letters or digits. parse_number will
1188 complain if past the radix, or if L or U are not final. */
1189 else if ((*p < '0' || *p > '9')
1190 && ((*p < 'a' || *p > 'z')
1191 && (*p < 'A' || *p > 'Z')))
1194 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1195 if (toktype == ERROR)
1197 char *err_copy = (char *) alloca (p - tokstart + 1);
1199 bcopy (tokstart, err_copy, p - tokstart);
1200 err_copy[p - tokstart] = 0;
1201 error ("Invalid number \"%s\".", err_copy);
1232 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1235 c = tokstart[++namelen];
1236 if (c >= '0' && c <= '9')
1238 c = tokstart[++namelen];
1239 if (c >= '0' && c <= '9')
1240 c = tokstart[++namelen];
1243 yylval.sval.ptr = tokstart + 1;
1244 yylval.sval.length = namelen - 1;
1245 lexptr += namelen + 1;
1249 if (!(c == '_' || c == '$'
1250 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1251 /* We must have come across a bad character (e.g. ';'). */
1252 error ("Invalid character '%c' in expression.", c);
1254 /* It's a name. See how long it is. */
1256 for (c = tokstart[namelen];
1257 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1258 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1259 c = tokstart[++namelen])
1262 /* The token "if" terminates the expression and is NOT
1263 removed from the input stream. */
1264 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1271 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1272 and $$digits (equivalent to $<-digits> if you could type that).
1273 Make token type LAST, and put the number (the digits) in yylval. */
1275 if (*tokstart == '$')
1277 register int negate = 0;
1279 /* Double dollar means negate the number and add -1 as well.
1280 Thus $$ alone means -1. */
1281 if (namelen >= 2 && tokstart[1] == '$')
1288 /* Just dollars (one or two) */
1289 yylval.lval = - negate;
1292 /* Is the rest of the token digits? */
1293 for (; c < namelen; c++)
1294 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1298 yylval.lval = atoi (tokstart + 1 + negate);
1300 yylval.lval = - yylval.lval;
1305 /* Handle tokens that refer to machine registers:
1306 $ followed by a register name. */
1308 if (*tokstart == '$') {
1309 for (c = 0; c < NUM_REGS; c++)
1310 if (namelen - 1 == strlen (reg_names[c])
1311 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1316 for (c = 0; c < num_std_regs; c++)
1317 if (namelen - 1 == strlen (std_regs[c].name)
1318 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1320 yylval.lval = std_regs[c].regnum;
1324 /* Catch specific keywords. Should be done with a data structure. */
1328 if (!strncmp (tokstart, "unsigned", 8))
1330 if (current_language->la_language == language_cplus
1331 && !strncmp (tokstart, "template", 8))
1335 if (!strncmp (tokstart, "struct", 6))
1337 if (!strncmp (tokstart, "signed", 6))
1338 return SIGNED_KEYWORD;
1339 if (!strncmp (tokstart, "sizeof", 6))
1343 if (!strncmp (tokstart, "union", 5))
1345 if (!strncmp (tokstart, "short", 5))
1349 if (!strncmp (tokstart, "enum", 4))
1351 if (!strncmp (tokstart, "long", 4))
1353 if (current_language->la_language == language_cplus
1354 && !strncmp (tokstart, "this", 4))
1356 static const char this_name[] =
1357 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1359 if (lookup_symbol (this_name, expression_context_block,
1360 VAR_NAMESPACE, 0, NULL))
1365 if (!strncmp (tokstart, "int", 3))
1372 yylval.sval.ptr = tokstart;
1373 yylval.sval.length = namelen;
1375 /* Any other names starting in $ are debugger internal variables. */
1377 if (*tokstart == '$')
1379 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1383 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1384 functions or symtabs. If this is not so, then ...
1385 Use token-type TYPENAME for symbols that happen to be defined
1386 currently as names of types; NAME for other symbols.
1387 The caller is not constrained to care about the distinction. */
1389 char *tmp = copy_name (yylval.sval);
1391 int is_a_field_of_this = 0;
1394 sym = lookup_symbol (tmp, expression_context_block,
1396 current_language->la_language == language_cplus
1397 ? &is_a_field_of_this : NULL,
1399 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1400 lookup_partial_symtab (tmp))
1402 yylval.ssym.sym = sym;
1403 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1406 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1408 yylval.tsym.type = SYMBOL_TYPE (sym);
1411 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1414 /* Input names that aren't symbols but ARE valid hex numbers,
1415 when the input radix permits them, can be names or numbers
1416 depending on the parse. Note we support radixes > 16 here. */
1418 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1419 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1421 YYSTYPE newlval; /* Its value is ignored. */
1422 hextype = parse_number (tokstart, namelen, 0, &newlval);
1425 yylval.ssym.sym = sym;
1426 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1429 if (hextype == UINT)
1431 yylval.ssym.sym = sym;
1432 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1433 return NAME_OR_UINT;
1437 /* Any other kind of symbol */
1438 yylval.ssym.sym = sym;
1439 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1448 error (msg ? msg : "Invalid syntax in expression.");
1451 /* Table mapping opcodes into strings for printing operators
1452 and precedences of the operators. */
1454 const static struct op_print c_op_print_tab[] =
1456 {",", BINOP_COMMA, PREC_COMMA, 0},
1457 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1458 {"||", BINOP_OR, PREC_OR, 0},
1459 {"&&", BINOP_AND, PREC_AND, 0},
1460 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1461 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1462 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1463 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1464 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1465 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1466 {">=", BINOP_GEQ, PREC_ORDER, 0},
1467 {">", BINOP_GTR, PREC_ORDER, 0},
1468 {"<", BINOP_LESS, PREC_ORDER, 0},
1469 {">>", BINOP_RSH, PREC_SHIFT, 0},
1470 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1471 {"+", BINOP_ADD, PREC_ADD, 0},
1472 {"-", BINOP_SUB, PREC_ADD, 0},
1473 {"*", BINOP_MUL, PREC_MUL, 0},
1474 {"/", BINOP_DIV, PREC_MUL, 0},
1475 {"%", BINOP_REM, PREC_MUL, 0},
1476 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1477 {"-", UNOP_NEG, PREC_PREFIX, 0},
1478 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1479 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1480 {"*", UNOP_IND, PREC_PREFIX, 0},
1481 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1482 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1483 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1484 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1486 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1489 /* These variables point to the objects
1490 representing the predefined C data types. */
1492 struct type *builtin_type_void;
1493 struct type *builtin_type_char;
1494 struct type *builtin_type_short;
1495 struct type *builtin_type_int;
1496 struct type *builtin_type_long;
1497 struct type *builtin_type_long_long;
1498 struct type *builtin_type_signed_char;
1499 struct type *builtin_type_unsigned_char;
1500 struct type *builtin_type_unsigned_short;
1501 struct type *builtin_type_unsigned_int;
1502 struct type *builtin_type_unsigned_long;
1503 struct type *builtin_type_unsigned_long_long;
1504 struct type *builtin_type_float;
1505 struct type *builtin_type_double;
1506 struct type *builtin_type_long_double;
1507 struct type *builtin_type_complex;
1508 struct type *builtin_type_double_complex;
1510 struct type ** const (c_builtin_types[]) =
1514 &builtin_type_short,
1516 &builtin_type_float,
1517 &builtin_type_double,
1519 &builtin_type_long_long,
1520 &builtin_type_signed_char,
1521 &builtin_type_unsigned_char,
1522 &builtin_type_unsigned_short,
1523 &builtin_type_unsigned_int,
1524 &builtin_type_unsigned_long,
1525 &builtin_type_unsigned_long_long,
1526 &builtin_type_long_double,
1527 &builtin_type_complex,
1528 &builtin_type_double_complex,
1532 const struct language_defn c_language_defn = {
1533 "c", /* Language name */
1540 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1541 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1542 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1543 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1544 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1545 c_op_print_tab, /* expression operators for printing */
1549 const struct language_defn cplus_language_defn = {
1550 "c++", /* Language name */
1557 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1558 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1559 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1560 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1561 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1562 c_op_print_tab, /* expression operators for printing */
1567 _initialize_c_exp ()
1570 init_type (TYPE_CODE_VOID, 1,
1572 "void", (struct objfile *) NULL);
1574 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1576 "char", (struct objfile *) NULL);
1577 builtin_type_signed_char =
1578 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1580 "signed char", (struct objfile *) NULL);
1581 builtin_type_unsigned_char =
1582 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1584 "unsigned char", (struct objfile *) NULL);
1585 builtin_type_short =
1586 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1588 "short", (struct objfile *) NULL);
1589 builtin_type_unsigned_short =
1590 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1592 "unsigned short", (struct objfile *) NULL);
1594 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1596 "int", (struct objfile *) NULL);
1597 builtin_type_unsigned_int =
1598 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1600 "unsigned int", (struct objfile *) NULL);
1602 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1604 "long", (struct objfile *) NULL);
1605 builtin_type_unsigned_long =
1606 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1608 "unsigned long", (struct objfile *) NULL);
1609 builtin_type_long_long =
1610 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1612 "long long", (struct objfile *) NULL);
1613 builtin_type_unsigned_long_long =
1614 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1616 "unsigned long long", (struct objfile *) NULL);
1617 builtin_type_float =
1618 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1620 "float", (struct objfile *) NULL);
1621 builtin_type_double =
1622 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1624 "double", (struct objfile *) NULL);
1625 builtin_type_long_double =
1626 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1628 "long double", (struct objfile *) NULL);
1629 builtin_type_complex =
1630 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1632 "complex", (struct objfile *) NULL);
1633 builtin_type_double_complex =
1634 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1636 "double complex", (struct objfile *) NULL);
1638 add_language (&c_language_defn);
1639 add_language (&cplus_language_defn);