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;
114 struct symtoken ssym;
117 enum exp_opcode opcode;
118 struct internalvar *ivar;
125 /* YYSTYPE gets defined by %union */
127 parse_number PARAMS ((char *, int, int, YYSTYPE *));
130 %type <voidval> exp exp1 type_exp start variable qualified_name
131 %type <tval> type typebase
132 %type <tvec> nonempty_typelist
133 /* %type <bval> block */
135 /* Fancy type parsing. */
136 %type <voidval> func_mod direct_abs_decl abs_decl
138 %type <lval> array_mod
140 %token <lval> INT CHAR
144 /* Both NAME and TYPENAME tokens represent symbols in the input,
145 and both convey their data as strings.
146 But a TYPENAME is a string that happens to be defined as a typedef
147 or builtin type name (such as int or char)
148 and a NAME is any other symbol.
149 Contexts where this distinction is not important can use the
150 nonterminal "name", which matches either NAME or TYPENAME. */
153 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
154 %token <tsym> TYPENAME
156 %type <ssym> name_not_typename
157 %type <tsym> typename
159 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
160 but which would parse as a valid number in the current input radix.
161 E.g. "c" when input_radix==16. Depending on the parse, it will be
162 turned into a name or into a number. NAME_OR_UINT ditto. */
164 %token <ssym> NAME_OR_INT NAME_OR_UINT
166 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
170 /* Special type cases, put in to allow the parser to distinguish different
172 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD
174 %token <lval> LAST REGNAME
176 %token <ivar> VARIABLE
178 %token <opcode> ASSIGN_MODIFY
185 %right '=' ASSIGN_MODIFY
193 %left '<' '>' LEQ GEQ
198 %right UNARY INCREMENT DECREMENT
199 %right ARROW '.' '[' '('
200 %token <ssym> BLOCKNAME
212 { write_exp_elt_opcode(OP_TYPE);
213 write_exp_elt_type($1);
214 write_exp_elt_opcode(OP_TYPE);}
217 /* Expressions, including the comma operator. */
220 { write_exp_elt_opcode (BINOP_COMMA); }
223 /* Expressions, not including the comma operator. */
224 exp : '*' exp %prec UNARY
225 { write_exp_elt_opcode (UNOP_IND); }
227 exp : '&' exp %prec UNARY
228 { write_exp_elt_opcode (UNOP_ADDR); }
230 exp : '-' exp %prec UNARY
231 { write_exp_elt_opcode (UNOP_NEG); }
234 exp : '!' exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_ZEROP); }
238 exp : '~' exp %prec UNARY
239 { write_exp_elt_opcode (UNOP_LOGNOT); }
242 exp : INCREMENT exp %prec UNARY
243 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
246 exp : DECREMENT exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
250 exp : exp INCREMENT %prec UNARY
251 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
254 exp : exp DECREMENT %prec UNARY
255 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
258 exp : SIZEOF exp %prec UNARY
259 { write_exp_elt_opcode (UNOP_SIZEOF); }
263 { write_exp_elt_opcode (STRUCTOP_PTR);
264 write_exp_string ($3);
265 write_exp_elt_opcode (STRUCTOP_PTR); }
268 exp : exp ARROW qualified_name
269 { /* exp->type::name becomes exp->*(&type::name) */
270 /* Note: this doesn't work if name is a
271 static member! FIXME */
272 write_exp_elt_opcode (UNOP_ADDR);
273 write_exp_elt_opcode (STRUCTOP_MPTR); }
275 exp : exp ARROW '*' exp
276 { write_exp_elt_opcode (STRUCTOP_MPTR); }
280 { write_exp_elt_opcode (STRUCTOP_STRUCT);
281 write_exp_string ($3);
282 write_exp_elt_opcode (STRUCTOP_STRUCT); }
285 exp : exp '.' qualified_name
286 { /* exp.type::name becomes exp.*(&type::name) */
287 /* Note: this doesn't work if name is a
288 static member! FIXME */
289 write_exp_elt_opcode (UNOP_ADDR);
290 write_exp_elt_opcode (STRUCTOP_MEMBER); }
293 exp : exp '.' '*' exp
294 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
297 exp : exp '[' exp1 ']'
298 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
302 /* This is to save the value of arglist_len
303 being accumulated by an outer function call. */
304 { start_arglist (); }
305 arglist ')' %prec ARROW
306 { write_exp_elt_opcode (OP_FUNCALL);
307 write_exp_elt_longcst ((LONGEST) end_arglist ());
308 write_exp_elt_opcode (OP_FUNCALL); }
318 arglist : arglist ',' exp %prec ABOVE_COMMA
322 exp : '{' type '}' exp %prec UNARY
323 { write_exp_elt_opcode (UNOP_MEMVAL);
324 write_exp_elt_type ($2);
325 write_exp_elt_opcode (UNOP_MEMVAL); }
328 exp : '(' type ')' exp %prec UNARY
329 { write_exp_elt_opcode (UNOP_CAST);
330 write_exp_elt_type ($2);
331 write_exp_elt_opcode (UNOP_CAST); }
338 /* Binary operators in order of decreasing precedence. */
341 { write_exp_elt_opcode (BINOP_REPEAT); }
345 { write_exp_elt_opcode (BINOP_MUL); }
349 { write_exp_elt_opcode (BINOP_DIV); }
353 { write_exp_elt_opcode (BINOP_REM); }
357 { write_exp_elt_opcode (BINOP_ADD); }
361 { write_exp_elt_opcode (BINOP_SUB); }
365 { write_exp_elt_opcode (BINOP_LSH); }
369 { write_exp_elt_opcode (BINOP_RSH); }
373 { write_exp_elt_opcode (BINOP_EQUAL); }
376 exp : exp NOTEQUAL exp
377 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
381 { write_exp_elt_opcode (BINOP_LEQ); }
385 { write_exp_elt_opcode (BINOP_GEQ); }
389 { write_exp_elt_opcode (BINOP_LESS); }
393 { write_exp_elt_opcode (BINOP_GTR); }
397 { write_exp_elt_opcode (BINOP_LOGAND); }
401 { write_exp_elt_opcode (BINOP_LOGXOR); }
405 { write_exp_elt_opcode (BINOP_LOGIOR); }
409 { write_exp_elt_opcode (BINOP_AND); }
413 { write_exp_elt_opcode (BINOP_OR); }
416 exp : exp '?' exp ':' exp %prec '?'
417 { write_exp_elt_opcode (TERNOP_COND); }
421 { write_exp_elt_opcode (BINOP_ASSIGN); }
424 exp : exp ASSIGN_MODIFY exp
425 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
426 write_exp_elt_opcode ($2);
427 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
431 { write_exp_elt_opcode (OP_LONG);
432 if ($1 == (int) $1 || $1 == (unsigned int) $1)
433 write_exp_elt_type (builtin_type_int);
435 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
436 write_exp_elt_longcst ((LONGEST) $1);
437 write_exp_elt_opcode (OP_LONG); }
442 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
443 write_exp_elt_opcode (OP_LONG);
444 if (val.lval == (int) val.lval ||
445 val.lval == (unsigned int) val.lval)
446 write_exp_elt_type (builtin_type_int);
448 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
449 write_exp_elt_longcst (val.lval);
450 write_exp_elt_opcode (OP_LONG); }
455 write_exp_elt_opcode (OP_LONG);
456 if ($1 == (unsigned int) $1)
457 write_exp_elt_type (builtin_type_unsigned_int);
459 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
460 write_exp_elt_longcst ((LONGEST) $1);
461 write_exp_elt_opcode (OP_LONG);
467 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
468 write_exp_elt_opcode (OP_LONG);
469 if (val.ulval == (unsigned int) val.ulval)
470 write_exp_elt_type (builtin_type_unsigned_int);
472 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
473 write_exp_elt_longcst ((LONGEST)val.ulval);
474 write_exp_elt_opcode (OP_LONG);
479 { write_exp_elt_opcode (OP_LONG);
480 write_exp_elt_type (builtin_type_char);
481 write_exp_elt_longcst ((LONGEST) $1);
482 write_exp_elt_opcode (OP_LONG); }
486 { write_exp_elt_opcode (OP_DOUBLE);
487 write_exp_elt_type (builtin_type_double);
488 write_exp_elt_dblcst ($1);
489 write_exp_elt_opcode (OP_DOUBLE); }
496 { write_exp_elt_opcode (OP_LAST);
497 write_exp_elt_longcst ((LONGEST) $1);
498 write_exp_elt_opcode (OP_LAST); }
502 { write_exp_elt_opcode (OP_REGISTER);
503 write_exp_elt_longcst ((LONGEST) $1);
504 write_exp_elt_opcode (OP_REGISTER); }
508 { write_exp_elt_opcode (OP_INTERNALVAR);
509 write_exp_elt_intern ($1);
510 write_exp_elt_opcode (OP_INTERNALVAR); }
513 exp : SIZEOF '(' type ')' %prec UNARY
514 { write_exp_elt_opcode (OP_LONG);
515 write_exp_elt_type (builtin_type_int);
516 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
517 write_exp_elt_opcode (OP_LONG); }
521 { write_exp_elt_opcode (OP_STRING);
522 write_exp_string ($1);
523 write_exp_elt_opcode (OP_STRING); }
528 { write_exp_elt_opcode (OP_THIS);
529 write_exp_elt_opcode (OP_THIS); }
537 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
541 lookup_symtab (copy_name ($1.stoken));
543 $$ = BLOCKVECTOR_BLOCK
544 (BLOCKVECTOR (tem), STATIC_BLOCK);
546 error ("No file or function \"%s\".",
547 copy_name ($1.stoken));
552 block : block COLONCOLON name
554 = lookup_symbol (copy_name ($3), $1,
555 VAR_NAMESPACE, 0, NULL);
556 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
557 error ("No function \"%s\" in specified context.",
559 $$ = SYMBOL_BLOCK_VALUE (tem); }
562 variable: block COLONCOLON name
563 { struct symbol *sym;
564 sym = lookup_symbol (copy_name ($3), $1,
565 VAR_NAMESPACE, 0, NULL);
567 error ("No symbol \"%s\" in specified context.",
570 write_exp_elt_opcode (OP_VAR_VALUE);
571 write_exp_elt_sym (sym);
572 write_exp_elt_opcode (OP_VAR_VALUE); }
575 qualified_name: typebase COLONCOLON name
577 struct type *type = $1;
578 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
579 && TYPE_CODE (type) != TYPE_CODE_UNION)
580 error ("`%s' is not defined as an aggregate type.",
583 write_exp_elt_opcode (OP_SCOPE);
584 write_exp_elt_type (type);
585 write_exp_string ($3);
586 write_exp_elt_opcode (OP_SCOPE);
588 | typebase COLONCOLON '~' name
590 struct type *type = $1;
591 struct stoken tmp_token;
592 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
593 && TYPE_CODE (type) != TYPE_CODE_UNION)
594 error ("`%s' is not defined as an aggregate type.",
597 if (strcmp (type_name_no_tag (type), $4.ptr))
598 error ("invalid destructor `%s::~%s'",
599 type_name_no_tag (type), $4.ptr);
601 tmp_token.ptr = (char*) alloca ($4.length + 2);
602 tmp_token.length = $4.length + 1;
603 tmp_token.ptr[0] = '~';
604 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
605 tmp_token.ptr[tmp_token.length] = 0;
606 write_exp_elt_opcode (OP_SCOPE);
607 write_exp_elt_type (type);
608 write_exp_string (tmp_token);
609 write_exp_elt_opcode (OP_SCOPE);
613 variable: qualified_name
616 char *name = copy_name ($2);
618 struct minimal_symbol *msymbol;
621 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
624 write_exp_elt_opcode (OP_VAR_VALUE);
625 write_exp_elt_sym (sym);
626 write_exp_elt_opcode (OP_VAR_VALUE);
630 msymbol = lookup_minimal_symbol (name,
631 (struct objfile *) NULL);
634 write_exp_elt_opcode (OP_LONG);
635 write_exp_elt_type (builtin_type_int);
636 write_exp_elt_longcst ((LONGEST) msymbol -> address);
637 write_exp_elt_opcode (OP_LONG);
638 write_exp_elt_opcode (UNOP_MEMVAL);
639 if (msymbol -> type == mst_data ||
640 msymbol -> type == mst_bss)
641 write_exp_elt_type (builtin_type_int);
642 else if (msymbol -> type == mst_text)
643 write_exp_elt_type (lookup_function_type (builtin_type_int));
645 write_exp_elt_type (builtin_type_char);
646 write_exp_elt_opcode (UNOP_MEMVAL);
649 if (!have_full_symbols () && !have_partial_symbols ())
650 error ("No symbol table is loaded. Use the \"file\" command.");
652 error ("No symbol \"%s\" in current context.", name);
656 variable: name_not_typename
657 { struct symbol *sym = $1.sym;
661 switch (SYMBOL_CLASS (sym))
669 if (innermost_block == 0 ||
670 contained_in (block_found,
672 innermost_block = block_found;
679 case LOC_CONST_BYTES:
681 /* In this case the expression can
682 be evaluated regardless of what
683 frame we are in, so there is no
684 need to check for the
685 innermost_block. These cases are
686 listed so that gcc -Wall will
687 report types that may not have
692 write_exp_elt_opcode (OP_VAR_VALUE);
693 write_exp_elt_sym (sym);
694 write_exp_elt_opcode (OP_VAR_VALUE);
696 else if ($1.is_a_field_of_this)
698 /* C++: it hangs off of `this'. Must
699 not inadvertently convert from a method call
701 if (innermost_block == 0 ||
702 contained_in (block_found, innermost_block))
703 innermost_block = block_found;
704 write_exp_elt_opcode (OP_THIS);
705 write_exp_elt_opcode (OP_THIS);
706 write_exp_elt_opcode (STRUCTOP_PTR);
707 write_exp_string ($1.stoken);
708 write_exp_elt_opcode (STRUCTOP_PTR);
712 struct minimal_symbol *msymbol;
713 register char *arg = copy_name ($1.stoken);
715 msymbol = lookup_minimal_symbol (arg,
716 (struct objfile *) NULL);
719 write_exp_elt_opcode (OP_LONG);
720 write_exp_elt_type (builtin_type_int);
721 write_exp_elt_longcst ((LONGEST) msymbol -> address);
722 write_exp_elt_opcode (OP_LONG);
723 write_exp_elt_opcode (UNOP_MEMVAL);
724 if (msymbol -> type == mst_data ||
725 msymbol -> type == mst_bss)
726 write_exp_elt_type (builtin_type_int);
727 else if (msymbol -> type == mst_text)
728 write_exp_elt_type (lookup_function_type (builtin_type_int));
730 write_exp_elt_type (builtin_type_char);
731 write_exp_elt_opcode (UNOP_MEMVAL);
733 else if (!have_full_symbols () && !have_partial_symbols ())
734 error ("No symbol table is loaded. Use the \"file\" command.");
736 error ("No symbol \"%s\" in current context.",
737 copy_name ($1.stoken));
746 /* This is where the interesting stuff happens. */
749 struct type *follow_type = $1;
758 follow_type = lookup_pointer_type (follow_type);
761 follow_type = lookup_reference_type (follow_type);
764 array_size = pop_type_int ();
765 if (array_size != -1)
766 follow_type = create_array_type (follow_type,
769 follow_type = lookup_pointer_type (follow_type);
772 follow_type = lookup_function_type (follow_type);
780 { push_type (tp_pointer); $$ = 0; }
782 { push_type (tp_pointer); $$ = $2; }
784 { push_type (tp_reference); $$ = 0; }
786 { push_type (tp_reference); $$ = $2; }
790 direct_abs_decl: '(' abs_decl ')'
792 | direct_abs_decl array_mod
795 push_type (tp_array);
800 push_type (tp_array);
803 | direct_abs_decl func_mod
804 { push_type (tp_function); }
806 { push_type (tp_function); }
817 | '(' nonempty_typelist ')'
818 { free ((PTR)$2); $$ = 0; }
822 | typebase COLONCOLON '*'
823 { $$ = lookup_member_type (builtin_type_int, $1); }
824 | type '(' typebase COLONCOLON '*' ')'
825 { $$ = lookup_member_type ($1, $3); }
826 | type '(' typebase COLONCOLON '*' ')' '(' ')'
827 { $$ = lookup_member_type
828 (lookup_function_type ($1), $3); }
829 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
830 { $$ = lookup_member_type
831 (lookup_function_type ($1), $3);
835 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
839 { $$ = builtin_type_int; }
841 { $$ = builtin_type_long; }
843 { $$ = builtin_type_short; }
845 { $$ = builtin_type_long; }
846 | UNSIGNED LONG INT_KEYWORD
847 { $$ = builtin_type_unsigned_long; }
849 { $$ = builtin_type_long_long; }
850 | LONG LONG INT_KEYWORD
851 { $$ = builtin_type_long_long; }
853 { $$ = builtin_type_unsigned_long_long; }
854 | UNSIGNED LONG LONG INT_KEYWORD
855 { $$ = builtin_type_unsigned_long_long; }
857 { $$ = builtin_type_short; }
858 | UNSIGNED SHORT INT_KEYWORD
859 { $$ = builtin_type_unsigned_short; }
861 { $$ = lookup_struct (copy_name ($2),
862 expression_context_block); }
864 { $$ = lookup_struct (copy_name ($2),
865 expression_context_block); }
867 { $$ = lookup_union (copy_name ($2),
868 expression_context_block); }
870 { $$ = lookup_enum (copy_name ($2),
871 expression_context_block); }
873 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
875 { $$ = builtin_type_unsigned_int; }
876 | SIGNED_KEYWORD typename
877 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
879 { $$ = builtin_type_int; }
880 | TEMPLATE name '<' type '>'
881 { $$ = lookup_template_type(copy_name($2), $4,
882 expression_context_block);
884 /* "const" and "volatile" are curently ignored. */
885 | CONST_KEYWORD typebase { $$ = $2; }
886 | VOLATILE_KEYWORD typebase { $$ = $2; }
892 $$.stoken.ptr = "int";
893 $$.stoken.length = 3;
894 $$.type = builtin_type_int;
898 $$.stoken.ptr = "long";
899 $$.stoken.length = 4;
900 $$.type = builtin_type_long;
904 $$.stoken.ptr = "short";
905 $$.stoken.length = 5;
906 $$.type = builtin_type_short;
912 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
913 $<ivec>$[0] = 1; /* Number of types in vector */
916 | nonempty_typelist ',' type
917 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
918 $$ = (struct type **) realloc ((char *) $1, len);
919 $$[$<ivec>$[0]] = $3;
923 name : NAME { $$ = $1.stoken; }
924 | BLOCKNAME { $$ = $1.stoken; }
925 | TYPENAME { $$ = $1.stoken; }
926 | NAME_OR_INT { $$ = $1.stoken; }
927 | NAME_OR_UINT { $$ = $1.stoken; }
930 name_not_typename : NAME
932 /* These would be useful if name_not_typename was useful, but it is just
933 a fake for "variable", so these cause reduce/reduce conflicts because
934 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
935 =exp) or just an exp. If name_not_typename was ever used in an lvalue
936 context where only a name could occur, this might be useful.
944 /* Take care of parsing a number (anything that starts with a digit).
945 Set yylval and return the token type; update lexptr.
946 LEN is the number of characters in it. */
948 /*** Needs some error checking for the float case ***/
951 parse_number (p, len, parsed_float, putithere)
957 register LONGEST n = 0;
958 register LONGEST prevn = 0;
961 register int base = input_radix;
966 /* It's a float since it contains a point or an exponent. */
967 putithere->dval = atof (p);
971 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1005 if (c >= 'A' && c <= 'Z')
1007 if (c != 'l' && c != 'u')
1009 if (c >= '0' && c <= '9')
1013 if (base > 10 && c >= 'a' && c <= 'f')
1014 n += i = c - 'a' + 10;
1015 else if (len == 0 && c == 'l')
1017 else if (len == 0 && c == 'u')
1020 return ERROR; /* Char not a digit */
1023 return ERROR; /* Invalid digit in this base */
1024 /* Portably test for overflow (only works for nonzero values, so make
1025 a second check for zero). */
1026 if((prevn >= n) && n != 0)
1027 unsigned_p=1; /* Try something unsigned */
1028 /* If range checking enabled, portably test for unsigned overflow. */
1029 if(RANGE_CHECK && n!=0)
1031 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
1032 range_error("Overflow on numeric constant.");
1039 putithere->ulval = n;
1044 putithere->lval = n;
1053 enum exp_opcode opcode;
1056 const static struct token tokentab3[] =
1058 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1059 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1062 const static struct token tokentab2[] =
1064 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1065 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1066 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1067 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1068 {"%=", ASSIGN_MODIFY, BINOP_REM},
1069 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1070 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1071 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1072 {"++", INCREMENT, BINOP_END},
1073 {"--", DECREMENT, BINOP_END},
1074 {"->", ARROW, BINOP_END},
1075 {"&&", ANDAND, BINOP_END},
1076 {"||", OROR, BINOP_END},
1077 {"::", COLONCOLON, BINOP_END},
1078 {"<<", LSH, BINOP_END},
1079 {">>", RSH, BINOP_END},
1080 {"==", EQUAL, BINOP_END},
1081 {"!=", NOTEQUAL, BINOP_END},
1082 {"<=", LEQ, BINOP_END},
1083 {">=", GEQ, BINOP_END}
1086 /* Read one token, getting characters through lexptr. */
1092 register int namelen;
1093 register unsigned i;
1094 register char *tokstart;
1099 /* See if it is a special token of length 3. */
1100 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1101 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1104 yylval.opcode = tokentab3[i].opcode;
1105 return tokentab3[i].token;
1108 /* See if it is a special token of length 2. */
1109 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1110 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1113 yylval.opcode = tokentab2[i].opcode;
1114 return tokentab2[i].token;
1117 switch (c = *tokstart)
1129 /* We either have a character constant ('0' or '\177' for example)
1130 or we have a quoted symbol reference ('foo(int,int)' in C++
1135 c = parse_escape (&lexptr);
1140 namelen = skip_quoted (tokstart) - tokstart;
1143 lexptr = tokstart + namelen;
1148 error ("Invalid character constant.");
1158 if (paren_depth == 0)
1165 if (comma_terminates && paren_depth == 0)
1171 /* Might be a floating point number. */
1172 if (lexptr[1] < '0' || lexptr[1] > '9')
1173 goto symbol; /* Nope, must be a symbol. */
1174 /* FALL THRU into number case. */
1187 /* It's a number. */
1188 int got_dot = 0, got_e = 0, toktype;
1189 register char *p = tokstart;
1190 int hex = input_radix > 10;
1192 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1197 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1205 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1206 got_dot = got_e = 1;
1207 else if (!hex && !got_dot && *p == '.')
1209 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1210 && (*p == '-' || *p == '+'))
1211 /* This is the sign of the exponent, not the end of the
1214 /* We will take any letters or digits. parse_number will
1215 complain if past the radix, or if L or U are not final. */
1216 else if ((*p < '0' || *p > '9')
1217 && ((*p < 'a' || *p > 'z')
1218 && (*p < 'A' || *p > 'Z')))
1221 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1222 if (toktype == ERROR)
1224 char *err_copy = (char *) alloca (p - tokstart + 1);
1226 memcpy (err_copy, tokstart, p - tokstart);
1227 err_copy[p - tokstart] = 0;
1228 error ("Invalid number \"%s\".", err_copy);
1259 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1262 c = tokstart[++namelen];
1263 if (c >= '0' && c <= '9')
1265 c = tokstart[++namelen];
1266 if (c >= '0' && c <= '9')
1267 c = tokstart[++namelen];
1270 yylval.sval.ptr = tokstart + 1;
1271 yylval.sval.length = namelen - 1;
1272 lexptr += namelen + 1;
1276 if (!(c == '_' || c == '$'
1277 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1278 /* We must have come across a bad character (e.g. ';'). */
1279 error ("Invalid character '%c' in expression.", c);
1281 /* It's a name. See how long it is. */
1283 for (c = tokstart[namelen];
1284 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1285 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1286 c = tokstart[++namelen])
1289 /* The token "if" terminates the expression and is NOT
1290 removed from the input stream. */
1291 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1298 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1299 and $$digits (equivalent to $<-digits> if you could type that).
1300 Make token type LAST, and put the number (the digits) in yylval. */
1303 if (*tokstart == '$')
1305 register int negate = 0;
1307 /* Double dollar means negate the number and add -1 as well.
1308 Thus $$ alone means -1. */
1309 if (namelen >= 2 && tokstart[1] == '$')
1316 /* Just dollars (one or two) */
1317 yylval.lval = - negate;
1320 /* Is the rest of the token digits? */
1321 for (; c < namelen; c++)
1322 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1326 yylval.lval = atoi (tokstart + 1 + negate);
1328 yylval.lval = - yylval.lval;
1333 /* Handle tokens that refer to machine registers:
1334 $ followed by a register name. */
1336 if (*tokstart == '$') {
1337 for (c = 0; c < NUM_REGS; c++)
1338 if (namelen - 1 == strlen (reg_names[c])
1339 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1344 for (c = 0; c < num_std_regs; c++)
1345 if (namelen - 1 == strlen (std_regs[c].name)
1346 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1348 yylval.lval = std_regs[c].regnum;
1352 /* Catch specific keywords. Should be done with a data structure. */
1356 if (!strncmp (tokstart, "unsigned", 8))
1358 if (current_language->la_language == language_cplus
1359 && !strncmp (tokstart, "template", 8))
1361 if (!strncmp (tokstart, "volatile", 8))
1362 return VOLATILE_KEYWORD;
1365 if (!strncmp (tokstart, "struct", 6))
1367 if (!strncmp (tokstart, "signed", 6))
1368 return SIGNED_KEYWORD;
1369 if (!strncmp (tokstart, "sizeof", 6))
1373 if (current_language->la_language == language_cplus
1374 && !strncmp (tokstart, "class", 5))
1376 if (!strncmp (tokstart, "union", 5))
1378 if (!strncmp (tokstart, "short", 5))
1380 if (!strncmp (tokstart, "const", 5))
1381 return CONST_KEYWORD;
1384 if (!strncmp (tokstart, "enum", 4))
1386 if (!strncmp (tokstart, "long", 4))
1388 if (current_language->la_language == language_cplus
1389 && !strncmp (tokstart, "this", 4))
1391 static const char this_name[] =
1392 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1394 if (lookup_symbol (this_name, expression_context_block,
1395 VAR_NAMESPACE, 0, NULL))
1400 if (!strncmp (tokstart, "int", 3))
1407 yylval.sval.ptr = tokstart;
1408 yylval.sval.length = namelen;
1410 /* Any other names starting in $ are debugger internal variables. */
1412 if (*tokstart == '$')
1414 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1418 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1419 functions or symtabs. If this is not so, then ...
1420 Use token-type TYPENAME for symbols that happen to be defined
1421 currently as names of types; NAME for other symbols.
1422 The caller is not constrained to care about the distinction. */
1424 char *tmp = copy_name (yylval.sval);
1426 int is_a_field_of_this = 0;
1429 sym = lookup_symbol (tmp, expression_context_block,
1431 current_language->la_language == language_cplus
1432 ? &is_a_field_of_this : NULL,
1434 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1435 lookup_partial_symtab (tmp))
1437 yylval.ssym.sym = sym;
1438 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1441 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1443 yylval.tsym.type = SYMBOL_TYPE (sym);
1446 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1449 /* Input names that aren't symbols but ARE valid hex numbers,
1450 when the input radix permits them, can be names or numbers
1451 depending on the parse. Note we support radixes > 16 here. */
1453 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1454 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1456 YYSTYPE newlval; /* Its value is ignored. */
1457 hextype = parse_number (tokstart, namelen, 0, &newlval);
1460 yylval.ssym.sym = sym;
1461 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1464 if (hextype == UINT)
1466 yylval.ssym.sym = sym;
1467 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1468 return NAME_OR_UINT;
1472 /* Any other kind of symbol */
1473 yylval.ssym.sym = sym;
1474 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1483 error (msg ? msg : "Invalid syntax in expression.");
1486 /* Table mapping opcodes into strings for printing operators
1487 and precedences of the operators. */
1489 const static struct op_print c_op_print_tab[] =
1491 {",", BINOP_COMMA, PREC_COMMA, 0},
1492 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1493 {"||", BINOP_OR, PREC_OR, 0},
1494 {"&&", BINOP_AND, PREC_AND, 0},
1495 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1496 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1497 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1498 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1499 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1500 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1501 {">=", BINOP_GEQ, PREC_ORDER, 0},
1502 {">", BINOP_GTR, PREC_ORDER, 0},
1503 {"<", BINOP_LESS, PREC_ORDER, 0},
1504 {">>", BINOP_RSH, PREC_SHIFT, 0},
1505 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1506 {"+", BINOP_ADD, PREC_ADD, 0},
1507 {"-", BINOP_SUB, PREC_ADD, 0},
1508 {"*", BINOP_MUL, PREC_MUL, 0},
1509 {"/", BINOP_DIV, PREC_MUL, 0},
1510 {"%", BINOP_REM, PREC_MUL, 0},
1511 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1512 {"-", UNOP_NEG, PREC_PREFIX, 0},
1513 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1514 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1515 {"*", UNOP_IND, PREC_PREFIX, 0},
1516 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1517 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1518 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1519 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1521 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1524 /* These variables point to the objects
1525 representing the predefined C data types. */
1527 struct type *builtin_type_void;
1528 struct type *builtin_type_char;
1529 struct type *builtin_type_short;
1530 struct type *builtin_type_int;
1531 struct type *builtin_type_long;
1532 struct type *builtin_type_long_long;
1533 struct type *builtin_type_signed_char;
1534 struct type *builtin_type_unsigned_char;
1535 struct type *builtin_type_unsigned_short;
1536 struct type *builtin_type_unsigned_int;
1537 struct type *builtin_type_unsigned_long;
1538 struct type *builtin_type_unsigned_long_long;
1539 struct type *builtin_type_float;
1540 struct type *builtin_type_double;
1541 struct type *builtin_type_long_double;
1542 struct type *builtin_type_complex;
1543 struct type *builtin_type_double_complex;
1545 struct type ** const (c_builtin_types[]) =
1549 &builtin_type_short,
1551 &builtin_type_float,
1552 &builtin_type_double,
1554 &builtin_type_long_long,
1555 &builtin_type_signed_char,
1556 &builtin_type_unsigned_char,
1557 &builtin_type_unsigned_short,
1558 &builtin_type_unsigned_int,
1559 &builtin_type_unsigned_long,
1560 &builtin_type_unsigned_long_long,
1561 &builtin_type_long_double,
1562 &builtin_type_complex,
1563 &builtin_type_double_complex,
1567 const struct language_defn c_language_defn = {
1568 "c", /* Language name */
1575 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1576 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1577 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1578 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1579 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1580 c_op_print_tab, /* expression operators for printing */
1584 const struct language_defn cplus_language_defn = {
1585 "c++", /* Language name */
1592 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1593 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1594 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1595 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1596 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1597 c_op_print_tab, /* expression operators for printing */
1602 _initialize_c_exp ()
1605 init_type (TYPE_CODE_VOID, 1,
1607 "void", (struct objfile *) NULL);
1609 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1611 "char", (struct objfile *) NULL);
1612 builtin_type_signed_char =
1613 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1615 "signed char", (struct objfile *) NULL);
1616 builtin_type_unsigned_char =
1617 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1619 "unsigned char", (struct objfile *) NULL);
1620 builtin_type_short =
1621 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1623 "short", (struct objfile *) NULL);
1624 builtin_type_unsigned_short =
1625 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1627 "unsigned short", (struct objfile *) NULL);
1629 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1631 "int", (struct objfile *) NULL);
1632 builtin_type_unsigned_int =
1633 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1635 "unsigned int", (struct objfile *) NULL);
1637 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1639 "long", (struct objfile *) NULL);
1640 builtin_type_unsigned_long =
1641 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1643 "unsigned long", (struct objfile *) NULL);
1644 builtin_type_long_long =
1645 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1647 "long long", (struct objfile *) NULL);
1648 builtin_type_unsigned_long_long =
1649 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1651 "unsigned long long", (struct objfile *) NULL);
1652 builtin_type_float =
1653 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1655 "float", (struct objfile *) NULL);
1656 builtin_type_double =
1657 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1659 "double", (struct objfile *) NULL);
1660 builtin_type_long_double =
1661 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1663 "long double", (struct objfile *) NULL);
1664 builtin_type_complex =
1665 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1667 "complex", (struct objfile *) NULL);
1668 builtin_type_double_complex =
1669 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1671 "double complex", (struct objfile *) NULL);
1673 add_language (&c_language_defn);
1674 add_language (&cplus_language_defn);