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"
45 /* These MUST be included in any grammar file!!!! Please choose unique names!
46 Note that this are a combined list of variables that can be produced
47 by any one of bison, byacc, or yacc. */
48 #define yymaxdepth c_maxdepth
49 #define yyparse c_parse
51 #define yyerror c_error
54 #define yydebug c_debug
63 #define yyerrflag c_errflag
64 #define yynerrs c_nerrs
69 #define yystate c_state
75 #define yyss c_yyss /* byacc */
76 #define yyssp c_yysp /* byacc */
77 #define yyvs c_yyvs /* byacc */
78 #define yyvsp c_yyvsp /* byacc */
81 yyparse PARAMS ((void));
84 yylex PARAMS ((void));
87 yyerror PARAMS ((char *));
89 /* #define YYDEBUG 1 */
93 /* Although the yacc "value" of an expression is not used,
94 since the result is stored in the structure being created,
95 other node types do have values. */
100 unsigned LONGEST ulval;
106 struct symtoken ssym;
109 enum exp_opcode opcode;
110 struct internalvar *ivar;
117 /* YYSTYPE gets defined by %union */
119 parse_number PARAMS ((char *, int, int, YYSTYPE *));
122 %type <voidval> exp exp1 type_exp start variable qualified_name
123 %type <tval> type typebase
124 %type <tvec> nonempty_typelist
125 /* %type <bval> block */
127 /* Fancy type parsing. */
128 %type <voidval> func_mod direct_abs_decl abs_decl
130 %type <lval> array_mod
132 %token <lval> INT CHAR
136 /* Both NAME and TYPENAME tokens represent symbols in the input,
137 and both convey their data as strings.
138 But a TYPENAME is a string that happens to be defined as a typedef
139 or builtin type name (such as int or char)
140 and a NAME is any other symbol.
141 Contexts where this distinction is not important can use the
142 nonterminal "name", which matches either NAME or TYPENAME. */
145 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
146 %token <tsym> TYPENAME
148 %type <ssym> name_not_typename
149 %type <tsym> typename
151 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
152 but which would parse as a valid number in the current input radix.
153 E.g. "c" when input_radix==16. Depending on the parse, it will be
154 turned into a name or into a number. NAME_OR_UINT ditto. */
156 %token <ssym> NAME_OR_INT NAME_OR_UINT
158 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
162 /* Special type cases, put in to allow the parser to distinguish different
164 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD
166 %token <lval> LAST REGNAME
168 %token <ivar> VARIABLE
170 %token <opcode> ASSIGN_MODIFY
177 %right '=' ASSIGN_MODIFY
185 %left '<' '>' LEQ GEQ
190 %right UNARY INCREMENT DECREMENT
191 %right ARROW '.' '[' '('
192 %token <ssym> BLOCKNAME
197 /* Ensure that if the generated parser contains any calls to malloc/realloc,
198 that they get mapped to xmalloc/xrealloc. We have to do this here
199 rather than earlier in the file because this is the first point after
200 the place where the SVR4 yacc includes <malloc.h>, and if we do it
201 before that, then the remapped declarations in <malloc.h> will collide
202 with the ones in "defs.h". */
204 #define malloc xmalloc
205 #define realloc xrealloc
216 { write_exp_elt_opcode(OP_TYPE);
217 write_exp_elt_type($1);
218 write_exp_elt_opcode(OP_TYPE);}
221 /* Expressions, including the comma operator. */
224 { write_exp_elt_opcode (BINOP_COMMA); }
227 /* Expressions, not including the comma operator. */
228 exp : '*' exp %prec UNARY
229 { write_exp_elt_opcode (UNOP_IND); }
231 exp : '&' exp %prec UNARY
232 { write_exp_elt_opcode (UNOP_ADDR); }
234 exp : '-' exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_NEG); }
238 exp : '!' exp %prec UNARY
239 { write_exp_elt_opcode (UNOP_ZEROP); }
242 exp : '~' exp %prec UNARY
243 { write_exp_elt_opcode (UNOP_LOGNOT); }
246 exp : INCREMENT exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
250 exp : DECREMENT exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
254 exp : exp INCREMENT %prec UNARY
255 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
258 exp : exp DECREMENT %prec UNARY
259 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
262 exp : SIZEOF exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_SIZEOF); }
267 { write_exp_elt_opcode (STRUCTOP_PTR);
268 write_exp_string ($3);
269 write_exp_elt_opcode (STRUCTOP_PTR); }
272 exp : exp ARROW qualified_name
273 { /* exp->type::name becomes exp->*(&type::name) */
274 /* Note: this doesn't work if name is a
275 static member! FIXME */
276 write_exp_elt_opcode (UNOP_ADDR);
277 write_exp_elt_opcode (STRUCTOP_MPTR); }
279 exp : exp ARROW '*' exp
280 { write_exp_elt_opcode (STRUCTOP_MPTR); }
284 { write_exp_elt_opcode (STRUCTOP_STRUCT);
285 write_exp_string ($3);
286 write_exp_elt_opcode (STRUCTOP_STRUCT); }
289 exp : exp '.' qualified_name
290 { /* exp.type::name becomes exp.*(&type::name) */
291 /* Note: this doesn't work if name is a
292 static member! FIXME */
293 write_exp_elt_opcode (UNOP_ADDR);
294 write_exp_elt_opcode (STRUCTOP_MEMBER); }
297 exp : exp '.' '*' exp
298 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
301 exp : exp '[' exp1 ']'
302 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
306 /* This is to save the value of arglist_len
307 being accumulated by an outer function call. */
308 { start_arglist (); }
309 arglist ')' %prec ARROW
310 { write_exp_elt_opcode (OP_FUNCALL);
311 write_exp_elt_longcst ((LONGEST) end_arglist ());
312 write_exp_elt_opcode (OP_FUNCALL); }
322 arglist : arglist ',' exp %prec ABOVE_COMMA
326 exp : '{' type '}' exp %prec UNARY
327 { write_exp_elt_opcode (UNOP_MEMVAL);
328 write_exp_elt_type ($2);
329 write_exp_elt_opcode (UNOP_MEMVAL); }
332 exp : '(' type ')' exp %prec UNARY
333 { write_exp_elt_opcode (UNOP_CAST);
334 write_exp_elt_type ($2);
335 write_exp_elt_opcode (UNOP_CAST); }
342 /* Binary operators in order of decreasing precedence. */
345 { write_exp_elt_opcode (BINOP_REPEAT); }
349 { write_exp_elt_opcode (BINOP_MUL); }
353 { write_exp_elt_opcode (BINOP_DIV); }
357 { write_exp_elt_opcode (BINOP_REM); }
361 { write_exp_elt_opcode (BINOP_ADD); }
365 { write_exp_elt_opcode (BINOP_SUB); }
369 { write_exp_elt_opcode (BINOP_LSH); }
373 { write_exp_elt_opcode (BINOP_RSH); }
377 { write_exp_elt_opcode (BINOP_EQUAL); }
380 exp : exp NOTEQUAL exp
381 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
385 { write_exp_elt_opcode (BINOP_LEQ); }
389 { write_exp_elt_opcode (BINOP_GEQ); }
393 { write_exp_elt_opcode (BINOP_LESS); }
397 { write_exp_elt_opcode (BINOP_GTR); }
401 { write_exp_elt_opcode (BINOP_LOGAND); }
405 { write_exp_elt_opcode (BINOP_LOGXOR); }
409 { write_exp_elt_opcode (BINOP_LOGIOR); }
413 { write_exp_elt_opcode (BINOP_AND); }
417 { write_exp_elt_opcode (BINOP_OR); }
420 exp : exp '?' exp ':' exp %prec '?'
421 { write_exp_elt_opcode (TERNOP_COND); }
425 { write_exp_elt_opcode (BINOP_ASSIGN); }
428 exp : exp ASSIGN_MODIFY exp
429 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
430 write_exp_elt_opcode ($2);
431 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
435 { write_exp_elt_opcode (OP_LONG);
436 if ($1 == (int) $1 || $1 == (unsigned int) $1)
437 write_exp_elt_type (builtin_type_int);
439 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
440 write_exp_elt_longcst ((LONGEST) $1);
441 write_exp_elt_opcode (OP_LONG); }
446 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
447 write_exp_elt_opcode (OP_LONG);
448 if (val.lval == (int) val.lval ||
449 val.lval == (unsigned int) val.lval)
450 write_exp_elt_type (builtin_type_int);
452 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
453 write_exp_elt_longcst (val.lval);
454 write_exp_elt_opcode (OP_LONG); }
459 write_exp_elt_opcode (OP_LONG);
460 if ($1 == (unsigned int) $1)
461 write_exp_elt_type (builtin_type_unsigned_int);
463 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
464 write_exp_elt_longcst ((LONGEST) $1);
465 write_exp_elt_opcode (OP_LONG);
471 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
472 write_exp_elt_opcode (OP_LONG);
473 if (val.ulval == (unsigned int) val.ulval)
474 write_exp_elt_type (builtin_type_unsigned_int);
476 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
477 write_exp_elt_longcst ((LONGEST)val.ulval);
478 write_exp_elt_opcode (OP_LONG);
483 { write_exp_elt_opcode (OP_LONG);
484 write_exp_elt_type (builtin_type_char);
485 write_exp_elt_longcst ((LONGEST) $1);
486 write_exp_elt_opcode (OP_LONG); }
490 { write_exp_elt_opcode (OP_DOUBLE);
491 write_exp_elt_type (builtin_type_double);
492 write_exp_elt_dblcst ($1);
493 write_exp_elt_opcode (OP_DOUBLE); }
500 { write_exp_elt_opcode (OP_LAST);
501 write_exp_elt_longcst ((LONGEST) $1);
502 write_exp_elt_opcode (OP_LAST); }
506 { write_exp_elt_opcode (OP_REGISTER);
507 write_exp_elt_longcst ((LONGEST) $1);
508 write_exp_elt_opcode (OP_REGISTER); }
512 { write_exp_elt_opcode (OP_INTERNALVAR);
513 write_exp_elt_intern ($1);
514 write_exp_elt_opcode (OP_INTERNALVAR); }
517 exp : SIZEOF '(' type ')' %prec UNARY
518 { write_exp_elt_opcode (OP_LONG);
519 write_exp_elt_type (builtin_type_int);
520 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
521 write_exp_elt_opcode (OP_LONG); }
525 { write_exp_elt_opcode (OP_STRING);
526 write_exp_string ($1);
527 write_exp_elt_opcode (OP_STRING); }
532 { write_exp_elt_opcode (OP_THIS);
533 write_exp_elt_opcode (OP_THIS); }
541 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
545 lookup_symtab (copy_name ($1.stoken));
547 $$ = BLOCKVECTOR_BLOCK
548 (BLOCKVECTOR (tem), STATIC_BLOCK);
550 error ("No file or function \"%s\".",
551 copy_name ($1.stoken));
556 block : block COLONCOLON name
558 = lookup_symbol (copy_name ($3), $1,
559 VAR_NAMESPACE, 0, NULL);
560 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
561 error ("No function \"%s\" in specified context.",
563 $$ = SYMBOL_BLOCK_VALUE (tem); }
566 variable: block COLONCOLON name
567 { struct symbol *sym;
568 sym = lookup_symbol (copy_name ($3), $1,
569 VAR_NAMESPACE, 0, NULL);
571 error ("No symbol \"%s\" in specified context.",
574 write_exp_elt_opcode (OP_VAR_VALUE);
575 write_exp_elt_sym (sym);
576 write_exp_elt_opcode (OP_VAR_VALUE); }
579 qualified_name: typebase COLONCOLON name
581 struct type *type = $1;
582 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
583 && TYPE_CODE (type) != TYPE_CODE_UNION)
584 error ("`%s' is not defined as an aggregate type.",
587 write_exp_elt_opcode (OP_SCOPE);
588 write_exp_elt_type (type);
589 write_exp_string ($3);
590 write_exp_elt_opcode (OP_SCOPE);
592 | typebase COLONCOLON '~' name
594 struct type *type = $1;
595 struct stoken tmp_token;
596 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
597 && TYPE_CODE (type) != TYPE_CODE_UNION)
598 error ("`%s' is not defined as an aggregate type.",
601 if (strcmp (type_name_no_tag (type), $4.ptr))
602 error ("invalid destructor `%s::~%s'",
603 type_name_no_tag (type), $4.ptr);
605 tmp_token.ptr = (char*) alloca ($4.length + 2);
606 tmp_token.length = $4.length + 1;
607 tmp_token.ptr[0] = '~';
608 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
609 tmp_token.ptr[tmp_token.length] = 0;
610 write_exp_elt_opcode (OP_SCOPE);
611 write_exp_elt_type (type);
612 write_exp_string (tmp_token);
613 write_exp_elt_opcode (OP_SCOPE);
617 variable: qualified_name
620 char *name = copy_name ($2);
622 struct minimal_symbol *msymbol;
625 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
628 write_exp_elt_opcode (OP_VAR_VALUE);
629 write_exp_elt_sym (sym);
630 write_exp_elt_opcode (OP_VAR_VALUE);
634 msymbol = lookup_minimal_symbol (name,
635 (struct objfile *) NULL);
638 write_exp_elt_opcode (OP_LONG);
639 write_exp_elt_type (builtin_type_int);
640 write_exp_elt_longcst ((LONGEST) msymbol -> address);
641 write_exp_elt_opcode (OP_LONG);
642 write_exp_elt_opcode (UNOP_MEMVAL);
643 if (msymbol -> type == mst_data ||
644 msymbol -> type == mst_bss)
645 write_exp_elt_type (builtin_type_int);
646 else if (msymbol -> type == mst_text)
647 write_exp_elt_type (lookup_function_type (builtin_type_int));
649 write_exp_elt_type (builtin_type_char);
650 write_exp_elt_opcode (UNOP_MEMVAL);
653 if (!have_full_symbols () && !have_partial_symbols ())
654 error ("No symbol table is loaded. Use the \"file\" command.");
656 error ("No symbol \"%s\" in current context.", name);
660 variable: name_not_typename
661 { struct symbol *sym = $1.sym;
665 switch (SYMBOL_CLASS (sym))
673 if (innermost_block == 0 ||
674 contained_in (block_found,
676 innermost_block = block_found;
683 case LOC_CONST_BYTES:
685 /* In this case the expression can
686 be evaluated regardless of what
687 frame we are in, so there is no
688 need to check for the
689 innermost_block. These cases are
690 listed so that gcc -Wall will
691 report types that may not have
696 write_exp_elt_opcode (OP_VAR_VALUE);
697 write_exp_elt_sym (sym);
698 write_exp_elt_opcode (OP_VAR_VALUE);
700 else if ($1.is_a_field_of_this)
702 /* C++: it hangs off of `this'. Must
703 not inadvertently convert from a method call
705 if (innermost_block == 0 ||
706 contained_in (block_found, innermost_block))
707 innermost_block = block_found;
708 write_exp_elt_opcode (OP_THIS);
709 write_exp_elt_opcode (OP_THIS);
710 write_exp_elt_opcode (STRUCTOP_PTR);
711 write_exp_string ($1.stoken);
712 write_exp_elt_opcode (STRUCTOP_PTR);
716 struct minimal_symbol *msymbol;
717 register char *arg = copy_name ($1.stoken);
719 msymbol = lookup_minimal_symbol (arg,
720 (struct objfile *) NULL);
723 write_exp_elt_opcode (OP_LONG);
724 write_exp_elt_type (builtin_type_int);
725 write_exp_elt_longcst ((LONGEST) msymbol -> address);
726 write_exp_elt_opcode (OP_LONG);
727 write_exp_elt_opcode (UNOP_MEMVAL);
728 if (msymbol -> type == mst_data ||
729 msymbol -> type == mst_bss)
730 write_exp_elt_type (builtin_type_int);
731 else if (msymbol -> type == mst_text)
732 write_exp_elt_type (lookup_function_type (builtin_type_int));
734 write_exp_elt_type (builtin_type_char);
735 write_exp_elt_opcode (UNOP_MEMVAL);
737 else if (!have_full_symbols () && !have_partial_symbols ())
738 error ("No symbol table is loaded. Use the \"file\" command.");
740 error ("No symbol \"%s\" in current context.",
741 copy_name ($1.stoken));
750 /* This is where the interesting stuff happens. */
753 struct type *follow_type = $1;
762 follow_type = lookup_pointer_type (follow_type);
765 follow_type = lookup_reference_type (follow_type);
768 array_size = pop_type_int ();
769 if (array_size != -1)
770 follow_type = create_array_type (follow_type,
773 follow_type = lookup_pointer_type (follow_type);
776 follow_type = lookup_function_type (follow_type);
784 { push_type (tp_pointer); $$ = 0; }
786 { push_type (tp_pointer); $$ = $2; }
788 { push_type (tp_reference); $$ = 0; }
790 { push_type (tp_reference); $$ = $2; }
794 direct_abs_decl: '(' abs_decl ')'
796 | direct_abs_decl array_mod
799 push_type (tp_array);
804 push_type (tp_array);
807 | direct_abs_decl func_mod
808 { push_type (tp_function); }
810 { push_type (tp_function); }
821 | '(' nonempty_typelist ')'
822 { free ((PTR)$2); $$ = 0; }
826 | typebase COLONCOLON '*'
827 { $$ = lookup_member_type (builtin_type_int, $1); }
828 | type '(' typebase COLONCOLON '*' ')'
829 { $$ = lookup_member_type ($1, $3); }
830 | type '(' typebase COLONCOLON '*' ')' '(' ')'
831 { $$ = lookup_member_type
832 (lookup_function_type ($1), $3); }
833 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
834 { $$ = lookup_member_type
835 (lookup_function_type ($1), $3);
837 /* "const" and "volatile" are curently ignored. */
838 | CONST_KEYWORD type { $$ = $2; }
839 | VOLATILE_KEYWORD type { $$ = $2; }
846 { $$ = builtin_type_int; }
848 { $$ = builtin_type_long; }
850 { $$ = builtin_type_short; }
852 { $$ = builtin_type_long; }
853 | UNSIGNED LONG INT_KEYWORD
854 { $$ = builtin_type_unsigned_long; }
856 { $$ = builtin_type_long_long; }
857 | LONG LONG INT_KEYWORD
858 { $$ = builtin_type_long_long; }
860 { $$ = builtin_type_unsigned_long_long; }
861 | UNSIGNED LONG LONG INT_KEYWORD
862 { $$ = builtin_type_unsigned_long_long; }
864 { $$ = builtin_type_short; }
865 | UNSIGNED SHORT INT_KEYWORD
866 { $$ = builtin_type_unsigned_short; }
868 { $$ = lookup_struct (copy_name ($2),
869 expression_context_block); }
871 { $$ = lookup_struct (copy_name ($2),
872 expression_context_block); }
874 { $$ = lookup_union (copy_name ($2),
875 expression_context_block); }
877 { $$ = lookup_enum (copy_name ($2),
878 expression_context_block); }
880 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
882 { $$ = builtin_type_unsigned_int; }
883 | SIGNED_KEYWORD typename
884 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
886 { $$ = builtin_type_int; }
887 | TEMPLATE name '<' type '>'
888 { $$ = lookup_template_type(copy_name($2), $4,
889 expression_context_block);
896 $$.stoken.ptr = "int";
897 $$.stoken.length = 3;
898 $$.type = builtin_type_int;
902 $$.stoken.ptr = "long";
903 $$.stoken.length = 4;
904 $$.type = builtin_type_long;
908 $$.stoken.ptr = "short";
909 $$.stoken.length = 5;
910 $$.type = builtin_type_short;
916 { $$ = (struct type **) xmalloc (sizeof (struct type *) * 2);
917 $<ivec>$[0] = 1; /* Number of types in vector */
920 | nonempty_typelist ',' type
921 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
922 $$ = (struct type **) xrealloc ((char *) $1, len);
923 $$[$<ivec>$[0]] = $3;
927 name : NAME { $$ = $1.stoken; }
928 | BLOCKNAME { $$ = $1.stoken; }
929 | TYPENAME { $$ = $1.stoken; }
930 | NAME_OR_INT { $$ = $1.stoken; }
931 | NAME_OR_UINT { $$ = $1.stoken; }
934 name_not_typename : NAME
936 /* These would be useful if name_not_typename was useful, but it is just
937 a fake for "variable", so these cause reduce/reduce conflicts because
938 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
939 =exp) or just an exp. If name_not_typename was ever used in an lvalue
940 context where only a name could occur, this might be useful.
948 /* Take care of parsing a number (anything that starts with a digit).
949 Set yylval and return the token type; update lexptr.
950 LEN is the number of characters in it. */
952 /*** Needs some error checking for the float case ***/
955 parse_number (p, len, parsed_float, putithere)
961 register LONGEST n = 0;
962 register LONGEST prevn = 0;
965 register int base = input_radix;
970 /* It's a float since it contains a point or an exponent. */
971 putithere->dval = atof (p);
975 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1009 if (c >= 'A' && c <= 'Z')
1011 if (c != 'l' && c != 'u')
1013 if (c >= '0' && c <= '9')
1017 if (base > 10 && c >= 'a' && c <= 'f')
1018 n += i = c - 'a' + 10;
1019 else if (len == 0 && c == 'l')
1021 else if (len == 0 && c == 'u')
1024 return ERROR; /* Char not a digit */
1027 return ERROR; /* Invalid digit in this base */
1028 /* Portably test for overflow (only works for nonzero values, so make
1029 a second check for zero). */
1030 if((prevn >= n) && n != 0)
1031 unsigned_p=1; /* Try something unsigned */
1032 /* If range checking enabled, portably test for unsigned overflow. */
1033 if(RANGE_CHECK && n!=0)
1035 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
1036 range_error("Overflow on numeric constant.");
1043 putithere->ulval = n;
1048 putithere->lval = n;
1057 enum exp_opcode opcode;
1060 const static struct token tokentab3[] =
1062 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1063 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1066 const static struct token tokentab2[] =
1068 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1069 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1070 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1071 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1072 {"%=", ASSIGN_MODIFY, BINOP_REM},
1073 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1074 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1075 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1076 {"++", INCREMENT, BINOP_END},
1077 {"--", DECREMENT, BINOP_END},
1078 {"->", ARROW, BINOP_END},
1079 {"&&", ANDAND, BINOP_END},
1080 {"||", OROR, BINOP_END},
1081 {"::", COLONCOLON, BINOP_END},
1082 {"<<", LSH, BINOP_END},
1083 {">>", RSH, BINOP_END},
1084 {"==", EQUAL, BINOP_END},
1085 {"!=", NOTEQUAL, BINOP_END},
1086 {"<=", LEQ, BINOP_END},
1087 {">=", GEQ, BINOP_END}
1090 /* Read one token, getting characters through lexptr. */
1096 register int namelen;
1097 register unsigned i;
1098 register char *tokstart;
1103 /* See if it is a special token of length 3. */
1104 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1105 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1108 yylval.opcode = tokentab3[i].opcode;
1109 return tokentab3[i].token;
1112 /* See if it is a special token of length 2. */
1113 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1114 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1117 yylval.opcode = tokentab2[i].opcode;
1118 return tokentab2[i].token;
1121 switch (c = *tokstart)
1133 /* We either have a character constant ('0' or '\177' for example)
1134 or we have a quoted symbol reference ('foo(int,int)' in C++
1139 c = parse_escape (&lexptr);
1144 namelen = skip_quoted (tokstart) - tokstart;
1147 lexptr = tokstart + namelen;
1152 error ("Invalid character constant.");
1162 if (paren_depth == 0)
1169 if (comma_terminates && paren_depth == 0)
1175 /* Might be a floating point number. */
1176 if (lexptr[1] < '0' || lexptr[1] > '9')
1177 goto symbol; /* Nope, must be a symbol. */
1178 /* FALL THRU into number case. */
1191 /* It's a number. */
1192 int got_dot = 0, got_e = 0, toktype;
1193 register char *p = tokstart;
1194 int hex = input_radix > 10;
1196 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1201 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1209 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1210 got_dot = got_e = 1;
1211 else if (!hex && !got_dot && *p == '.')
1213 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1214 && (*p == '-' || *p == '+'))
1215 /* This is the sign of the exponent, not the end of the
1218 /* We will take any letters or digits. parse_number will
1219 complain if past the radix, or if L or U are not final. */
1220 else if ((*p < '0' || *p > '9')
1221 && ((*p < 'a' || *p > 'z')
1222 && (*p < 'A' || *p > 'Z')))
1225 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1226 if (toktype == ERROR)
1228 char *err_copy = (char *) alloca (p - tokstart + 1);
1230 memcpy (err_copy, tokstart, p - tokstart);
1231 err_copy[p - tokstart] = 0;
1232 error ("Invalid number \"%s\".", err_copy);
1263 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1266 c = tokstart[++namelen];
1267 if (c >= '0' && c <= '9')
1269 c = tokstart[++namelen];
1270 if (c >= '0' && c <= '9')
1271 c = tokstart[++namelen];
1274 yylval.sval.ptr = tokstart + 1;
1275 yylval.sval.length = namelen - 1;
1276 lexptr += namelen + 1;
1280 if (!(c == '_' || c == '$'
1281 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1282 /* We must have come across a bad character (e.g. ';'). */
1283 error ("Invalid character '%c' in expression.", c);
1285 /* It's a name. See how long it is. */
1287 for (c = tokstart[namelen];
1288 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1289 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1290 c = tokstart[++namelen])
1293 /* The token "if" terminates the expression and is NOT
1294 removed from the input stream. */
1295 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1302 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1303 and $$digits (equivalent to $<-digits> if you could type that).
1304 Make token type LAST, and put the number (the digits) in yylval. */
1307 if (*tokstart == '$')
1309 register int negate = 0;
1311 /* Double dollar means negate the number and add -1 as well.
1312 Thus $$ alone means -1. */
1313 if (namelen >= 2 && tokstart[1] == '$')
1320 /* Just dollars (one or two) */
1321 yylval.lval = - negate;
1324 /* Is the rest of the token digits? */
1325 for (; c < namelen; c++)
1326 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1330 yylval.lval = atoi (tokstart + 1 + negate);
1332 yylval.lval = - yylval.lval;
1337 /* Handle tokens that refer to machine registers:
1338 $ followed by a register name. */
1340 if (*tokstart == '$') {
1341 for (c = 0; c < NUM_REGS; c++)
1342 if (namelen - 1 == strlen (reg_names[c])
1343 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1348 for (c = 0; c < num_std_regs; c++)
1349 if (namelen - 1 == strlen (std_regs[c].name)
1350 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1352 yylval.lval = std_regs[c].regnum;
1356 /* Catch specific keywords. Should be done with a data structure. */
1360 if (!strncmp (tokstart, "unsigned", 8))
1362 if (current_language->la_language == language_cplus
1363 && !strncmp (tokstart, "template", 8))
1365 if (!strncmp (tokstart, "volatile", 8))
1366 return VOLATILE_KEYWORD;
1369 if (!strncmp (tokstart, "struct", 6))
1371 if (!strncmp (tokstart, "signed", 6))
1372 return SIGNED_KEYWORD;
1373 if (!strncmp (tokstart, "sizeof", 6))
1377 if (current_language->la_language == language_cplus
1378 && !strncmp (tokstart, "class", 5))
1380 if (!strncmp (tokstart, "union", 5))
1382 if (!strncmp (tokstart, "short", 5))
1384 if (!strncmp (tokstart, "const", 5))
1385 return CONST_KEYWORD;
1388 if (!strncmp (tokstart, "enum", 4))
1390 if (!strncmp (tokstart, "long", 4))
1392 if (current_language->la_language == language_cplus
1393 && !strncmp (tokstart, "this", 4))
1395 static const char this_name[] =
1396 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1398 if (lookup_symbol (this_name, expression_context_block,
1399 VAR_NAMESPACE, 0, NULL))
1404 if (!strncmp (tokstart, "int", 3))
1411 yylval.sval.ptr = tokstart;
1412 yylval.sval.length = namelen;
1414 /* Any other names starting in $ are debugger internal variables. */
1416 if (*tokstart == '$')
1418 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1422 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1423 functions or symtabs. If this is not so, then ...
1424 Use token-type TYPENAME for symbols that happen to be defined
1425 currently as names of types; NAME for other symbols.
1426 The caller is not constrained to care about the distinction. */
1428 char *tmp = copy_name (yylval.sval);
1430 int is_a_field_of_this = 0;
1433 sym = lookup_symbol (tmp, expression_context_block,
1435 current_language->la_language == language_cplus
1436 ? &is_a_field_of_this : NULL,
1438 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1439 lookup_partial_symtab (tmp))
1441 yylval.ssym.sym = sym;
1442 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1445 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1447 yylval.tsym.type = SYMBOL_TYPE (sym);
1450 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1453 /* Input names that aren't symbols but ARE valid hex numbers,
1454 when the input radix permits them, can be names or numbers
1455 depending on the parse. Note we support radixes > 16 here. */
1457 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1458 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1460 YYSTYPE newlval; /* Its value is ignored. */
1461 hextype = parse_number (tokstart, namelen, 0, &newlval);
1464 yylval.ssym.sym = sym;
1465 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1468 if (hextype == UINT)
1470 yylval.ssym.sym = sym;
1471 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1472 return NAME_OR_UINT;
1476 /* Any other kind of symbol */
1477 yylval.ssym.sym = sym;
1478 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1487 error (msg ? msg : "Invalid syntax in expression.");
1490 /* Table mapping opcodes into strings for printing operators
1491 and precedences of the operators. */
1493 const static struct op_print c_op_print_tab[] =
1495 {",", BINOP_COMMA, PREC_COMMA, 0},
1496 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1497 {"||", BINOP_OR, PREC_OR, 0},
1498 {"&&", BINOP_AND, PREC_AND, 0},
1499 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1500 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1501 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1502 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1503 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1504 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1505 {">=", BINOP_GEQ, PREC_ORDER, 0},
1506 {">", BINOP_GTR, PREC_ORDER, 0},
1507 {"<", BINOP_LESS, PREC_ORDER, 0},
1508 {">>", BINOP_RSH, PREC_SHIFT, 0},
1509 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1510 {"+", BINOP_ADD, PREC_ADD, 0},
1511 {"-", BINOP_SUB, PREC_ADD, 0},
1512 {"*", BINOP_MUL, PREC_MUL, 0},
1513 {"/", BINOP_DIV, PREC_MUL, 0},
1514 {"%", BINOP_REM, PREC_MUL, 0},
1515 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1516 {"-", UNOP_NEG, PREC_PREFIX, 0},
1517 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1518 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1519 {"*", UNOP_IND, PREC_PREFIX, 0},
1520 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1521 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1522 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1523 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1525 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1528 /* These variables point to the objects
1529 representing the predefined C data types. */
1531 struct type *builtin_type_void;
1532 struct type *builtin_type_char;
1533 struct type *builtin_type_short;
1534 struct type *builtin_type_int;
1535 struct type *builtin_type_long;
1536 struct type *builtin_type_long_long;
1537 struct type *builtin_type_signed_char;
1538 struct type *builtin_type_unsigned_char;
1539 struct type *builtin_type_unsigned_short;
1540 struct type *builtin_type_unsigned_int;
1541 struct type *builtin_type_unsigned_long;
1542 struct type *builtin_type_unsigned_long_long;
1543 struct type *builtin_type_float;
1544 struct type *builtin_type_double;
1545 struct type *builtin_type_long_double;
1546 struct type *builtin_type_complex;
1547 struct type *builtin_type_double_complex;
1549 struct type ** const (c_builtin_types[]) =
1553 &builtin_type_short,
1555 &builtin_type_float,
1556 &builtin_type_double,
1558 &builtin_type_long_long,
1559 &builtin_type_signed_char,
1560 &builtin_type_unsigned_char,
1561 &builtin_type_unsigned_short,
1562 &builtin_type_unsigned_int,
1563 &builtin_type_unsigned_long,
1564 &builtin_type_unsigned_long_long,
1565 &builtin_type_long_double,
1566 &builtin_type_complex,
1567 &builtin_type_double_complex,
1571 const struct language_defn c_language_defn = {
1572 "c", /* Language name */
1579 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1580 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1581 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1582 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1583 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1584 c_op_print_tab, /* expression operators for printing */
1588 const struct language_defn cplus_language_defn = {
1589 "c++", /* Language name */
1596 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1597 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1598 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1599 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1600 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1601 c_op_print_tab, /* expression operators for printing */
1606 _initialize_c_exp ()
1609 init_type (TYPE_CODE_VOID, 1,
1611 "void", (struct objfile *) NULL);
1613 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1615 "char", (struct objfile *) NULL);
1616 builtin_type_signed_char =
1617 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1619 "signed char", (struct objfile *) NULL);
1620 builtin_type_unsigned_char =
1621 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1623 "unsigned char", (struct objfile *) NULL);
1624 builtin_type_short =
1625 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1627 "short", (struct objfile *) NULL);
1628 builtin_type_unsigned_short =
1629 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1631 "unsigned short", (struct objfile *) NULL);
1633 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1635 "int", (struct objfile *) NULL);
1636 builtin_type_unsigned_int =
1637 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1639 "unsigned int", (struct objfile *) NULL);
1641 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1643 "long", (struct objfile *) NULL);
1644 builtin_type_unsigned_long =
1645 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1647 "unsigned long", (struct objfile *) NULL);
1648 builtin_type_long_long =
1649 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1651 "long long", (struct objfile *) NULL);
1652 builtin_type_unsigned_long_long =
1653 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1655 "unsigned long long", (struct objfile *) NULL);
1656 builtin_type_float =
1657 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1659 "float", (struct objfile *) NULL);
1660 builtin_type_double =
1661 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1663 "double", (struct objfile *) NULL);
1664 builtin_type_long_double =
1665 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1667 "long double", (struct objfile *) NULL);
1668 builtin_type_complex =
1669 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1671 "complex", (struct objfile *) NULL);
1672 builtin_type_double_complex =
1673 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1675 "double complex", (struct objfile *) NULL);
1677 add_language (&c_language_defn);
1678 add_language (&cplus_language_defn);