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 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
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
200 /* Ensure that if the generated parser contains any calls to malloc/realloc,
201 that they get mapped to xmalloc/xrealloc. We have to do this here
202 rather than earlier in the file because this is the first point after
203 the place where the SVR4 yacc includes <malloc.h>, and if we do it
204 before that, then the remapped declarations in <malloc.h> will collide
205 with the ones in "defs.h". */
207 #define malloc xmalloc
208 #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);
843 { $$ = builtin_type_int; }
845 { $$ = builtin_type_long; }
847 { $$ = builtin_type_short; }
849 { $$ = builtin_type_long; }
850 | UNSIGNED LONG INT_KEYWORD
851 { $$ = builtin_type_unsigned_long; }
853 { $$ = builtin_type_long_long; }
854 | LONG LONG INT_KEYWORD
855 { $$ = builtin_type_long_long; }
857 { $$ = builtin_type_unsigned_long_long; }
858 | UNSIGNED LONG LONG INT_KEYWORD
859 { $$ = builtin_type_unsigned_long_long; }
861 { $$ = builtin_type_short; }
862 | UNSIGNED SHORT INT_KEYWORD
863 { $$ = builtin_type_unsigned_short; }
865 { $$ = lookup_struct (copy_name ($2),
866 expression_context_block); }
868 { $$ = lookup_union (copy_name ($2),
869 expression_context_block); }
871 { $$ = lookup_enum (copy_name ($2),
872 expression_context_block); }
874 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
876 { $$ = builtin_type_unsigned_int; }
877 | SIGNED_KEYWORD typename
880 { $$ = builtin_type_int; }
881 | TEMPLATE name '<' type '>'
882 { $$ = lookup_template_type(copy_name($2), $4,
883 expression_context_block);
890 $$.stoken.ptr = "int";
891 $$.stoken.length = 3;
892 $$.type = builtin_type_int;
896 $$.stoken.ptr = "long";
897 $$.stoken.length = 4;
898 $$.type = builtin_type_long;
902 $$.stoken.ptr = "short";
903 $$.stoken.length = 5;
904 $$.type = builtin_type_short;
910 { $$ = (struct type **) xmalloc (sizeof (struct type *) * 2);
911 $<ivec>$[0] = 1; /* Number of types in vector */
914 | nonempty_typelist ',' type
915 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
916 $$ = (struct type **) xrealloc ((char *) $1, len);
917 $$[$<ivec>$[0]] = $3;
921 name : NAME { $$ = $1.stoken; }
922 | BLOCKNAME { $$ = $1.stoken; }
923 | TYPENAME { $$ = $1.stoken; }
924 | NAME_OR_INT { $$ = $1.stoken; }
925 | NAME_OR_UINT { $$ = $1.stoken; }
928 name_not_typename : NAME
930 /* These would be useful if name_not_typename was useful, but it is just
931 a fake for "variable", so these cause reduce/reduce conflicts because
932 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
933 =exp) or just an exp. If name_not_typename was ever used in an lvalue
934 context where only a name could occur, this might be useful.
942 /* Take care of parsing a number (anything that starts with a digit).
943 Set yylval and return the token type; update lexptr.
944 LEN is the number of characters in it. */
946 /*** Needs some error checking for the float case ***/
949 parse_number (p, len, parsed_float, putithere)
955 register LONGEST n = 0;
956 register LONGEST prevn = 0;
959 register int base = input_radix;
964 /* It's a float since it contains a point or an exponent. */
965 putithere->dval = atof (p);
969 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1003 if (c >= 'A' && c <= 'Z')
1005 if (c != 'l' && c != 'u')
1007 if (c >= '0' && c <= '9')
1011 if (base > 10 && c >= 'a' && c <= 'f')
1012 n += i = c - 'a' + 10;
1013 else if (len == 0 && c == 'l')
1015 else if (len == 0 && c == 'u')
1018 return ERROR; /* Char not a digit */
1021 return ERROR; /* Invalid digit in this base */
1022 /* Portably test for overflow (only works for nonzero values, so make
1023 a second check for zero). */
1024 if((prevn >= n) && n != 0)
1025 unsigned_p=1; /* Try something unsigned */
1026 /* If range checking enabled, portably test for unsigned overflow. */
1027 if(RANGE_CHECK && n!=0)
1029 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
1030 range_error("Overflow on numeric constant.");
1037 putithere->ulval = n;
1042 putithere->lval = n;
1051 enum exp_opcode opcode;
1054 const static struct token tokentab3[] =
1056 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1057 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1060 const static struct token tokentab2[] =
1062 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1063 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1064 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1065 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1066 {"%=", ASSIGN_MODIFY, BINOP_REM},
1067 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1068 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1069 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1070 {"++", INCREMENT, BINOP_END},
1071 {"--", DECREMENT, BINOP_END},
1072 {"->", ARROW, BINOP_END},
1073 {"&&", ANDAND, BINOP_END},
1074 {"||", OROR, BINOP_END},
1075 {"::", COLONCOLON, BINOP_END},
1076 {"<<", LSH, BINOP_END},
1077 {">>", RSH, BINOP_END},
1078 {"==", EQUAL, BINOP_END},
1079 {"!=", NOTEQUAL, BINOP_END},
1080 {"<=", LEQ, BINOP_END},
1081 {">=", GEQ, BINOP_END}
1084 /* Read one token, getting characters through lexptr. */
1090 register int namelen;
1091 register unsigned i;
1092 register char *tokstart;
1097 /* See if it is a special token of length 3. */
1098 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1099 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1102 yylval.opcode = tokentab3[i].opcode;
1103 return tokentab3[i].token;
1106 /* See if it is a special token of length 2. */
1107 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1108 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1111 yylval.opcode = tokentab2[i].opcode;
1112 return tokentab2[i].token;
1115 switch (c = *tokstart)
1127 /* We either have a character constant ('0' or '\177' for example)
1128 or we have a quoted symbol reference ('foo(int,int)' in C++
1133 c = parse_escape (&lexptr);
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 bcopy (tokstart, err_copy, p - tokstart);
1225 err_copy[p - tokstart] = 0;
1226 error ("Invalid number \"%s\".", err_copy);
1257 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1260 c = tokstart[++namelen];
1261 if (c >= '0' && c <= '9')
1263 c = tokstart[++namelen];
1264 if (c >= '0' && c <= '9')
1265 c = tokstart[++namelen];
1268 yylval.sval.ptr = tokstart + 1;
1269 yylval.sval.length = namelen - 1;
1270 lexptr += namelen + 1;
1274 if (!(c == '_' || c == '$'
1275 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1276 /* We must have come across a bad character (e.g. ';'). */
1277 error ("Invalid character '%c' in expression.", c);
1279 /* It's a name. See how long it is. */
1281 for (c = tokstart[namelen];
1282 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1283 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1284 c = tokstart[++namelen])
1287 /* The token "if" terminates the expression and is NOT
1288 removed from the input stream. */
1289 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1296 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1297 and $$digits (equivalent to $<-digits> if you could type that).
1298 Make token type LAST, and put the number (the digits) in yylval. */
1301 if (*tokstart == '$')
1303 register int negate = 0;
1305 /* Double dollar means negate the number and add -1 as well.
1306 Thus $$ alone means -1. */
1307 if (namelen >= 2 && tokstart[1] == '$')
1314 /* Just dollars (one or two) */
1315 yylval.lval = - negate;
1318 /* Is the rest of the token digits? */
1319 for (; c < namelen; c++)
1320 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1324 yylval.lval = atoi (tokstart + 1 + negate);
1326 yylval.lval = - yylval.lval;
1331 /* Handle tokens that refer to machine registers:
1332 $ followed by a register name. */
1334 if (*tokstart == '$') {
1335 for (c = 0; c < NUM_REGS; c++)
1336 if (namelen - 1 == strlen (reg_names[c])
1337 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1342 for (c = 0; c < num_std_regs; c++)
1343 if (namelen - 1 == strlen (std_regs[c].name)
1344 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1346 yylval.lval = std_regs[c].regnum;
1350 /* Catch specific keywords. Should be done with a data structure. */
1354 if (!strncmp (tokstart, "unsigned", 8))
1356 if (current_language->la_language == language_cplus
1357 && !strncmp (tokstart, "template", 8))
1361 if (!strncmp (tokstart, "struct", 6))
1363 if (!strncmp (tokstart, "signed", 6))
1364 return SIGNED_KEYWORD;
1365 if (!strncmp (tokstart, "sizeof", 6))
1369 if (!strncmp (tokstart, "union", 5))
1371 if (!strncmp (tokstart, "short", 5))
1375 if (!strncmp (tokstart, "enum", 4))
1377 if (!strncmp (tokstart, "long", 4))
1379 if (current_language->la_language == language_cplus
1380 && !strncmp (tokstart, "this", 4))
1382 static const char this_name[] =
1383 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1385 if (lookup_symbol (this_name, expression_context_block,
1386 VAR_NAMESPACE, 0, NULL))
1391 if (!strncmp (tokstart, "int", 3))
1398 yylval.sval.ptr = tokstart;
1399 yylval.sval.length = namelen;
1401 /* Any other names starting in $ are debugger internal variables. */
1403 if (*tokstart == '$')
1405 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1409 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1410 functions or symtabs. If this is not so, then ...
1411 Use token-type TYPENAME for symbols that happen to be defined
1412 currently as names of types; NAME for other symbols.
1413 The caller is not constrained to care about the distinction. */
1415 char *tmp = copy_name (yylval.sval);
1417 int is_a_field_of_this = 0;
1420 sym = lookup_symbol (tmp, expression_context_block,
1422 current_language->la_language == language_cplus
1423 ? &is_a_field_of_this : NULL,
1425 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1426 lookup_partial_symtab (tmp))
1428 yylval.ssym.sym = sym;
1429 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1432 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1434 yylval.tsym.type = SYMBOL_TYPE (sym);
1437 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1440 /* Input names that aren't symbols but ARE valid hex numbers,
1441 when the input radix permits them, can be names or numbers
1442 depending on the parse. Note we support radixes > 16 here. */
1444 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1445 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1447 YYSTYPE newlval; /* Its value is ignored. */
1448 hextype = parse_number (tokstart, namelen, 0, &newlval);
1451 yylval.ssym.sym = sym;
1452 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1455 if (hextype == UINT)
1457 yylval.ssym.sym = sym;
1458 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1459 return NAME_OR_UINT;
1463 /* Any other kind of symbol */
1464 yylval.ssym.sym = sym;
1465 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1474 error (msg ? msg : "Invalid syntax in expression.");
1477 /* Table mapping opcodes into strings for printing operators
1478 and precedences of the operators. */
1480 const static struct op_print c_op_print_tab[] =
1482 {",", BINOP_COMMA, PREC_COMMA, 0},
1483 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1484 {"||", BINOP_OR, PREC_OR, 0},
1485 {"&&", BINOP_AND, PREC_AND, 0},
1486 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1487 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1488 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1489 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1490 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1491 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1492 {">=", BINOP_GEQ, PREC_ORDER, 0},
1493 {">", BINOP_GTR, PREC_ORDER, 0},
1494 {"<", BINOP_LESS, PREC_ORDER, 0},
1495 {">>", BINOP_RSH, PREC_SHIFT, 0},
1496 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1497 {"+", BINOP_ADD, PREC_ADD, 0},
1498 {"-", BINOP_SUB, PREC_ADD, 0},
1499 {"*", BINOP_MUL, PREC_MUL, 0},
1500 {"/", BINOP_DIV, PREC_MUL, 0},
1501 {"%", BINOP_REM, PREC_MUL, 0},
1502 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1503 {"-", UNOP_NEG, PREC_PREFIX, 0},
1504 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1505 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1506 {"*", UNOP_IND, PREC_PREFIX, 0},
1507 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1508 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1509 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1510 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1512 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1515 /* These variables point to the objects
1516 representing the predefined C data types. */
1518 struct type *builtin_type_void;
1519 struct type *builtin_type_char;
1520 struct type *builtin_type_short;
1521 struct type *builtin_type_int;
1522 struct type *builtin_type_long;
1523 struct type *builtin_type_long_long;
1524 struct type *builtin_type_signed_char;
1525 struct type *builtin_type_unsigned_char;
1526 struct type *builtin_type_unsigned_short;
1527 struct type *builtin_type_unsigned_int;
1528 struct type *builtin_type_unsigned_long;
1529 struct type *builtin_type_unsigned_long_long;
1530 struct type *builtin_type_float;
1531 struct type *builtin_type_double;
1532 struct type *builtin_type_long_double;
1533 struct type *builtin_type_complex;
1534 struct type *builtin_type_double_complex;
1536 struct type ** const (c_builtin_types[]) =
1540 &builtin_type_short,
1542 &builtin_type_float,
1543 &builtin_type_double,
1545 &builtin_type_long_long,
1546 &builtin_type_signed_char,
1547 &builtin_type_unsigned_char,
1548 &builtin_type_unsigned_short,
1549 &builtin_type_unsigned_int,
1550 &builtin_type_unsigned_long,
1551 &builtin_type_unsigned_long_long,
1552 &builtin_type_long_double,
1553 &builtin_type_complex,
1554 &builtin_type_double_complex,
1558 const struct language_defn c_language_defn = {
1559 "c", /* Language name */
1566 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1567 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1568 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1569 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1570 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1571 c_op_print_tab, /* expression operators for printing */
1575 const struct language_defn cplus_language_defn = {
1576 "c++", /* Language name */
1583 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1584 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1585 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1586 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1587 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1588 c_op_print_tab, /* expression operators for printing */
1593 _initialize_c_exp ()
1596 init_type (TYPE_CODE_VOID, 1,
1598 "void", (struct objfile *) NULL);
1600 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1602 "char", (struct objfile *) NULL);
1603 builtin_type_signed_char =
1604 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1606 "signed char", (struct objfile *) NULL);
1607 builtin_type_unsigned_char =
1608 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1610 "unsigned char", (struct objfile *) NULL);
1611 builtin_type_short =
1612 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1614 "short", (struct objfile *) NULL);
1615 builtin_type_unsigned_short =
1616 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1618 "unsigned short", (struct objfile *) NULL);
1620 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1622 "int", (struct objfile *) NULL);
1623 builtin_type_unsigned_int =
1624 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1626 "unsigned int", (struct objfile *) NULL);
1628 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1630 "long", (struct objfile *) NULL);
1631 builtin_type_unsigned_long =
1632 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1634 "unsigned long", (struct objfile *) NULL);
1635 builtin_type_long_long =
1636 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1638 "long long", (struct objfile *) NULL);
1639 builtin_type_unsigned_long_long =
1640 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1642 "unsigned long long", (struct objfile *) NULL);
1643 builtin_type_float =
1644 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1646 "float", (struct objfile *) NULL);
1647 builtin_type_double =
1648 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1650 "double", (struct objfile *) NULL);
1651 builtin_type_long_double =
1652 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1654 "long double", (struct objfile *) NULL);
1655 builtin_type_complex =
1656 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1658 "complex", (struct objfile *) NULL);
1659 builtin_type_double_complex =
1660 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1662 "double complex", (struct objfile *) NULL);
1664 add_language (&c_language_defn);
1665 add_language (&cplus_language_defn);