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.
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator. */
45 #include "expression.h"
46 #include "parser-defs.h"
53 /* These MUST be included in any grammar file!!!! Please choose unique names!
54 Note that this are a combined list of variables that can be produced
55 by any one of bison, byacc, or yacc. */
56 #define yymaxdepth c_maxdepth
57 #define yyparse c_parse
59 #define yyerror c_error
62 #define yydebug c_debug
71 #define yyerrflag c_errflag
72 #define yynerrs c_nerrs
77 #define yystate c_state
83 #define yyss c_yyss /* byacc */
84 #define yyssp c_yysp /* byacc */
85 #define yyvs c_yyvs /* byacc */
86 #define yyvsp c_yyvsp /* byacc */
89 yyparse PARAMS ((void));
92 yylex PARAMS ((void));
95 yyerror PARAMS ((char *));
97 /* #define YYDEBUG 1 */
101 /* Although the yacc "value" of an expression is not used,
102 since the result is stored in the structure being created,
103 other node types do have values. */
108 unsigned LONGEST ulval;
118 struct symtoken ssym;
121 enum exp_opcode opcode;
122 struct internalvar *ivar;
129 /* YYSTYPE gets defined by %union */
131 parse_number PARAMS ((char *, int, int, YYSTYPE *));
134 %type <voidval> exp exp1 type_exp start variable qualified_name
135 %type <tval> type typebase
136 %type <tvec> nonempty_typelist
137 /* %type <bval> block */
139 /* Fancy type parsing. */
140 %type <voidval> func_mod direct_abs_decl abs_decl
142 %type <lval> array_mod
144 %token <typed_val> INT
147 /* Both NAME and TYPENAME tokens represent symbols in the input,
148 and both convey their data as strings.
149 But a TYPENAME is a string that happens to be defined as a typedef
150 or builtin type name (such as int or char)
151 and a NAME is any other symbol.
152 Contexts where this distinction is not important can use the
153 nonterminal "name", which matches either NAME or TYPENAME. */
156 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
157 %token <tsym> TYPENAME
159 %type <ssym> name_not_typename
160 %type <tsym> typename
162 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
163 but which would parse as a valid number in the current input radix.
164 E.g. "c" when input_radix==16. Depending on the parse, it will be
165 turned into a name or into a number. */
167 %token <ssym> NAME_OR_INT
169 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
173 /* Special type cases, put in to allow the parser to distinguish different
175 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD
176 %token <lval> LAST REGNAME
178 %token <ivar> VARIABLE
180 %token <opcode> ASSIGN_MODIFY
187 %right '=' ASSIGN_MODIFY
195 %left '<' '>' LEQ GEQ
200 %right UNARY INCREMENT DECREMENT
201 %right ARROW '.' '[' '('
202 %token <ssym> BLOCKNAME
214 { write_exp_elt_opcode(OP_TYPE);
215 write_exp_elt_type($1);
216 write_exp_elt_opcode(OP_TYPE);}
219 /* Expressions, including the comma operator. */
222 { write_exp_elt_opcode (BINOP_COMMA); }
225 /* Expressions, not including the comma operator. */
226 exp : '*' exp %prec UNARY
227 { write_exp_elt_opcode (UNOP_IND); }
229 exp : '&' exp %prec UNARY
230 { write_exp_elt_opcode (UNOP_ADDR); }
232 exp : '-' exp %prec UNARY
233 { write_exp_elt_opcode (UNOP_NEG); }
236 exp : '!' exp %prec UNARY
237 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
240 exp : '~' exp %prec UNARY
241 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
244 exp : INCREMENT exp %prec UNARY
245 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
248 exp : DECREMENT exp %prec UNARY
249 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
252 exp : exp INCREMENT %prec UNARY
253 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
256 exp : exp DECREMENT %prec UNARY
257 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
260 exp : SIZEOF exp %prec UNARY
261 { write_exp_elt_opcode (UNOP_SIZEOF); }
265 { write_exp_elt_opcode (STRUCTOP_PTR);
266 write_exp_string ($3);
267 write_exp_elt_opcode (STRUCTOP_PTR); }
270 exp : exp ARROW qualified_name
271 { /* exp->type::name becomes exp->*(&type::name) */
272 /* Note: this doesn't work if name is a
273 static member! FIXME */
274 write_exp_elt_opcode (UNOP_ADDR);
275 write_exp_elt_opcode (STRUCTOP_MPTR); }
277 exp : exp ARROW '*' exp
278 { write_exp_elt_opcode (STRUCTOP_MPTR); }
282 { write_exp_elt_opcode (STRUCTOP_STRUCT);
283 write_exp_string ($3);
284 write_exp_elt_opcode (STRUCTOP_STRUCT); }
287 exp : exp '.' qualified_name
288 { /* exp.type::name becomes exp.*(&type::name) */
289 /* Note: this doesn't work if name is a
290 static member! FIXME */
291 write_exp_elt_opcode (UNOP_ADDR);
292 write_exp_elt_opcode (STRUCTOP_MEMBER); }
295 exp : exp '.' '*' exp
296 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
299 exp : exp '[' exp1 ']'
300 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
304 /* This is to save the value of arglist_len
305 being accumulated by an outer function call. */
306 { start_arglist (); }
307 arglist ')' %prec ARROW
308 { write_exp_elt_opcode (OP_FUNCALL);
309 write_exp_elt_longcst ((LONGEST) end_arglist ());
310 write_exp_elt_opcode (OP_FUNCALL); }
320 arglist : arglist ',' exp %prec ABOVE_COMMA
324 exp : '{' type '}' exp %prec UNARY
325 { write_exp_elt_opcode (UNOP_MEMVAL);
326 write_exp_elt_type ($2);
327 write_exp_elt_opcode (UNOP_MEMVAL); }
330 exp : '(' type ')' exp %prec UNARY
331 { write_exp_elt_opcode (UNOP_CAST);
332 write_exp_elt_type ($2);
333 write_exp_elt_opcode (UNOP_CAST); }
340 /* Binary operators in order of decreasing precedence. */
343 { write_exp_elt_opcode (BINOP_REPEAT); }
347 { write_exp_elt_opcode (BINOP_MUL); }
351 { write_exp_elt_opcode (BINOP_DIV); }
355 { write_exp_elt_opcode (BINOP_REM); }
359 { write_exp_elt_opcode (BINOP_ADD); }
363 { write_exp_elt_opcode (BINOP_SUB); }
367 { write_exp_elt_opcode (BINOP_LSH); }
371 { write_exp_elt_opcode (BINOP_RSH); }
375 { write_exp_elt_opcode (BINOP_EQUAL); }
378 exp : exp NOTEQUAL exp
379 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
383 { write_exp_elt_opcode (BINOP_LEQ); }
387 { write_exp_elt_opcode (BINOP_GEQ); }
391 { write_exp_elt_opcode (BINOP_LESS); }
395 { write_exp_elt_opcode (BINOP_GTR); }
399 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
403 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
407 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
411 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
415 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
418 exp : exp '?' exp ':' exp %prec '?'
419 { write_exp_elt_opcode (TERNOP_COND); }
423 { write_exp_elt_opcode (BINOP_ASSIGN); }
426 exp : exp ASSIGN_MODIFY exp
427 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
428 write_exp_elt_opcode ($2);
429 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
433 { write_exp_elt_opcode (OP_LONG);
434 write_exp_elt_type ($1.type);
435 write_exp_elt_longcst ((LONGEST)($1.val));
436 write_exp_elt_opcode (OP_LONG); }
441 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
442 write_exp_elt_opcode (OP_LONG);
443 write_exp_elt_type (val.typed_val.type);
444 write_exp_elt_longcst ((LONGEST)val.typed_val.val);
445 write_exp_elt_opcode (OP_LONG);
451 { write_exp_elt_opcode (OP_DOUBLE);
452 write_exp_elt_type (builtin_type_double);
453 write_exp_elt_dblcst ($1);
454 write_exp_elt_opcode (OP_DOUBLE); }
461 { write_exp_elt_opcode (OP_LAST);
462 write_exp_elt_longcst ((LONGEST) $1);
463 write_exp_elt_opcode (OP_LAST); }
467 { write_exp_elt_opcode (OP_REGISTER);
468 write_exp_elt_longcst ((LONGEST) $1);
469 write_exp_elt_opcode (OP_REGISTER); }
473 { write_exp_elt_opcode (OP_INTERNALVAR);
474 write_exp_elt_intern ($1);
475 write_exp_elt_opcode (OP_INTERNALVAR); }
478 exp : SIZEOF '(' type ')' %prec UNARY
479 { write_exp_elt_opcode (OP_LONG);
480 write_exp_elt_type (builtin_type_int);
481 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
482 write_exp_elt_opcode (OP_LONG); }
486 { write_exp_elt_opcode (OP_STRING);
487 write_exp_string ($1);
488 write_exp_elt_opcode (OP_STRING); }
493 { write_exp_elt_opcode (OP_THIS);
494 write_exp_elt_opcode (OP_THIS); }
502 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
506 lookup_symtab (copy_name ($1.stoken));
508 $$ = BLOCKVECTOR_BLOCK
509 (BLOCKVECTOR (tem), STATIC_BLOCK);
511 error ("No file or function \"%s\".",
512 copy_name ($1.stoken));
517 block : block COLONCOLON name
519 = lookup_symbol (copy_name ($3), $1,
520 VAR_NAMESPACE, 0, NULL);
521 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
522 error ("No function \"%s\" in specified context.",
524 $$ = SYMBOL_BLOCK_VALUE (tem); }
527 variable: block COLONCOLON name
528 { struct symbol *sym;
529 sym = lookup_symbol (copy_name ($3), $1,
530 VAR_NAMESPACE, 0, NULL);
532 error ("No symbol \"%s\" in specified context.",
535 write_exp_elt_opcode (OP_VAR_VALUE);
536 write_exp_elt_sym (sym);
537 write_exp_elt_opcode (OP_VAR_VALUE); }
540 qualified_name: typebase COLONCOLON name
542 struct type *type = $1;
543 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
544 && TYPE_CODE (type) != TYPE_CODE_UNION)
545 error ("`%s' is not defined as an aggregate type.",
548 write_exp_elt_opcode (OP_SCOPE);
549 write_exp_elt_type (type);
550 write_exp_string ($3);
551 write_exp_elt_opcode (OP_SCOPE);
553 | typebase COLONCOLON '~' name
555 struct type *type = $1;
556 struct stoken tmp_token;
557 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
558 && TYPE_CODE (type) != TYPE_CODE_UNION)
559 error ("`%s' is not defined as an aggregate type.",
562 if (strcmp (type_name_no_tag (type), $4.ptr))
563 error ("invalid destructor `%s::~%s'",
564 type_name_no_tag (type), $4.ptr);
566 tmp_token.ptr = (char*) alloca ($4.length + 2);
567 tmp_token.length = $4.length + 1;
568 tmp_token.ptr[0] = '~';
569 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
570 tmp_token.ptr[tmp_token.length] = 0;
571 write_exp_elt_opcode (OP_SCOPE);
572 write_exp_elt_type (type);
573 write_exp_string (tmp_token);
574 write_exp_elt_opcode (OP_SCOPE);
578 variable: qualified_name
581 char *name = copy_name ($2);
583 struct minimal_symbol *msymbol;
586 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
589 write_exp_elt_opcode (OP_VAR_VALUE);
590 write_exp_elt_sym (sym);
591 write_exp_elt_opcode (OP_VAR_VALUE);
595 msymbol = lookup_minimal_symbol (name,
596 (struct objfile *) NULL);
599 write_exp_elt_opcode (OP_LONG);
600 write_exp_elt_type (builtin_type_int);
601 write_exp_elt_longcst ((LONGEST) msymbol -> address);
602 write_exp_elt_opcode (OP_LONG);
603 write_exp_elt_opcode (UNOP_MEMVAL);
604 if (msymbol -> type == mst_data ||
605 msymbol -> type == mst_bss)
606 write_exp_elt_type (builtin_type_int);
607 else if (msymbol -> type == mst_text)
608 write_exp_elt_type (lookup_function_type (builtin_type_int));
610 write_exp_elt_type (builtin_type_char);
611 write_exp_elt_opcode (UNOP_MEMVAL);
614 if (!have_full_symbols () && !have_partial_symbols ())
615 error ("No symbol table is loaded. Use the \"file\" command.");
617 error ("No symbol \"%s\" in current context.", name);
621 variable: name_not_typename
622 { struct symbol *sym = $1.sym;
626 switch (SYMBOL_CLASS (sym))
634 if (innermost_block == 0 ||
635 contained_in (block_found,
637 innermost_block = block_found;
644 case LOC_CONST_BYTES:
646 /* In this case the expression can
647 be evaluated regardless of what
648 frame we are in, so there is no
649 need to check for the
650 innermost_block. These cases are
651 listed so that gcc -Wall will
652 report types that may not have
657 write_exp_elt_opcode (OP_VAR_VALUE);
658 write_exp_elt_sym (sym);
659 write_exp_elt_opcode (OP_VAR_VALUE);
661 else if ($1.is_a_field_of_this)
663 /* C++: it hangs off of `this'. Must
664 not inadvertently convert from a method call
666 if (innermost_block == 0 ||
667 contained_in (block_found, innermost_block))
668 innermost_block = block_found;
669 write_exp_elt_opcode (OP_THIS);
670 write_exp_elt_opcode (OP_THIS);
671 write_exp_elt_opcode (STRUCTOP_PTR);
672 write_exp_string ($1.stoken);
673 write_exp_elt_opcode (STRUCTOP_PTR);
677 struct minimal_symbol *msymbol;
678 register char *arg = copy_name ($1.stoken);
680 msymbol = lookup_minimal_symbol (arg,
681 (struct objfile *) NULL);
684 write_exp_elt_opcode (OP_LONG);
685 write_exp_elt_type (builtin_type_int);
686 write_exp_elt_longcst ((LONGEST) msymbol -> address);
687 write_exp_elt_opcode (OP_LONG);
688 write_exp_elt_opcode (UNOP_MEMVAL);
689 if (msymbol -> type == mst_data ||
690 msymbol -> type == mst_bss)
691 write_exp_elt_type (builtin_type_int);
692 else if (msymbol -> type == mst_text)
693 write_exp_elt_type (lookup_function_type (builtin_type_int));
695 write_exp_elt_type (builtin_type_char);
696 write_exp_elt_opcode (UNOP_MEMVAL);
698 else if (!have_full_symbols () && !have_partial_symbols ())
699 error ("No symbol table is loaded. Use the \"file\" command.");
701 error ("No symbol \"%s\" in current context.",
702 copy_name ($1.stoken));
711 /* This is where the interesting stuff happens. */
714 struct type *follow_type = $1;
723 follow_type = lookup_pointer_type (follow_type);
726 follow_type = lookup_reference_type (follow_type);
729 array_size = pop_type_int ();
730 if (array_size != -1)
731 follow_type = create_array_type (follow_type,
734 follow_type = lookup_pointer_type (follow_type);
737 follow_type = lookup_function_type (follow_type);
745 { push_type (tp_pointer); $$ = 0; }
747 { push_type (tp_pointer); $$ = $2; }
749 { push_type (tp_reference); $$ = 0; }
751 { push_type (tp_reference); $$ = $2; }
755 direct_abs_decl: '(' abs_decl ')'
757 | direct_abs_decl array_mod
760 push_type (tp_array);
765 push_type (tp_array);
768 | direct_abs_decl func_mod
769 { push_type (tp_function); }
771 { push_type (tp_function); }
782 | '(' nonempty_typelist ')'
783 { free ((PTR)$2); $$ = 0; }
787 | typebase COLONCOLON '*'
788 { $$ = lookup_member_type (builtin_type_int, $1); }
789 | type '(' typebase COLONCOLON '*' ')'
790 { $$ = lookup_member_type ($1, $3); }
791 | type '(' typebase COLONCOLON '*' ')' '(' ')'
792 { $$ = lookup_member_type
793 (lookup_function_type ($1), $3); }
794 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
795 { $$ = lookup_member_type
796 (lookup_function_type ($1), $3);
800 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
804 { $$ = builtin_type_int; }
806 { $$ = builtin_type_long; }
808 { $$ = builtin_type_short; }
810 { $$ = builtin_type_long; }
811 | UNSIGNED LONG INT_KEYWORD
812 { $$ = builtin_type_unsigned_long; }
814 { $$ = builtin_type_long_long; }
815 | LONG LONG INT_KEYWORD
816 { $$ = builtin_type_long_long; }
818 { $$ = builtin_type_unsigned_long_long; }
819 | UNSIGNED LONG LONG INT_KEYWORD
820 { $$ = builtin_type_unsigned_long_long; }
822 { $$ = builtin_type_short; }
823 | UNSIGNED SHORT INT_KEYWORD
824 { $$ = builtin_type_unsigned_short; }
826 { $$ = lookup_struct (copy_name ($2),
827 expression_context_block); }
829 { $$ = lookup_struct (copy_name ($2),
830 expression_context_block); }
832 { $$ = lookup_union (copy_name ($2),
833 expression_context_block); }
835 { $$ = lookup_enum (copy_name ($2),
836 expression_context_block); }
838 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
840 { $$ = builtin_type_unsigned_int; }
841 | SIGNED_KEYWORD typename
842 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
844 { $$ = builtin_type_int; }
845 | TEMPLATE name '<' type '>'
846 { $$ = lookup_template_type(copy_name($2), $4,
847 expression_context_block);
849 /* "const" and "volatile" are curently ignored. */
850 | CONST_KEYWORD typebase { $$ = $2; }
851 | VOLATILE_KEYWORD typebase { $$ = $2; }
857 $$.stoken.ptr = "int";
858 $$.stoken.length = 3;
859 $$.type = builtin_type_int;
863 $$.stoken.ptr = "long";
864 $$.stoken.length = 4;
865 $$.type = builtin_type_long;
869 $$.stoken.ptr = "short";
870 $$.stoken.length = 5;
871 $$.type = builtin_type_short;
877 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
878 $<ivec>$[0] = 1; /* Number of types in vector */
881 | nonempty_typelist ',' type
882 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
883 $$ = (struct type **) realloc ((char *) $1, len);
884 $$[$<ivec>$[0]] = $3;
888 name : NAME { $$ = $1.stoken; }
889 | BLOCKNAME { $$ = $1.stoken; }
890 | TYPENAME { $$ = $1.stoken; }
891 | NAME_OR_INT { $$ = $1.stoken; }
894 name_not_typename : NAME
896 /* These would be useful if name_not_typename was useful, but it is just
897 a fake for "variable", so these cause reduce/reduce conflicts because
898 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
899 =exp) or just an exp. If name_not_typename was ever used in an lvalue
900 context where only a name could occur, this might be useful.
907 /* Take care of parsing a number (anything that starts with a digit).
908 Set yylval and return the token type; update lexptr.
909 LEN is the number of characters in it. */
911 /*** Needs some error checking for the float case ***/
914 parse_number (p, len, parsed_float, putithere)
920 register LONGEST n = 0;
921 register LONGEST prevn = 0;
924 register int base = input_radix;
928 struct type *signed_type;
929 struct type *unsigned_type;
933 /* It's a float since it contains a point or an exponent. */
934 putithere->dval = atof (p);
938 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
972 if (c >= 'A' && c <= 'Z')
974 if (c != 'l' && c != 'u')
976 if (c >= '0' && c <= '9')
980 if (base > 10 && c >= 'a' && c <= 'f')
981 n += i = c - 'a' + 10;
982 else if (len == 0 && c == 'l')
984 else if (len == 0 && c == 'u')
987 return ERROR; /* Char not a digit */
990 return ERROR; /* Invalid digit in this base */
992 /* Portably test for overflow (only works for nonzero values, so make
993 a second check for zero). */
994 if((prevn >= n) && n != 0)
995 unsigned_p=1; /* Try something unsigned */
996 /* If range checking enabled, portably test for unsigned overflow. */
997 if(RANGE_CHECK && n!=0)
999 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
1000 range_error("Overflow on numeric constant.");
1005 /* If the number is too big to be an int, or it's got an l suffix
1006 then it's a long. Work out if this has to be a long by
1007 shifting right and and seeing if anything remains, and the
1008 target int size is different to the target long size. */
1010 if ((TARGET_INT_BIT != TARGET_LONG_BIT && (n >> TARGET_INT_BIT)) || long_p)
1012 high_bit = ((LONGEST)1) << (TARGET_LONG_BIT-1);
1013 unsigned_type = builtin_type_unsigned_long;
1014 signed_type = builtin_type_long;
1018 high_bit = ((LONGEST)1) << (TARGET_INT_BIT-1);
1019 unsigned_type = builtin_type_unsigned_int;
1020 signed_type = builtin_type_int;
1023 putithere->typed_val.val = n;
1025 /* If the high bit of the worked out type is set then this number
1026 has to be unsigned. */
1028 if (unsigned_p || (n & high_bit))
1030 putithere->typed_val.type = unsigned_type;
1034 putithere->typed_val.type = signed_type;
1044 enum exp_opcode opcode;
1047 const static struct token tokentab3[] =
1049 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1050 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1053 const static struct token tokentab2[] =
1055 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1056 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1057 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1058 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1059 {"%=", ASSIGN_MODIFY, BINOP_REM},
1060 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1061 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1062 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1063 {"++", INCREMENT, BINOP_END},
1064 {"--", DECREMENT, BINOP_END},
1065 {"->", ARROW, BINOP_END},
1066 {"&&", ANDAND, BINOP_END},
1067 {"||", OROR, BINOP_END},
1068 {"::", COLONCOLON, BINOP_END},
1069 {"<<", LSH, BINOP_END},
1070 {">>", RSH, BINOP_END},
1071 {"==", EQUAL, BINOP_END},
1072 {"!=", NOTEQUAL, BINOP_END},
1073 {"<=", LEQ, BINOP_END},
1074 {">=", GEQ, BINOP_END}
1077 /* Read one token, getting characters through lexptr. */
1088 static char *tempbuf;
1089 static int tempbufsize;
1094 /* See if it is a special token of length 3. */
1095 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1096 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1099 yylval.opcode = tokentab3[i].opcode;
1100 return tokentab3[i].token;
1103 /* See if it is a special token of length 2. */
1104 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1105 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1108 yylval.opcode = tokentab2[i].opcode;
1109 return tokentab2[i].token;
1112 switch (c = *tokstart)
1124 /* We either have a character constant ('0' or '\177' for example)
1125 or we have a quoted symbol reference ('foo(int,int)' in C++
1130 c = parse_escape (&lexptr);
1132 yylval.typed_val.val = c;
1133 yylval.typed_val.type = builtin_type_char;
1138 namelen = skip_quoted (tokstart) - tokstart;
1141 lexptr = tokstart + namelen;
1146 error ("Invalid character constant.");
1156 if (paren_depth == 0)
1163 if (comma_terminates && paren_depth == 0)
1169 /* Might be a floating point number. */
1170 if (lexptr[1] < '0' || lexptr[1] > '9')
1171 goto symbol; /* Nope, must be a symbol. */
1172 /* FALL THRU into number case. */
1185 /* It's a number. */
1186 int got_dot = 0, got_e = 0, toktype;
1187 register char *p = tokstart;
1188 int hex = input_radix > 10;
1190 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1195 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1203 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1204 got_dot = got_e = 1;
1205 else if (!hex && !got_dot && *p == '.')
1207 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1208 && (*p == '-' || *p == '+'))
1209 /* This is the sign of the exponent, not the end of the
1212 /* We will take any letters or digits. parse_number will
1213 complain if past the radix, or if L or U are not final. */
1214 else if ((*p < '0' || *p > '9')
1215 && ((*p < 'a' || *p > 'z')
1216 && (*p < 'A' || *p > 'Z')))
1219 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1220 if (toktype == ERROR)
1222 char *err_copy = (char *) alloca (p - tokstart + 1);
1224 memcpy (err_copy, tokstart, p - tokstart);
1225 err_copy[p - tokstart] = 0;
1226 error ("Invalid number \"%s\".", err_copy);
1258 /* Build the gdb internal form of the input string in tempbuf,
1259 translating any standard C escape forms seen. Note that the
1260 buffer is null byte terminated *only* for the convenience of
1261 debugging gdb itself and printing the buffer contents when
1262 the buffer contains no embedded nulls. Gdb does not depend
1263 upon the buffer being null byte terminated, it uses the length
1264 string instead. This allows gdb to handle C strings (as well
1265 as strings in other languages) with embedded null bytes */
1267 tokptr = ++tokstart;
1271 /* Grow the static temp buffer if necessary, including allocating
1272 the first one on demand. */
1273 if (tempbufindex + 1 >= tempbufsize)
1275 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1281 /* Do nothing, loop will terminate. */
1285 c = parse_escape (&tokptr);
1290 tempbuf[tempbufindex++] = c;
1293 tempbuf[tempbufindex++] = *tokptr++;
1296 } while ((*tokptr != '"') && (*tokptr != '\0'));
1297 if (*tokptr++ != '"')
1299 error ("Unterminated string in expression.");
1301 tempbuf[tempbufindex] = '\0'; /* See note above */
1302 yylval.sval.ptr = tempbuf;
1303 yylval.sval.length = tempbufindex;
1308 if (!(c == '_' || c == '$'
1309 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1310 /* We must have come across a bad character (e.g. ';'). */
1311 error ("Invalid character '%c' in expression.", c);
1313 /* It's a name. See how long it is. */
1315 for (c = tokstart[namelen];
1316 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1317 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1318 c = tokstart[++namelen])
1321 /* The token "if" terminates the expression and is NOT
1322 removed from the input stream. */
1323 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1330 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1331 and $$digits (equivalent to $<-digits> if you could type that).
1332 Make token type LAST, and put the number (the digits) in yylval. */
1335 if (*tokstart == '$')
1337 register int negate = 0;
1339 /* Double dollar means negate the number and add -1 as well.
1340 Thus $$ alone means -1. */
1341 if (namelen >= 2 && tokstart[1] == '$')
1348 /* Just dollars (one or two) */
1349 yylval.lval = - negate;
1352 /* Is the rest of the token digits? */
1353 for (; c < namelen; c++)
1354 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1358 yylval.lval = atoi (tokstart + 1 + negate);
1360 yylval.lval = - yylval.lval;
1365 /* Handle tokens that refer to machine registers:
1366 $ followed by a register name. */
1368 if (*tokstart == '$') {
1369 for (c = 0; c < NUM_REGS; c++)
1370 if (namelen - 1 == strlen (reg_names[c])
1371 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1376 for (c = 0; c < num_std_regs; c++)
1377 if (namelen - 1 == strlen (std_regs[c].name)
1378 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1380 yylval.lval = std_regs[c].regnum;
1384 /* Catch specific keywords. Should be done with a data structure. */
1388 if (!strncmp (tokstart, "unsigned", 8))
1390 if (current_language->la_language == language_cplus
1391 && !strncmp (tokstart, "template", 8))
1393 if (!strncmp (tokstart, "volatile", 8))
1394 return VOLATILE_KEYWORD;
1397 if (!strncmp (tokstart, "struct", 6))
1399 if (!strncmp (tokstart, "signed", 6))
1400 return SIGNED_KEYWORD;
1401 if (!strncmp (tokstart, "sizeof", 6))
1405 if (current_language->la_language == language_cplus
1406 && !strncmp (tokstart, "class", 5))
1408 if (!strncmp (tokstart, "union", 5))
1410 if (!strncmp (tokstart, "short", 5))
1412 if (!strncmp (tokstart, "const", 5))
1413 return CONST_KEYWORD;
1416 if (!strncmp (tokstart, "enum", 4))
1418 if (!strncmp (tokstart, "long", 4))
1420 if (current_language->la_language == language_cplus
1421 && !strncmp (tokstart, "this", 4))
1423 static const char this_name[] =
1424 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1426 if (lookup_symbol (this_name, expression_context_block,
1427 VAR_NAMESPACE, 0, NULL))
1432 if (!strncmp (tokstart, "int", 3))
1439 yylval.sval.ptr = tokstart;
1440 yylval.sval.length = namelen;
1442 /* Any other names starting in $ are debugger internal variables. */
1444 if (*tokstart == '$')
1446 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1450 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1451 functions or symtabs. If this is not so, then ...
1452 Use token-type TYPENAME for symbols that happen to be defined
1453 currently as names of types; NAME for other symbols.
1454 The caller is not constrained to care about the distinction. */
1456 char *tmp = copy_name (yylval.sval);
1458 int is_a_field_of_this = 0;
1461 sym = lookup_symbol (tmp, expression_context_block,
1463 current_language->la_language == language_cplus
1464 ? &is_a_field_of_this : NULL,
1466 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1467 lookup_partial_symtab (tmp))
1469 yylval.ssym.sym = sym;
1470 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1473 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1475 yylval.tsym.type = SYMBOL_TYPE (sym);
1478 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1481 /* Input names that aren't symbols but ARE valid hex numbers,
1482 when the input radix permits them, can be names or numbers
1483 depending on the parse. Note we support radixes > 16 here. */
1485 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1486 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1488 YYSTYPE newlval; /* Its value is ignored. */
1489 hextype = parse_number (tokstart, namelen, 0, &newlval);
1492 yylval.ssym.sym = sym;
1493 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1498 /* Any other kind of symbol */
1499 yylval.ssym.sym = sym;
1500 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1509 error (msg ? msg : "Invalid syntax in expression.");
1512 /* Print the character C on STREAM as part of the contents of a literal
1513 string whose delimiter is QUOTER. Note that that format for printing
1514 characters and strings is language specific. */
1517 emit_char (c, stream, quoter)
1523 c &= 0xFF; /* Avoid sign bit follies */
1525 if (PRINT_LITERAL_FORM (c))
1527 if (c == '\\' || c == quoter)
1529 fputs_filtered ("\\", stream);
1531 fprintf_filtered (stream, "%c", c);
1538 fputs_filtered ("\\n", stream);
1541 fputs_filtered ("\\b", stream);
1544 fputs_filtered ("\\t", stream);
1547 fputs_filtered ("\\f", stream);
1550 fputs_filtered ("\\r", stream);
1553 fputs_filtered ("\\e", stream);
1556 fputs_filtered ("\\a", stream);
1559 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
1566 c_printchar (c, stream)
1570 fputs_filtered ("'", stream);
1571 emit_char (c, stream, '\'');
1572 fputs_filtered ("'", stream);
1575 /* Print the character string STRING, printing at most LENGTH characters.
1576 Printing stops early if the number hits print_max; repeat counts
1577 are printed as appropriate. Print ellipses at the end if we
1578 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */
1581 c_printstr (stream, string, length, force_ellipses)
1584 unsigned int length;
1587 register unsigned int i;
1588 unsigned int things_printed = 0;
1591 extern int inspect_it;
1592 extern int repeat_count_threshold;
1593 extern int print_max;
1597 fputs_filtered ("\"\"", stdout);
1601 for (i = 0; i < length && things_printed < print_max; ++i)
1603 /* Position of the character we are examining
1604 to see whether it is repeated. */
1606 /* Number of repetitions we have detected so far. */
1613 fputs_filtered (", ", stream);
1619 while (rep1 < length && string[rep1] == string[i])
1625 if (reps > repeat_count_threshold)
1630 fputs_filtered ("\\\", ", stream);
1632 fputs_filtered ("\", ", stream);
1635 c_printchar (string[i], stream);
1636 fprintf_filtered (stream, " <repeats %u times>", reps);
1638 things_printed += repeat_count_threshold;
1646 fputs_filtered ("\\\"", stream);
1648 fputs_filtered ("\"", stream);
1651 emit_char (string[i], stream, '"');
1656 /* Terminate the quotes if necessary. */
1660 fputs_filtered ("\\\"", stream);
1662 fputs_filtered ("\"", stream);
1665 if (force_ellipses || i < length)
1666 fputs_filtered ("...", stream);
1670 /* Table mapping opcodes into strings for printing operators
1671 and precedences of the operators. */
1673 const static struct op_print c_op_print_tab[] =
1675 {",", BINOP_COMMA, PREC_COMMA, 0},
1676 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1677 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1678 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1679 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1680 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1681 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1682 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1683 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1684 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1685 {">=", BINOP_GEQ, PREC_ORDER, 0},
1686 {">", BINOP_GTR, PREC_ORDER, 0},
1687 {"<", BINOP_LESS, PREC_ORDER, 0},
1688 {">>", BINOP_RSH, PREC_SHIFT, 0},
1689 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1690 {"+", BINOP_ADD, PREC_ADD, 0},
1691 {"-", BINOP_SUB, PREC_ADD, 0},
1692 {"*", BINOP_MUL, PREC_MUL, 0},
1693 {"/", BINOP_DIV, PREC_MUL, 0},
1694 {"%", BINOP_REM, PREC_MUL, 0},
1695 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1696 {"-", UNOP_NEG, PREC_PREFIX, 0},
1697 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1698 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1699 {"*", UNOP_IND, PREC_PREFIX, 0},
1700 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1701 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1702 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1703 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1705 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1709 /* These variables point to the objects
1710 representing the predefined C data types. */
1712 struct type *builtin_type_void;
1713 struct type *builtin_type_char;
1714 struct type *builtin_type_short;
1715 struct type *builtin_type_int;
1716 struct type *builtin_type_long;
1717 struct type *builtin_type_long_long;
1718 struct type *builtin_type_signed_char;
1719 struct type *builtin_type_unsigned_char;
1720 struct type *builtin_type_unsigned_short;
1721 struct type *builtin_type_unsigned_int;
1722 struct type *builtin_type_unsigned_long;
1723 struct type *builtin_type_unsigned_long_long;
1724 struct type *builtin_type_float;
1725 struct type *builtin_type_double;
1726 struct type *builtin_type_long_double;
1727 struct type *builtin_type_complex;
1728 struct type *builtin_type_double_complex;
1730 struct type ** const (c_builtin_types[]) =
1734 &builtin_type_short,
1736 &builtin_type_float,
1737 &builtin_type_double,
1739 &builtin_type_long_long,
1740 &builtin_type_signed_char,
1741 &builtin_type_unsigned_char,
1742 &builtin_type_unsigned_short,
1743 &builtin_type_unsigned_int,
1744 &builtin_type_unsigned_long,
1745 &builtin_type_unsigned_long_long,
1746 &builtin_type_long_double,
1747 &builtin_type_complex,
1748 &builtin_type_double_complex,
1752 const struct language_defn c_language_defn = {
1753 "c", /* Language name */
1760 c_printchar, /* Print a character constant */
1761 c_printstr, /* Function to print string constant */
1762 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1763 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1764 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1765 {"", "", "", ""}, /* Binary format info */
1766 {"0%o", "0", "o", ""}, /* Octal format info */
1767 {"%d", "", "d", ""}, /* Decimal format info */
1768 {"0x%x", "0x", "x", ""}, /* Hex format info */
1769 c_op_print_tab, /* expression operators for printing */
1773 const struct language_defn cplus_language_defn = {
1774 "c++", /* Language name */
1781 c_printchar, /* Print a character constant */
1782 c_printstr, /* Function to print string constant */
1783 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1784 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1785 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1786 {"", "", "", ""}, /* Binary format info */
1787 {"0%o", "0", "o", ""}, /* Octal format info */
1788 {"%d", "", "d", ""}, /* Decimal format info */
1789 {"0x%x", "0x", "x", ""}, /* Hex format info */
1790 c_op_print_tab, /* expression operators for printing */
1795 _initialize_c_exp ()
1798 init_type (TYPE_CODE_VOID, 1,
1800 "void", (struct objfile *) NULL);
1802 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1804 "char", (struct objfile *) NULL);
1805 builtin_type_signed_char =
1806 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1808 "signed char", (struct objfile *) NULL);
1809 builtin_type_unsigned_char =
1810 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1812 "unsigned char", (struct objfile *) NULL);
1813 builtin_type_short =
1814 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1816 "short", (struct objfile *) NULL);
1817 builtin_type_unsigned_short =
1818 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1820 "unsigned short", (struct objfile *) NULL);
1822 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1824 "int", (struct objfile *) NULL);
1825 builtin_type_unsigned_int =
1826 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1828 "unsigned int", (struct objfile *) NULL);
1830 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1832 "long", (struct objfile *) NULL);
1833 builtin_type_unsigned_long =
1834 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1836 "unsigned long", (struct objfile *) NULL);
1837 builtin_type_long_long =
1838 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1840 "long long", (struct objfile *) NULL);
1841 builtin_type_unsigned_long_long =
1842 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1844 "unsigned long long", (struct objfile *) NULL);
1845 builtin_type_float =
1846 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1848 "float", (struct objfile *) NULL);
1849 builtin_type_double =
1850 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1852 "double", (struct objfile *) NULL);
1853 builtin_type_long_double =
1854 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1856 "long double", (struct objfile *) NULL);
1857 builtin_type_complex =
1858 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1860 "complex", (struct objfile *) NULL);
1861 builtin_type_double_complex =
1862 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1864 "double complex", (struct objfile *) NULL);
1866 add_language (&c_language_defn);
1867 add_language (&cplus_language_defn);