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 yyparse PARAMS ((void));
82 yylex PARAMS ((void));
85 yyerror PARAMS ((char *));
87 /* #define YYDEBUG 1 */
91 /* Although the yacc "value" of an expression is not used,
92 since the result is stored in the structure being created,
93 other node types do have values. */
98 unsigned LONGEST ulval;
104 struct symtoken ssym;
107 enum exp_opcode opcode;
108 struct internalvar *ivar;
115 /* YYSTYPE gets defined by %union */
117 parse_number PARAMS ((char *, int, int, YYSTYPE *));
120 %type <voidval> exp exp1 type_exp start variable qualified_name
121 %type <tval> type typebase
122 %type <tvec> nonempty_typelist
123 /* %type <bval> block */
125 /* Fancy type parsing. */
126 %type <voidval> func_mod direct_abs_decl abs_decl
128 %type <lval> array_mod
130 %token <lval> INT CHAR
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. */
143 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
144 %token <tsym> TYPENAME
146 %type <ssym> name_not_typename
147 %type <tsym> typename
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. NAME_OR_UINT ditto. */
154 %token <ssym> NAME_OR_INT NAME_OR_UINT
156 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
160 /* Special type cases, put in to allow the parser to distinguish different
162 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD
164 %token <lval> LAST REGNAME
166 %token <ivar> VARIABLE
168 %token <opcode> ASSIGN_MODIFY
175 %right '=' ASSIGN_MODIFY
183 %left '<' '>' LEQ GEQ
188 %right UNARY INCREMENT DECREMENT
189 %right ARROW '.' '[' '('
190 %token <ssym> BLOCKNAME
201 { write_exp_elt_opcode(OP_TYPE);
202 write_exp_elt_type($1);
203 write_exp_elt_opcode(OP_TYPE);}
206 /* Expressions, including the comma operator. */
209 { write_exp_elt_opcode (BINOP_COMMA); }
212 /* Expressions, not including the comma operator. */
213 exp : '*' exp %prec UNARY
214 { write_exp_elt_opcode (UNOP_IND); }
216 exp : '&' exp %prec UNARY
217 { write_exp_elt_opcode (UNOP_ADDR); }
219 exp : '-' exp %prec UNARY
220 { write_exp_elt_opcode (UNOP_NEG); }
223 exp : '!' exp %prec UNARY
224 { write_exp_elt_opcode (UNOP_ZEROP); }
227 exp : '~' exp %prec UNARY
228 { write_exp_elt_opcode (UNOP_LOGNOT); }
231 exp : INCREMENT exp %prec UNARY
232 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
235 exp : DECREMENT exp %prec UNARY
236 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
239 exp : exp INCREMENT %prec UNARY
240 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
243 exp : exp DECREMENT %prec UNARY
244 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
247 exp : SIZEOF exp %prec UNARY
248 { write_exp_elt_opcode (UNOP_SIZEOF); }
252 { write_exp_elt_opcode (STRUCTOP_PTR);
253 write_exp_string ($3);
254 write_exp_elt_opcode (STRUCTOP_PTR); }
257 exp : exp ARROW qualified_name
258 { /* exp->type::name becomes exp->*(&type::name) */
259 /* Note: this doesn't work if name is a
260 static member! FIXME */
261 write_exp_elt_opcode (UNOP_ADDR);
262 write_exp_elt_opcode (STRUCTOP_MPTR); }
264 exp : exp ARROW '*' exp
265 { write_exp_elt_opcode (STRUCTOP_MPTR); }
269 { write_exp_elt_opcode (STRUCTOP_STRUCT);
270 write_exp_string ($3);
271 write_exp_elt_opcode (STRUCTOP_STRUCT); }
274 exp : exp '.' qualified_name
275 { /* exp.type::name becomes exp.*(&type::name) */
276 /* Note: this doesn't work if name is a
277 static member! FIXME */
278 write_exp_elt_opcode (UNOP_ADDR);
279 write_exp_elt_opcode (STRUCTOP_MEMBER); }
282 exp : exp '.' '*' exp
283 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
286 exp : exp '[' exp1 ']'
287 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
291 /* This is to save the value of arglist_len
292 being accumulated by an outer function call. */
293 { start_arglist (); }
294 arglist ')' %prec ARROW
295 { write_exp_elt_opcode (OP_FUNCALL);
296 write_exp_elt_longcst ((LONGEST) end_arglist ());
297 write_exp_elt_opcode (OP_FUNCALL); }
307 arglist : arglist ',' exp %prec ABOVE_COMMA
311 exp : '{' type '}' exp %prec UNARY
312 { write_exp_elt_opcode (UNOP_MEMVAL);
313 write_exp_elt_type ($2);
314 write_exp_elt_opcode (UNOP_MEMVAL); }
317 exp : '(' type ')' exp %prec UNARY
318 { write_exp_elt_opcode (UNOP_CAST);
319 write_exp_elt_type ($2);
320 write_exp_elt_opcode (UNOP_CAST); }
327 /* Binary operators in order of decreasing precedence. */
330 { write_exp_elt_opcode (BINOP_REPEAT); }
334 { write_exp_elt_opcode (BINOP_MUL); }
338 { write_exp_elt_opcode (BINOP_DIV); }
342 { write_exp_elt_opcode (BINOP_REM); }
346 { write_exp_elt_opcode (BINOP_ADD); }
350 { write_exp_elt_opcode (BINOP_SUB); }
354 { write_exp_elt_opcode (BINOP_LSH); }
358 { write_exp_elt_opcode (BINOP_RSH); }
362 { write_exp_elt_opcode (BINOP_EQUAL); }
365 exp : exp NOTEQUAL exp
366 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
370 { write_exp_elt_opcode (BINOP_LEQ); }
374 { write_exp_elt_opcode (BINOP_GEQ); }
378 { write_exp_elt_opcode (BINOP_LESS); }
382 { write_exp_elt_opcode (BINOP_GTR); }
386 { write_exp_elt_opcode (BINOP_LOGAND); }
390 { write_exp_elt_opcode (BINOP_LOGXOR); }
394 { write_exp_elt_opcode (BINOP_LOGIOR); }
398 { write_exp_elt_opcode (BINOP_AND); }
402 { write_exp_elt_opcode (BINOP_OR); }
405 exp : exp '?' exp ':' exp %prec '?'
406 { write_exp_elt_opcode (TERNOP_COND); }
410 { write_exp_elt_opcode (BINOP_ASSIGN); }
413 exp : exp ASSIGN_MODIFY exp
414 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
415 write_exp_elt_opcode ($2);
416 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
420 { write_exp_elt_opcode (OP_LONG);
421 if ($1 == (int) $1 || $1 == (unsigned int) $1)
422 write_exp_elt_type (builtin_type_int);
424 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
425 write_exp_elt_longcst ((LONGEST) $1);
426 write_exp_elt_opcode (OP_LONG); }
431 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
432 write_exp_elt_opcode (OP_LONG);
433 if (val.lval == (int) val.lval ||
434 val.lval == (unsigned int) val.lval)
435 write_exp_elt_type (builtin_type_int);
437 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
438 write_exp_elt_longcst (val.lval);
439 write_exp_elt_opcode (OP_LONG); }
444 write_exp_elt_opcode (OP_LONG);
445 if ($1 == (unsigned int) $1)
446 write_exp_elt_type (builtin_type_unsigned_int);
448 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
449 write_exp_elt_longcst ((LONGEST) $1);
450 write_exp_elt_opcode (OP_LONG);
456 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
457 write_exp_elt_opcode (OP_LONG);
458 if (val.ulval == (unsigned int) val.ulval)
459 write_exp_elt_type (builtin_type_unsigned_int);
461 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
462 write_exp_elt_longcst ((LONGEST)val.ulval);
463 write_exp_elt_opcode (OP_LONG);
468 { write_exp_elt_opcode (OP_LONG);
469 write_exp_elt_type (builtin_type_char);
470 write_exp_elt_longcst ((LONGEST) $1);
471 write_exp_elt_opcode (OP_LONG); }
475 { write_exp_elt_opcode (OP_DOUBLE);
476 write_exp_elt_type (builtin_type_double);
477 write_exp_elt_dblcst ($1);
478 write_exp_elt_opcode (OP_DOUBLE); }
485 { write_exp_elt_opcode (OP_LAST);
486 write_exp_elt_longcst ((LONGEST) $1);
487 write_exp_elt_opcode (OP_LAST); }
491 { write_exp_elt_opcode (OP_REGISTER);
492 write_exp_elt_longcst ((LONGEST) $1);
493 write_exp_elt_opcode (OP_REGISTER); }
497 { write_exp_elt_opcode (OP_INTERNALVAR);
498 write_exp_elt_intern ($1);
499 write_exp_elt_opcode (OP_INTERNALVAR); }
502 exp : SIZEOF '(' type ')' %prec UNARY
503 { write_exp_elt_opcode (OP_LONG);
504 write_exp_elt_type (builtin_type_int);
505 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
506 write_exp_elt_opcode (OP_LONG); }
510 { write_exp_elt_opcode (OP_STRING);
511 write_exp_string ($1);
512 write_exp_elt_opcode (OP_STRING); }
517 { write_exp_elt_opcode (OP_THIS);
518 write_exp_elt_opcode (OP_THIS); }
526 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
530 lookup_symtab (copy_name ($1.stoken));
532 $$ = BLOCKVECTOR_BLOCK
533 (BLOCKVECTOR (tem), STATIC_BLOCK);
535 error ("No file or function \"%s\".",
536 copy_name ($1.stoken));
541 block : block COLONCOLON name
543 = lookup_symbol (copy_name ($3), $1,
544 VAR_NAMESPACE, 0, NULL);
545 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
546 error ("No function \"%s\" in specified context.",
548 $$ = SYMBOL_BLOCK_VALUE (tem); }
551 variable: block COLONCOLON name
552 { struct symbol *sym;
553 sym = lookup_symbol (copy_name ($3), $1,
554 VAR_NAMESPACE, 0, NULL);
556 error ("No symbol \"%s\" in specified context.",
559 write_exp_elt_opcode (OP_VAR_VALUE);
560 write_exp_elt_sym (sym);
561 write_exp_elt_opcode (OP_VAR_VALUE); }
564 qualified_name: typebase COLONCOLON name
566 struct type *type = $1;
567 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
568 && TYPE_CODE (type) != TYPE_CODE_UNION)
569 error ("`%s' is not defined as an aggregate type.",
572 write_exp_elt_opcode (OP_SCOPE);
573 write_exp_elt_type (type);
574 write_exp_string ($3);
575 write_exp_elt_opcode (OP_SCOPE);
577 | typebase COLONCOLON '~' name
579 struct type *type = $1;
580 struct stoken tmp_token;
581 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
582 && TYPE_CODE (type) != TYPE_CODE_UNION)
583 error ("`%s' is not defined as an aggregate type.",
586 if (strcmp (type_name_no_tag (type), $4.ptr))
587 error ("invalid destructor `%s::~%s'",
588 type_name_no_tag (type), $4.ptr);
590 tmp_token.ptr = (char*) alloca ($4.length + 2);
591 tmp_token.length = $4.length + 1;
592 tmp_token.ptr[0] = '~';
593 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
594 tmp_token.ptr[tmp_token.length] = 0;
595 write_exp_elt_opcode (OP_SCOPE);
596 write_exp_elt_type (type);
597 write_exp_string (tmp_token);
598 write_exp_elt_opcode (OP_SCOPE);
602 variable: qualified_name
605 char *name = copy_name ($2);
607 struct minimal_symbol *msymbol;
610 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
613 write_exp_elt_opcode (OP_VAR_VALUE);
614 write_exp_elt_sym (sym);
615 write_exp_elt_opcode (OP_VAR_VALUE);
619 msymbol = lookup_minimal_symbol (name,
620 (struct objfile *) NULL);
623 write_exp_elt_opcode (OP_LONG);
624 write_exp_elt_type (builtin_type_int);
625 write_exp_elt_longcst ((LONGEST) msymbol -> address);
626 write_exp_elt_opcode (OP_LONG);
627 write_exp_elt_opcode (UNOP_MEMVAL);
628 if (msymbol -> type == mst_data ||
629 msymbol -> type == mst_bss)
630 write_exp_elt_type (builtin_type_int);
631 else if (msymbol -> type == mst_text)
632 write_exp_elt_type (lookup_function_type (builtin_type_int));
634 write_exp_elt_type (builtin_type_char);
635 write_exp_elt_opcode (UNOP_MEMVAL);
638 if (!have_full_symbols () && !have_partial_symbols ())
639 error ("No symbol table is loaded. Use the \"file\" command.");
641 error ("No symbol \"%s\" in current context.", name);
645 variable: name_not_typename
646 { struct symbol *sym = $1.sym;
650 switch (SYMBOL_CLASS (sym))
658 if (innermost_block == 0 ||
659 contained_in (block_found,
661 innermost_block = block_found;
668 case LOC_CONST_BYTES:
670 /* In this case the expression can
671 be evaluated regardless of what
672 frame we are in, so there is no
673 need to check for the
674 innermost_block. These cases are
675 listed so that gcc -Wall will
676 report types that may not have
681 write_exp_elt_opcode (OP_VAR_VALUE);
682 write_exp_elt_sym (sym);
683 write_exp_elt_opcode (OP_VAR_VALUE);
685 else if ($1.is_a_field_of_this)
687 /* C++: it hangs off of `this'. Must
688 not inadvertently convert from a method call
690 if (innermost_block == 0 ||
691 contained_in (block_found, innermost_block))
692 innermost_block = block_found;
693 write_exp_elt_opcode (OP_THIS);
694 write_exp_elt_opcode (OP_THIS);
695 write_exp_elt_opcode (STRUCTOP_PTR);
696 write_exp_string ($1.stoken);
697 write_exp_elt_opcode (STRUCTOP_PTR);
701 struct minimal_symbol *msymbol;
702 register char *arg = copy_name ($1.stoken);
704 msymbol = lookup_minimal_symbol (arg,
705 (struct objfile *) NULL);
708 write_exp_elt_opcode (OP_LONG);
709 write_exp_elt_type (builtin_type_int);
710 write_exp_elt_longcst ((LONGEST) msymbol -> address);
711 write_exp_elt_opcode (OP_LONG);
712 write_exp_elt_opcode (UNOP_MEMVAL);
713 if (msymbol -> type == mst_data ||
714 msymbol -> type == mst_bss)
715 write_exp_elt_type (builtin_type_int);
716 else if (msymbol -> type == mst_text)
717 write_exp_elt_type (lookup_function_type (builtin_type_int));
719 write_exp_elt_type (builtin_type_char);
720 write_exp_elt_opcode (UNOP_MEMVAL);
722 else if (!have_full_symbols () && !have_partial_symbols ())
723 error ("No symbol table is loaded. Use the \"file\" command.");
725 error ("No symbol \"%s\" in current context.",
726 copy_name ($1.stoken));
735 /* This is where the interesting stuff happens. */
738 struct type *follow_type = $1;
747 follow_type = lookup_pointer_type (follow_type);
750 follow_type = lookup_reference_type (follow_type);
753 array_size = pop_type_int ();
754 if (array_size != -1)
755 follow_type = create_array_type (follow_type,
758 follow_type = lookup_pointer_type (follow_type);
761 follow_type = lookup_function_type (follow_type);
769 { push_type (tp_pointer); $$ = 0; }
771 { push_type (tp_pointer); $$ = $2; }
773 { push_type (tp_reference); $$ = 0; }
775 { push_type (tp_reference); $$ = $2; }
779 direct_abs_decl: '(' abs_decl ')'
781 | direct_abs_decl array_mod
784 push_type (tp_array);
789 push_type (tp_array);
792 | direct_abs_decl func_mod
793 { push_type (tp_function); }
795 { push_type (tp_function); }
806 | '(' nonempty_typelist ')'
807 { free ((PTR)$2); $$ = 0; }
811 | typebase COLONCOLON '*'
812 { $$ = lookup_member_type (builtin_type_int, $1); }
813 | type '(' typebase COLONCOLON '*' ')'
814 { $$ = lookup_member_type ($1, $3); }
815 | type '(' typebase COLONCOLON '*' ')' '(' ')'
816 { $$ = lookup_member_type
817 (lookup_function_type ($1), $3); }
818 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
819 { $$ = lookup_member_type
820 (lookup_function_type ($1), $3);
828 { $$ = builtin_type_int; }
830 { $$ = builtin_type_long; }
832 { $$ = builtin_type_short; }
834 { $$ = builtin_type_long; }
835 | UNSIGNED LONG INT_KEYWORD
836 { $$ = builtin_type_unsigned_long; }
838 { $$ = builtin_type_long_long; }
839 | LONG LONG INT_KEYWORD
840 { $$ = builtin_type_long_long; }
842 { $$ = builtin_type_unsigned_long_long; }
843 | UNSIGNED LONG LONG INT_KEYWORD
844 { $$ = builtin_type_unsigned_long_long; }
846 { $$ = builtin_type_short; }
847 | UNSIGNED SHORT INT_KEYWORD
848 { $$ = builtin_type_unsigned_short; }
850 { $$ = lookup_struct (copy_name ($2),
851 expression_context_block); }
853 { $$ = lookup_union (copy_name ($2),
854 expression_context_block); }
856 { $$ = lookup_enum (copy_name ($2),
857 expression_context_block); }
859 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
861 { $$ = builtin_type_unsigned_int; }
862 | SIGNED_KEYWORD typename
865 { $$ = builtin_type_int; }
866 | TEMPLATE name '<' type '>'
867 { $$ = lookup_template_type(copy_name($2), $4,
868 expression_context_block);
875 $$.stoken.ptr = "int";
876 $$.stoken.length = 3;
877 $$.type = builtin_type_int;
881 $$.stoken.ptr = "long";
882 $$.stoken.length = 4;
883 $$.type = builtin_type_long;
887 $$.stoken.ptr = "short";
888 $$.stoken.length = 5;
889 $$.type = builtin_type_short;
895 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
896 $$[0] = (struct type *)0;
899 | nonempty_typelist ',' type
900 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
901 $$ = (struct type **)xrealloc ((char *) $1, len);
902 $$[$<ivec>$[0]] = $3;
906 name : NAME { $$ = $1.stoken; }
907 | BLOCKNAME { $$ = $1.stoken; }
908 | TYPENAME { $$ = $1.stoken; }
909 | NAME_OR_INT { $$ = $1.stoken; }
910 | NAME_OR_UINT { $$ = $1.stoken; }
913 name_not_typename : NAME
915 /* These would be useful if name_not_typename was useful, but it is just
916 a fake for "variable", so these cause reduce/reduce conflicts because
917 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
918 =exp) or just an exp. If name_not_typename was ever used in an lvalue
919 context where only a name could occur, this might be useful.
927 /* Take care of parsing a number (anything that starts with a digit).
928 Set yylval and return the token type; update lexptr.
929 LEN is the number of characters in it. */
931 /*** Needs some error checking for the float case ***/
934 parse_number (p, len, parsed_float, putithere)
940 register LONGEST n = 0;
941 register LONGEST prevn = 0;
944 register int base = input_radix;
949 /* It's a float since it contains a point or an exponent. */
950 putithere->dval = atof (p);
954 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
988 if (c >= 'A' && c <= 'Z')
990 if (c != 'l' && c != 'u')
992 if (c >= '0' && c <= '9')
996 if (base > 10 && c >= 'a' && c <= 'f')
997 n += i = c - 'a' + 10;
998 else if (len == 0 && c == 'l')
1000 else if (len == 0 && c == 'u')
1003 return ERROR; /* Char not a digit */
1006 return ERROR; /* Invalid digit in this base */
1007 /* Portably test for overflow (only works for nonzero values, so make
1008 a second check for zero). */
1009 if((prevn >= n) && n != 0)
1010 unsigned_p=1; /* Try something unsigned */
1011 /* If range checking enabled, portably test for unsigned overflow. */
1012 if(RANGE_CHECK && n!=0)
1014 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
1015 range_error("Overflow on numeric constant.");
1022 putithere->ulval = n;
1027 putithere->lval = n;
1036 enum exp_opcode opcode;
1039 const static struct token tokentab3[] =
1041 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1042 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1045 const static struct token tokentab2[] =
1047 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1048 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1049 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1050 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1051 {"%=", ASSIGN_MODIFY, BINOP_REM},
1052 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1053 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1054 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1055 {"++", INCREMENT, BINOP_END},
1056 {"--", DECREMENT, BINOP_END},
1057 {"->", ARROW, BINOP_END},
1058 {"&&", ANDAND, BINOP_END},
1059 {"||", OROR, BINOP_END},
1060 {"::", COLONCOLON, BINOP_END},
1061 {"<<", LSH, BINOP_END},
1062 {">>", RSH, BINOP_END},
1063 {"==", EQUAL, BINOP_END},
1064 {"!=", NOTEQUAL, BINOP_END},
1065 {"<=", LEQ, BINOP_END},
1066 {">=", GEQ, BINOP_END}
1069 /* Read one token, getting characters through lexptr. */
1075 register int namelen;
1076 register unsigned i;
1077 register char *tokstart;
1082 /* See if it is a special token of length 3. */
1083 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1084 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1087 yylval.opcode = tokentab3[i].opcode;
1088 return tokentab3[i].token;
1091 /* See if it is a special token of length 2. */
1092 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1093 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1096 yylval.opcode = tokentab2[i].opcode;
1097 return tokentab2[i].token;
1100 switch (c = *tokstart)
1115 c = parse_escape (&lexptr);
1119 error ("Invalid character constant.");
1128 if (paren_depth == 0)
1135 if (comma_terminates && paren_depth == 0)
1141 /* Might be a floating point number. */
1142 if (lexptr[1] < '0' || lexptr[1] > '9')
1143 goto symbol; /* Nope, must be a symbol. */
1144 /* FALL THRU into number case. */
1157 /* It's a number. */
1158 int got_dot = 0, got_e = 0, toktype;
1159 register char *p = tokstart;
1160 int hex = input_radix > 10;
1162 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1167 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1175 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1176 got_dot = got_e = 1;
1177 else if (!hex && !got_dot && *p == '.')
1179 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1180 && (*p == '-' || *p == '+'))
1181 /* This is the sign of the exponent, not the end of the
1184 /* We will take any letters or digits. parse_number will
1185 complain if past the radix, or if L or U are not final. */
1186 else if ((*p < '0' || *p > '9')
1187 && ((*p < 'a' || *p > 'z')
1188 && (*p < 'A' || *p > 'Z')))
1191 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1192 if (toktype == ERROR)
1194 char *err_copy = (char *) alloca (p - tokstart + 1);
1196 bcopy (tokstart, err_copy, p - tokstart);
1197 err_copy[p - tokstart] = 0;
1198 error ("Invalid number \"%s\".", err_copy);
1229 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1232 c = tokstart[++namelen];
1233 if (c >= '0' && c <= '9')
1235 c = tokstart[++namelen];
1236 if (c >= '0' && c <= '9')
1237 c = tokstart[++namelen];
1240 yylval.sval.ptr = tokstart + 1;
1241 yylval.sval.length = namelen - 1;
1242 lexptr += namelen + 1;
1246 if (!(c == '_' || c == '$'
1247 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1248 /* We must have come across a bad character (e.g. ';'). */
1249 error ("Invalid character '%c' in expression.", c);
1251 /* It's a name. See how long it is. */
1253 for (c = tokstart[namelen];
1254 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1255 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1256 c = tokstart[++namelen])
1259 /* The token "if" terminates the expression and is NOT
1260 removed from the input stream. */
1261 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1268 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1269 and $$digits (equivalent to $<-digits> if you could type that).
1270 Make token type LAST, and put the number (the digits) in yylval. */
1272 if (*tokstart == '$')
1274 register int negate = 0;
1276 /* Double dollar means negate the number and add -1 as well.
1277 Thus $$ alone means -1. */
1278 if (namelen >= 2 && tokstart[1] == '$')
1285 /* Just dollars (one or two) */
1286 yylval.lval = - negate;
1289 /* Is the rest of the token digits? */
1290 for (; c < namelen; c++)
1291 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1295 yylval.lval = atoi (tokstart + 1 + negate);
1297 yylval.lval = - yylval.lval;
1302 /* Handle tokens that refer to machine registers:
1303 $ followed by a register name. */
1305 if (*tokstart == '$') {
1306 for (c = 0; c < NUM_REGS; c++)
1307 if (namelen - 1 == strlen (reg_names[c])
1308 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1313 for (c = 0; c < num_std_regs; c++)
1314 if (namelen - 1 == strlen (std_regs[c].name)
1315 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1317 yylval.lval = std_regs[c].regnum;
1321 /* Catch specific keywords. Should be done with a data structure. */
1325 if (!strncmp (tokstart, "unsigned", 8))
1327 if (current_language->la_language == language_cplus
1328 && !strncmp (tokstart, "template", 8))
1332 if (!strncmp (tokstart, "struct", 6))
1334 if (!strncmp (tokstart, "signed", 6))
1335 return SIGNED_KEYWORD;
1336 if (!strncmp (tokstart, "sizeof", 6))
1340 if (!strncmp (tokstart, "union", 5))
1342 if (!strncmp (tokstart, "short", 5))
1346 if (!strncmp (tokstart, "enum", 4))
1348 if (!strncmp (tokstart, "long", 4))
1350 if (current_language->la_language == language_cplus
1351 && !strncmp (tokstart, "this", 4))
1353 static const char this_name[] =
1354 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1356 if (lookup_symbol (this_name, expression_context_block,
1357 VAR_NAMESPACE, 0, NULL))
1362 if (!strncmp (tokstart, "int", 3))
1369 yylval.sval.ptr = tokstart;
1370 yylval.sval.length = namelen;
1372 /* Any other names starting in $ are debugger internal variables. */
1374 if (*tokstart == '$')
1376 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1380 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1381 functions or symtabs. If this is not so, then ...
1382 Use token-type TYPENAME for symbols that happen to be defined
1383 currently as names of types; NAME for other symbols.
1384 The caller is not constrained to care about the distinction. */
1386 char *tmp = copy_name (yylval.sval);
1388 int is_a_field_of_this = 0;
1391 sym = lookup_symbol (tmp, expression_context_block,
1393 current_language->la_language == language_cplus
1394 ? &is_a_field_of_this : NULL,
1396 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1397 lookup_partial_symtab (tmp))
1399 yylval.ssym.sym = sym;
1400 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1403 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1405 yylval.tsym.type = SYMBOL_TYPE (sym);
1408 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1411 /* Input names that aren't symbols but ARE valid hex numbers,
1412 when the input radix permits them, can be names or numbers
1413 depending on the parse. Note we support radixes > 16 here. */
1415 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1416 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1418 YYSTYPE newlval; /* Its value is ignored. */
1419 hextype = parse_number (tokstart, namelen, 0, &newlval);
1422 yylval.ssym.sym = sym;
1423 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1426 if (hextype == UINT)
1428 yylval.ssym.sym = sym;
1429 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1430 return NAME_OR_UINT;
1434 /* Any other kind of symbol */
1435 yylval.ssym.sym = sym;
1436 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1445 error (msg ? msg : "Invalid syntax in expression.");
1448 /* Table mapping opcodes into strings for printing operators
1449 and precedences of the operators. */
1451 const static struct op_print c_op_print_tab[] =
1453 {",", BINOP_COMMA, PREC_COMMA, 0},
1454 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1455 {"||", BINOP_OR, PREC_OR, 0},
1456 {"&&", BINOP_AND, PREC_AND, 0},
1457 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1458 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1459 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1460 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1461 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1462 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1463 {">=", BINOP_GEQ, PREC_ORDER, 0},
1464 {">", BINOP_GTR, PREC_ORDER, 0},
1465 {"<", BINOP_LESS, PREC_ORDER, 0},
1466 {">>", BINOP_RSH, PREC_SHIFT, 0},
1467 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1468 {"+", BINOP_ADD, PREC_ADD, 0},
1469 {"-", BINOP_SUB, PREC_ADD, 0},
1470 {"*", BINOP_MUL, PREC_MUL, 0},
1471 {"/", BINOP_DIV, PREC_MUL, 0},
1472 {"%", BINOP_REM, PREC_MUL, 0},
1473 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1474 {"-", UNOP_NEG, PREC_PREFIX, 0},
1475 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1476 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1477 {"*", UNOP_IND, PREC_PREFIX, 0},
1478 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1479 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1480 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1481 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1483 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1486 /* These variables point to the objects
1487 representing the predefined C data types. */
1489 struct type *builtin_type_void;
1490 struct type *builtin_type_char;
1491 struct type *builtin_type_short;
1492 struct type *builtin_type_int;
1493 struct type *builtin_type_long;
1494 struct type *builtin_type_long_long;
1495 struct type *builtin_type_signed_char;
1496 struct type *builtin_type_unsigned_char;
1497 struct type *builtin_type_unsigned_short;
1498 struct type *builtin_type_unsigned_int;
1499 struct type *builtin_type_unsigned_long;
1500 struct type *builtin_type_unsigned_long_long;
1501 struct type *builtin_type_float;
1502 struct type *builtin_type_double;
1503 struct type *builtin_type_long_double;
1504 struct type *builtin_type_complex;
1505 struct type *builtin_type_double_complex;
1507 struct type ** const (c_builtin_types[]) =
1511 &builtin_type_short,
1513 &builtin_type_float,
1514 &builtin_type_double,
1516 &builtin_type_long_long,
1517 &builtin_type_signed_char,
1518 &builtin_type_unsigned_char,
1519 &builtin_type_unsigned_short,
1520 &builtin_type_unsigned_int,
1521 &builtin_type_unsigned_long,
1522 &builtin_type_unsigned_long_long,
1523 &builtin_type_long_double,
1524 &builtin_type_complex,
1525 &builtin_type_double_complex,
1529 const struct language_defn c_language_defn = {
1530 "c", /* Language name */
1537 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1538 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1539 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1540 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1541 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1542 c_op_print_tab, /* expression operators for printing */
1546 const struct language_defn cplus_language_defn = {
1547 "c++", /* Language name */
1554 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1555 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1556 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1557 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1558 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1559 c_op_print_tab, /* expression operators for printing */
1564 _initialize_c_exp ()
1567 init_type (TYPE_CODE_VOID, 1,
1569 "void", (struct objfile *) NULL);
1571 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1573 "char", (struct objfile *) NULL);
1574 builtin_type_signed_char =
1575 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1577 "signed char", (struct objfile *) NULL);
1578 builtin_type_unsigned_char =
1579 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1581 "unsigned char", (struct objfile *) NULL);
1582 builtin_type_short =
1583 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1585 "short", (struct objfile *) NULL);
1586 builtin_type_unsigned_short =
1587 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1589 "unsigned short", (struct objfile *) NULL);
1591 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1593 "int", (struct objfile *) NULL);
1594 builtin_type_unsigned_int =
1595 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1597 "unsigned int", (struct objfile *) NULL);
1599 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1601 "long", (struct objfile *) NULL);
1602 builtin_type_unsigned_long =
1603 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1605 "unsigned long", (struct objfile *) NULL);
1606 builtin_type_long_long =
1607 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1609 "long long", (struct objfile *) NULL);
1610 builtin_type_unsigned_long_long =
1611 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1613 "unsigned long long", (struct objfile *) NULL);
1614 builtin_type_float =
1615 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1617 "float", (struct objfile *) NULL);
1618 builtin_type_double =
1619 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1621 "double", (struct objfile *) NULL);
1622 builtin_type_long_double =
1623 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1625 "long double", (struct objfile *) NULL);
1626 builtin_type_complex =
1627 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1629 "complex", (struct objfile *) NULL);
1630 builtin_type_double_complex =
1631 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1633 "double complex", (struct objfile *) NULL);
1635 add_language (&c_language_defn);
1636 add_language (&cplus_language_defn);