1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Parse a C expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result. */
37 #include "expression.h"
38 #include "parser-defs.h"
42 /* Ensure that if the generated parser contains any calls to malloc/realloc,
43 that they get mapped to xmalloc/xrealloc. */
45 #define malloc xmalloc
46 #define realloc xrealloc
48 /* These MUST be included in any grammar file!!!!
49 Please choose unique names! */
50 #define yymaxdepth c_maxdepth
51 #define yyparse c_parse
53 #define yyerror c_error
56 #define yydebug c_debug
65 #define yyerrflag c_errflag
66 #define yynerrs c_nerrs
71 #define yystate c_state
78 yyparse PARAMS ((void));
81 yylex PARAMS ((void));
84 yyerror PARAMS ((char *));
86 /* #define YYDEBUG 1 */
90 /* Although the yacc "value" of an expression is not used,
91 since the result is stored in the structure being created,
92 other node types do have values. */
97 unsigned LONGEST ulval;
103 struct symtoken ssym;
106 enum exp_opcode opcode;
107 struct internalvar *ivar;
114 /* YYSTYPE gets defined by %union */
116 parse_number PARAMS ((char *, int, int, YYSTYPE *));
119 %type <voidval> exp exp1 type_exp start variable qualified_name
120 %type <tval> type typebase
121 %type <tvec> nonempty_typelist
122 /* %type <bval> block */
124 /* Fancy type parsing. */
125 %type <voidval> func_mod direct_abs_decl abs_decl
127 %type <lval> array_mod
129 %token <lval> INT CHAR
133 /* Both NAME and TYPENAME tokens represent symbols in the input,
134 and both convey their data as strings.
135 But a TYPENAME is a string that happens to be defined as a typedef
136 or builtin type name (such as int or char)
137 and a NAME is any other symbol.
138 Contexts where this distinction is not important can use the
139 nonterminal "name", which matches either NAME or TYPENAME. */
142 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
143 %token <tsym> TYPENAME
145 %type <ssym> name_not_typename
146 %type <tsym> typename
148 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
149 but which would parse as a valid number in the current input radix.
150 E.g. "c" when input_radix==16. Depending on the parse, it will be
151 turned into a name or into a number. NAME_OR_UINT ditto. */
153 %token <ssym> NAME_OR_INT NAME_OR_UINT
155 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
159 /* Special type cases, put in to allow the parser to distinguish different
161 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD
163 %token <lval> LAST REGNAME
165 %token <ivar> VARIABLE
167 %token <opcode> ASSIGN_MODIFY
174 %right '=' ASSIGN_MODIFY
182 %left '<' '>' LEQ GEQ
187 %right UNARY INCREMENT DECREMENT
188 %right ARROW '.' '[' '('
189 %token <ssym> BLOCKNAME
200 { write_exp_elt_opcode(OP_TYPE);
201 write_exp_elt_type($1);
202 write_exp_elt_opcode(OP_TYPE);}
205 /* Expressions, including the comma operator. */
208 { write_exp_elt_opcode (BINOP_COMMA); }
211 /* Expressions, not including the comma operator. */
212 exp : '*' exp %prec UNARY
213 { write_exp_elt_opcode (UNOP_IND); }
215 exp : '&' exp %prec UNARY
216 { write_exp_elt_opcode (UNOP_ADDR); }
218 exp : '-' exp %prec UNARY
219 { write_exp_elt_opcode (UNOP_NEG); }
222 exp : '!' exp %prec UNARY
223 { write_exp_elt_opcode (UNOP_ZEROP); }
226 exp : '~' exp %prec UNARY
227 { write_exp_elt_opcode (UNOP_LOGNOT); }
230 exp : INCREMENT exp %prec UNARY
231 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
234 exp : DECREMENT exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
238 exp : exp INCREMENT %prec UNARY
239 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
242 exp : exp DECREMENT %prec UNARY
243 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
246 exp : SIZEOF exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_SIZEOF); }
251 { write_exp_elt_opcode (STRUCTOP_PTR);
252 write_exp_string ($3);
253 write_exp_elt_opcode (STRUCTOP_PTR); }
256 exp : exp ARROW qualified_name
257 { /* exp->type::name becomes exp->*(&type::name) */
258 /* Note: this doesn't work if name is a
259 static member! FIXME */
260 write_exp_elt_opcode (UNOP_ADDR);
261 write_exp_elt_opcode (STRUCTOP_MPTR); }
263 exp : exp ARROW '*' exp
264 { write_exp_elt_opcode (STRUCTOP_MPTR); }
268 { write_exp_elt_opcode (STRUCTOP_STRUCT);
269 write_exp_string ($3);
270 write_exp_elt_opcode (STRUCTOP_STRUCT); }
273 exp : exp '.' qualified_name
274 { /* exp.type::name becomes exp.*(&type::name) */
275 /* Note: this doesn't work if name is a
276 static member! FIXME */
277 write_exp_elt_opcode (UNOP_ADDR);
278 write_exp_elt_opcode (STRUCTOP_MEMBER); }
281 exp : exp '.' '*' exp
282 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
285 exp : exp '[' exp1 ']'
286 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
290 /* This is to save the value of arglist_len
291 being accumulated by an outer function call. */
292 { start_arglist (); }
293 arglist ')' %prec ARROW
294 { write_exp_elt_opcode (OP_FUNCALL);
295 write_exp_elt_longcst ((LONGEST) end_arglist ());
296 write_exp_elt_opcode (OP_FUNCALL); }
306 arglist : arglist ',' exp %prec ABOVE_COMMA
310 exp : '{' type '}' exp %prec UNARY
311 { write_exp_elt_opcode (UNOP_MEMVAL);
312 write_exp_elt_type ($2);
313 write_exp_elt_opcode (UNOP_MEMVAL); }
316 exp : '(' type ')' exp %prec UNARY
317 { write_exp_elt_opcode (UNOP_CAST);
318 write_exp_elt_type ($2);
319 write_exp_elt_opcode (UNOP_CAST); }
326 /* Binary operators in order of decreasing precedence. */
329 { write_exp_elt_opcode (BINOP_REPEAT); }
333 { write_exp_elt_opcode (BINOP_MUL); }
337 { write_exp_elt_opcode (BINOP_DIV); }
341 { write_exp_elt_opcode (BINOP_REM); }
345 { write_exp_elt_opcode (BINOP_ADD); }
349 { write_exp_elt_opcode (BINOP_SUB); }
353 { write_exp_elt_opcode (BINOP_LSH); }
357 { write_exp_elt_opcode (BINOP_RSH); }
361 { write_exp_elt_opcode (BINOP_EQUAL); }
364 exp : exp NOTEQUAL exp
365 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
369 { write_exp_elt_opcode (BINOP_LEQ); }
373 { write_exp_elt_opcode (BINOP_GEQ); }
377 { write_exp_elt_opcode (BINOP_LESS); }
381 { write_exp_elt_opcode (BINOP_GTR); }
385 { write_exp_elt_opcode (BINOP_LOGAND); }
389 { write_exp_elt_opcode (BINOP_LOGXOR); }
393 { write_exp_elt_opcode (BINOP_LOGIOR); }
397 { write_exp_elt_opcode (BINOP_AND); }
401 { write_exp_elt_opcode (BINOP_OR); }
404 exp : exp '?' exp ':' exp %prec '?'
405 { write_exp_elt_opcode (TERNOP_COND); }
409 { write_exp_elt_opcode (BINOP_ASSIGN); }
412 exp : exp ASSIGN_MODIFY exp
413 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
414 write_exp_elt_opcode ($2);
415 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
419 { write_exp_elt_opcode (OP_LONG);
420 if ($1 == (int) $1 || $1 == (unsigned int) $1)
421 write_exp_elt_type (builtin_type_int);
423 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
424 write_exp_elt_longcst ((LONGEST) $1);
425 write_exp_elt_opcode (OP_LONG); }
430 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
431 write_exp_elt_opcode (OP_LONG);
432 if (val.lval == (int) val.lval ||
433 val.lval == (unsigned int) val.lval)
434 write_exp_elt_type (builtin_type_int);
436 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
437 write_exp_elt_longcst (val.lval);
438 write_exp_elt_opcode (OP_LONG); }
443 write_exp_elt_opcode (OP_LONG);
444 if ($1 == (unsigned int) $1)
445 write_exp_elt_type (builtin_type_unsigned_int);
447 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
448 write_exp_elt_longcst ((LONGEST) $1);
449 write_exp_elt_opcode (OP_LONG);
455 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
456 write_exp_elt_opcode (OP_LONG);
457 if (val.ulval == (unsigned int) val.ulval)
458 write_exp_elt_type (builtin_type_unsigned_int);
460 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
461 write_exp_elt_longcst ((LONGEST)val.ulval);
462 write_exp_elt_opcode (OP_LONG);
467 { write_exp_elt_opcode (OP_LONG);
468 write_exp_elt_type (builtin_type_char);
469 write_exp_elt_longcst ((LONGEST) $1);
470 write_exp_elt_opcode (OP_LONG); }
474 { write_exp_elt_opcode (OP_DOUBLE);
475 write_exp_elt_type (builtin_type_double);
476 write_exp_elt_dblcst ($1);
477 write_exp_elt_opcode (OP_DOUBLE); }
484 { write_exp_elt_opcode (OP_LAST);
485 write_exp_elt_longcst ((LONGEST) $1);
486 write_exp_elt_opcode (OP_LAST); }
490 { write_exp_elt_opcode (OP_REGISTER);
491 write_exp_elt_longcst ((LONGEST) $1);
492 write_exp_elt_opcode (OP_REGISTER); }
496 { write_exp_elt_opcode (OP_INTERNALVAR);
497 write_exp_elt_intern ($1);
498 write_exp_elt_opcode (OP_INTERNALVAR); }
501 exp : SIZEOF '(' type ')' %prec UNARY
502 { write_exp_elt_opcode (OP_LONG);
503 write_exp_elt_type (builtin_type_int);
504 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
505 write_exp_elt_opcode (OP_LONG); }
509 { write_exp_elt_opcode (OP_STRING);
510 write_exp_string ($1);
511 write_exp_elt_opcode (OP_STRING); }
516 { write_exp_elt_opcode (OP_THIS);
517 write_exp_elt_opcode (OP_THIS); }
525 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
529 lookup_symtab (copy_name ($1.stoken));
531 $$ = BLOCKVECTOR_BLOCK
532 (BLOCKVECTOR (tem), STATIC_BLOCK);
534 error ("No file or function \"%s\".",
535 copy_name ($1.stoken));
540 block : block COLONCOLON name
542 = lookup_symbol (copy_name ($3), $1,
543 VAR_NAMESPACE, 0, NULL);
544 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
545 error ("No function \"%s\" in specified context.",
547 $$ = SYMBOL_BLOCK_VALUE (tem); }
550 variable: block COLONCOLON name
551 { struct symbol *sym;
552 sym = lookup_symbol (copy_name ($3), $1,
553 VAR_NAMESPACE, 0, NULL);
555 error ("No symbol \"%s\" in specified context.",
558 write_exp_elt_opcode (OP_VAR_VALUE);
559 write_exp_elt_sym (sym);
560 write_exp_elt_opcode (OP_VAR_VALUE); }
563 qualified_name: typebase COLONCOLON name
565 struct type *type = $1;
566 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
567 && TYPE_CODE (type) != TYPE_CODE_UNION)
568 error ("`%s' is not defined as an aggregate type.",
571 write_exp_elt_opcode (OP_SCOPE);
572 write_exp_elt_type (type);
573 write_exp_string ($3);
574 write_exp_elt_opcode (OP_SCOPE);
576 | typebase COLONCOLON '~' name
578 struct type *type = $1;
579 struct stoken tmp_token;
580 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
581 && TYPE_CODE (type) != TYPE_CODE_UNION)
582 error ("`%s' is not defined as an aggregate type.",
585 if (strcmp (type_name_no_tag (type), $4.ptr))
586 error ("invalid destructor `%s::~%s'",
587 type_name_no_tag (type), $4.ptr);
589 tmp_token.ptr = (char*) alloca ($4.length + 2);
590 tmp_token.length = $4.length + 1;
591 tmp_token.ptr[0] = '~';
592 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
593 tmp_token.ptr[tmp_token.length] = 0;
594 write_exp_elt_opcode (OP_SCOPE);
595 write_exp_elt_type (type);
596 write_exp_string (tmp_token);
597 write_exp_elt_opcode (OP_SCOPE);
601 variable: qualified_name
604 char *name = copy_name ($2);
606 struct minimal_symbol *msymbol;
609 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
612 write_exp_elt_opcode (OP_VAR_VALUE);
613 write_exp_elt_sym (sym);
614 write_exp_elt_opcode (OP_VAR_VALUE);
618 msymbol = lookup_minimal_symbol (name,
619 (struct objfile *) NULL);
622 write_exp_elt_opcode (OP_LONG);
623 write_exp_elt_type (builtin_type_int);
624 write_exp_elt_longcst ((LONGEST) msymbol -> address);
625 write_exp_elt_opcode (OP_LONG);
626 write_exp_elt_opcode (UNOP_MEMVAL);
627 if (msymbol -> type == mst_data ||
628 msymbol -> type == mst_bss)
629 write_exp_elt_type (builtin_type_int);
630 else if (msymbol -> type == mst_text)
631 write_exp_elt_type (lookup_function_type (builtin_type_int));
633 write_exp_elt_type (builtin_type_char);
634 write_exp_elt_opcode (UNOP_MEMVAL);
637 if (!have_full_symbols () && !have_partial_symbols ())
638 error ("No symbol table is loaded. Use the \"file\" command.");
640 error ("No symbol \"%s\" in current context.", name);
644 variable: name_not_typename
645 { struct symbol *sym = $1.sym;
649 switch (SYMBOL_CLASS (sym))
657 if (innermost_block == 0 ||
658 contained_in (block_found,
660 innermost_block = block_found;
667 case LOC_CONST_BYTES:
669 /* In this case the expression can
670 be evaluated regardless of what
671 frame we are in, so there is no
672 need to check for the
673 innermost_block. These cases are
674 listed so that gcc -Wall will
675 report types that may not have
680 write_exp_elt_opcode (OP_VAR_VALUE);
681 write_exp_elt_sym (sym);
682 write_exp_elt_opcode (OP_VAR_VALUE);
684 else if ($1.is_a_field_of_this)
686 /* C++: it hangs off of `this'. Must
687 not inadvertently convert from a method call
689 if (innermost_block == 0 ||
690 contained_in (block_found, innermost_block))
691 innermost_block = block_found;
692 write_exp_elt_opcode (OP_THIS);
693 write_exp_elt_opcode (OP_THIS);
694 write_exp_elt_opcode (STRUCTOP_PTR);
695 write_exp_string ($1.stoken);
696 write_exp_elt_opcode (STRUCTOP_PTR);
700 struct minimal_symbol *msymbol;
701 register char *arg = copy_name ($1.stoken);
703 msymbol = lookup_minimal_symbol (arg,
704 (struct objfile *) NULL);
707 write_exp_elt_opcode (OP_LONG);
708 write_exp_elt_type (builtin_type_int);
709 write_exp_elt_longcst ((LONGEST) msymbol -> address);
710 write_exp_elt_opcode (OP_LONG);
711 write_exp_elt_opcode (UNOP_MEMVAL);
712 if (msymbol -> type == mst_data ||
713 msymbol -> type == mst_bss)
714 write_exp_elt_type (builtin_type_int);
715 else if (msymbol -> type == mst_text)
716 write_exp_elt_type (lookup_function_type (builtin_type_int));
718 write_exp_elt_type (builtin_type_char);
719 write_exp_elt_opcode (UNOP_MEMVAL);
721 else if (!have_full_symbols () && !have_partial_symbols ())
722 error ("No symbol table is loaded. Use the \"file\" command.");
724 error ("No symbol \"%s\" in current context.",
725 copy_name ($1.stoken));
734 /* This is where the interesting stuff happens. */
737 struct type *follow_type = $1;
746 follow_type = lookup_pointer_type (follow_type);
749 follow_type = lookup_reference_type (follow_type);
752 array_size = pop_type_int ();
753 if (array_size != -1)
754 follow_type = create_array_type (follow_type,
757 follow_type = lookup_pointer_type (follow_type);
760 follow_type = lookup_function_type (follow_type);
768 { push_type (tp_pointer); $$ = 0; }
770 { push_type (tp_pointer); $$ = $2; }
772 { push_type (tp_reference); $$ = 0; }
774 { push_type (tp_reference); $$ = $2; }
778 direct_abs_decl: '(' abs_decl ')'
780 | direct_abs_decl array_mod
783 push_type (tp_array);
788 push_type (tp_array);
791 | direct_abs_decl func_mod
792 { push_type (tp_function); }
794 { push_type (tp_function); }
805 | '(' nonempty_typelist ')'
806 { free ((PTR)$2); $$ = 0; }
810 | typebase COLONCOLON '*'
811 { $$ = lookup_member_type (builtin_type_int, $1); }
812 | type '(' typebase COLONCOLON '*' ')'
813 { $$ = lookup_member_type ($1, $3); }
814 | type '(' typebase COLONCOLON '*' ')' '(' ')'
815 { $$ = lookup_member_type
816 (lookup_function_type ($1), $3); }
817 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
818 { $$ = lookup_member_type
819 (lookup_function_type ($1), $3);
827 { $$ = builtin_type_int; }
829 { $$ = builtin_type_long; }
831 { $$ = builtin_type_short; }
833 { $$ = builtin_type_long; }
834 | UNSIGNED LONG INT_KEYWORD
835 { $$ = builtin_type_unsigned_long; }
837 { $$ = builtin_type_long_long; }
838 | LONG LONG INT_KEYWORD
839 { $$ = builtin_type_long_long; }
841 { $$ = builtin_type_unsigned_long_long; }
842 | UNSIGNED LONG LONG INT_KEYWORD
843 { $$ = builtin_type_unsigned_long_long; }
845 { $$ = builtin_type_short; }
846 | UNSIGNED SHORT INT_KEYWORD
847 { $$ = builtin_type_unsigned_short; }
849 { $$ = lookup_struct (copy_name ($2),
850 expression_context_block); }
852 { $$ = lookup_union (copy_name ($2),
853 expression_context_block); }
855 { $$ = lookup_enum (copy_name ($2),
856 expression_context_block); }
858 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
860 { $$ = builtin_type_unsigned_int; }
861 | SIGNED_KEYWORD typename
864 { $$ = builtin_type_int; }
865 | TEMPLATE name '<' type '>'
866 { $$ = lookup_template_type(copy_name($2), $4,
867 expression_context_block);
874 $$.stoken.ptr = "int";
875 $$.stoken.length = 3;
876 $$.type = builtin_type_int;
880 $$.stoken.ptr = "long";
881 $$.stoken.length = 4;
882 $$.type = builtin_type_long;
886 $$.stoken.ptr = "short";
887 $$.stoken.length = 5;
888 $$.type = builtin_type_short;
894 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
895 $$[0] = (struct type *)0;
898 | nonempty_typelist ',' type
899 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
900 $$ = (struct type **)xrealloc ((char *) $1, len);
901 $$[$<ivec>$[0]] = $3;
905 name : NAME { $$ = $1.stoken; }
906 | BLOCKNAME { $$ = $1.stoken; }
907 | TYPENAME { $$ = $1.stoken; }
908 | NAME_OR_INT { $$ = $1.stoken; }
909 | NAME_OR_UINT { $$ = $1.stoken; }
912 name_not_typename : NAME
914 /* These would be useful if name_not_typename was useful, but it is just
915 a fake for "variable", so these cause reduce/reduce conflicts because
916 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
917 =exp) or just an exp. If name_not_typename was ever used in an lvalue
918 context where only a name could occur, this might be useful.
926 /* Take care of parsing a number (anything that starts with a digit).
927 Set yylval and return the token type; update lexptr.
928 LEN is the number of characters in it. */
930 /*** Needs some error checking for the float case ***/
933 parse_number (p, len, parsed_float, putithere)
939 register LONGEST n = 0;
940 register LONGEST prevn = 0;
943 register int base = input_radix;
948 /* It's a float since it contains a point or an exponent. */
949 putithere->dval = atof (p);
953 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
987 if (c >= 'A' && c <= 'Z')
989 if (c != 'l' && c != 'u')
991 if (c >= '0' && c <= '9')
995 if (base > 10 && c >= 'a' && c <= 'f')
996 n += i = c - 'a' + 10;
997 else if (len == 0 && c == 'l')
999 else if (len == 0 && c == 'u')
1002 return ERROR; /* Char not a digit */
1005 return ERROR; /* Invalid digit in this base */
1006 /* Portably test for overflow (only works for nonzero values, so make
1007 a second check for zero). */
1008 if((prevn >= n) && n != 0)
1009 unsigned_p=1; /* Try something unsigned */
1010 /* If range checking enabled, portably test for unsigned overflow. */
1011 if(RANGE_CHECK && n!=0)
1013 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
1014 range_error("Overflow on numeric constant.");
1021 putithere->ulval = n;
1026 putithere->lval = n;
1035 enum exp_opcode opcode;
1038 const static struct token tokentab3[] =
1040 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1041 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1044 const static struct token tokentab2[] =
1046 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1047 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1048 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1049 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1050 {"%=", ASSIGN_MODIFY, BINOP_REM},
1051 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1052 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1053 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1054 {"++", INCREMENT, BINOP_END},
1055 {"--", DECREMENT, BINOP_END},
1056 {"->", ARROW, BINOP_END},
1057 {"&&", ANDAND, BINOP_END},
1058 {"||", OROR, BINOP_END},
1059 {"::", COLONCOLON, BINOP_END},
1060 {"<<", LSH, BINOP_END},
1061 {">>", RSH, BINOP_END},
1062 {"==", EQUAL, BINOP_END},
1063 {"!=", NOTEQUAL, BINOP_END},
1064 {"<=", LEQ, BINOP_END},
1065 {">=", GEQ, BINOP_END}
1068 /* Read one token, getting characters through lexptr. */
1074 register int namelen;
1075 register unsigned i;
1076 register char *tokstart;
1081 /* See if it is a special token of length 3. */
1082 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1083 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1086 yylval.opcode = tokentab3[i].opcode;
1087 return tokentab3[i].token;
1090 /* See if it is a special token of length 2. */
1091 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1092 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1095 yylval.opcode = tokentab2[i].opcode;
1096 return tokentab2[i].token;
1099 switch (c = *tokstart)
1114 c = parse_escape (&lexptr);
1118 error ("Invalid character constant.");
1127 if (paren_depth == 0)
1134 if (comma_terminates && paren_depth == 0)
1140 /* Might be a floating point number. */
1141 if (lexptr[1] < '0' || lexptr[1] > '9')
1142 goto symbol; /* Nope, must be a symbol. */
1143 /* FALL THRU into number case. */
1156 /* It's a number. */
1157 int got_dot = 0, got_e = 0, toktype;
1158 register char *p = tokstart;
1159 int hex = input_radix > 10;
1161 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1166 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1174 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1175 got_dot = got_e = 1;
1176 else if (!hex && !got_dot && *p == '.')
1178 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1179 && (*p == '-' || *p == '+'))
1180 /* This is the sign of the exponent, not the end of the
1183 /* We will take any letters or digits. parse_number will
1184 complain if past the radix, or if L or U are not final. */
1185 else if ((*p < '0' || *p > '9')
1186 && ((*p < 'a' || *p > 'z')
1187 && (*p < 'A' || *p > 'Z')))
1190 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1191 if (toktype == ERROR)
1193 char *err_copy = (char *) alloca (p - tokstart + 1);
1195 bcopy (tokstart, err_copy, p - tokstart);
1196 err_copy[p - tokstart] = 0;
1197 error ("Invalid number \"%s\".", err_copy);
1228 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1231 c = tokstart[++namelen];
1232 if (c >= '0' && c <= '9')
1234 c = tokstart[++namelen];
1235 if (c >= '0' && c <= '9')
1236 c = tokstart[++namelen];
1239 yylval.sval.ptr = tokstart + 1;
1240 yylval.sval.length = namelen - 1;
1241 lexptr += namelen + 1;
1245 if (!(c == '_' || c == '$'
1246 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1247 /* We must have come across a bad character (e.g. ';'). */
1248 error ("Invalid character '%c' in expression.", c);
1250 /* It's a name. See how long it is. */
1252 for (c = tokstart[namelen];
1253 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1254 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1255 c = tokstart[++namelen])
1258 /* The token "if" terminates the expression and is NOT
1259 removed from the input stream. */
1260 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1267 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1268 and $$digits (equivalent to $<-digits> if you could type that).
1269 Make token type LAST, and put the number (the digits) in yylval. */
1271 if (*tokstart == '$')
1273 register int negate = 0;
1275 /* Double dollar means negate the number and add -1 as well.
1276 Thus $$ alone means -1. */
1277 if (namelen >= 2 && tokstart[1] == '$')
1284 /* Just dollars (one or two) */
1285 yylval.lval = - negate;
1288 /* Is the rest of the token digits? */
1289 for (; c < namelen; c++)
1290 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1294 yylval.lval = atoi (tokstart + 1 + negate);
1296 yylval.lval = - yylval.lval;
1301 /* Handle tokens that refer to machine registers:
1302 $ followed by a register name. */
1304 if (*tokstart == '$') {
1305 for (c = 0; c < NUM_REGS; c++)
1306 if (namelen - 1 == strlen (reg_names[c])
1307 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1312 for (c = 0; c < num_std_regs; c++)
1313 if (namelen - 1 == strlen (std_regs[c].name)
1314 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1316 yylval.lval = std_regs[c].regnum;
1320 /* Catch specific keywords. Should be done with a data structure. */
1324 if (!strncmp (tokstart, "unsigned", 8))
1326 if (current_language->la_language == language_cplus
1327 && !strncmp (tokstart, "template", 8))
1331 if (!strncmp (tokstart, "struct", 6))
1333 if (!strncmp (tokstart, "signed", 6))
1334 return SIGNED_KEYWORD;
1335 if (!strncmp (tokstart, "sizeof", 6))
1339 if (!strncmp (tokstart, "union", 5))
1341 if (!strncmp (tokstart, "short", 5))
1345 if (!strncmp (tokstart, "enum", 4))
1347 if (!strncmp (tokstart, "long", 4))
1349 if (current_language->la_language == language_cplus
1350 && !strncmp (tokstart, "this", 4))
1352 static const char this_name[] =
1353 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1355 if (lookup_symbol (this_name, expression_context_block,
1356 VAR_NAMESPACE, 0, NULL))
1361 if (!strncmp (tokstart, "int", 3))
1368 yylval.sval.ptr = tokstart;
1369 yylval.sval.length = namelen;
1371 /* Any other names starting in $ are debugger internal variables. */
1373 if (*tokstart == '$')
1375 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1379 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1380 functions or symtabs. If this is not so, then ...
1381 Use token-type TYPENAME for symbols that happen to be defined
1382 currently as names of types; NAME for other symbols.
1383 The caller is not constrained to care about the distinction. */
1385 char *tmp = copy_name (yylval.sval);
1387 int is_a_field_of_this = 0;
1390 sym = lookup_symbol (tmp, expression_context_block,
1392 current_language->la_language == language_cplus
1393 ? &is_a_field_of_this : NULL,
1395 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1396 lookup_partial_symtab (tmp))
1398 yylval.ssym.sym = sym;
1399 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1402 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1404 yylval.tsym.type = SYMBOL_TYPE (sym);
1407 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1410 /* Input names that aren't symbols but ARE valid hex numbers,
1411 when the input radix permits them, can be names or numbers
1412 depending on the parse. Note we support radixes > 16 here. */
1414 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1415 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1417 YYSTYPE newlval; /* Its value is ignored. */
1418 hextype = parse_number (tokstart, namelen, 0, &newlval);
1421 yylval.ssym.sym = sym;
1422 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1425 if (hextype == UINT)
1427 yylval.ssym.sym = sym;
1428 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1429 return NAME_OR_UINT;
1433 /* Any other kind of symbol */
1434 yylval.ssym.sym = sym;
1435 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1444 error (msg ? msg : "Invalid syntax in expression.");
1447 /* Table mapping opcodes into strings for printing operators
1448 and precedences of the operators. */
1450 const static struct op_print c_op_print_tab[] =
1452 {",", BINOP_COMMA, PREC_COMMA, 0},
1453 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1454 {"||", BINOP_OR, PREC_OR, 0},
1455 {"&&", BINOP_AND, PREC_AND, 0},
1456 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1457 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1458 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1459 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1460 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1461 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1462 {">=", BINOP_GEQ, PREC_ORDER, 0},
1463 {">", BINOP_GTR, PREC_ORDER, 0},
1464 {"<", BINOP_LESS, PREC_ORDER, 0},
1465 {">>", BINOP_RSH, PREC_SHIFT, 0},
1466 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1467 {"+", BINOP_ADD, PREC_ADD, 0},
1468 {"-", BINOP_SUB, PREC_ADD, 0},
1469 {"*", BINOP_MUL, PREC_MUL, 0},
1470 {"/", BINOP_DIV, PREC_MUL, 0},
1471 {"%", BINOP_REM, PREC_MUL, 0},
1472 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1473 {"-", UNOP_NEG, PREC_PREFIX, 0},
1474 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1475 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1476 {"*", UNOP_IND, PREC_PREFIX, 0},
1477 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1478 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1479 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1480 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1482 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1485 /* These variables point to the objects
1486 representing the predefined C data types. */
1488 struct type *builtin_type_void;
1489 struct type *builtin_type_char;
1490 struct type *builtin_type_short;
1491 struct type *builtin_type_int;
1492 struct type *builtin_type_long;
1493 struct type *builtin_type_long_long;
1494 struct type *builtin_type_signed_char;
1495 struct type *builtin_type_unsigned_char;
1496 struct type *builtin_type_unsigned_short;
1497 struct type *builtin_type_unsigned_int;
1498 struct type *builtin_type_unsigned_long;
1499 struct type *builtin_type_unsigned_long_long;
1500 struct type *builtin_type_float;
1501 struct type *builtin_type_double;
1502 struct type *builtin_type_long_double;
1503 struct type *builtin_type_complex;
1504 struct type *builtin_type_double_complex;
1506 struct type ** const (c_builtin_types[]) =
1510 &builtin_type_short,
1512 &builtin_type_float,
1513 &builtin_type_double,
1515 &builtin_type_long_long,
1516 &builtin_type_signed_char,
1517 &builtin_type_unsigned_char,
1518 &builtin_type_unsigned_short,
1519 &builtin_type_unsigned_int,
1520 &builtin_type_unsigned_long,
1521 &builtin_type_unsigned_long_long,
1522 &builtin_type_long_double,
1523 &builtin_type_complex,
1524 &builtin_type_double_complex,
1528 const struct language_defn c_language_defn = {
1529 "c", /* Language name */
1536 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1537 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1538 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1539 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1540 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1541 c_op_print_tab, /* expression operators for printing */
1545 const struct language_defn cplus_language_defn = {
1546 "c++", /* Language name */
1553 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1554 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1555 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1556 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1557 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1558 c_op_print_tab, /* expression operators for printing */
1563 _initialize_c_exp ()
1566 init_type (TYPE_CODE_VOID, 1,
1568 "void", (struct objfile *) NULL);
1570 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1572 "char", (struct objfile *) NULL);
1573 builtin_type_signed_char =
1574 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1576 "signed char", (struct objfile *) NULL);
1577 builtin_type_unsigned_char =
1578 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1580 "unsigned char", (struct objfile *) NULL);
1581 builtin_type_short =
1582 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1584 "short", (struct objfile *) NULL);
1585 builtin_type_unsigned_short =
1586 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1588 "unsigned short", (struct objfile *) NULL);
1590 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1592 "int", (struct objfile *) NULL);
1593 builtin_type_unsigned_int =
1594 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1596 "unsigned int", (struct objfile *) NULL);
1598 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1600 "long", (struct objfile *) NULL);
1601 builtin_type_unsigned_long =
1602 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1604 "unsigned long", (struct objfile *) NULL);
1605 builtin_type_long_long =
1606 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1608 "long long", (struct objfile *) NULL);
1609 builtin_type_unsigned_long_long =
1610 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1612 "unsigned long long", (struct objfile *) NULL);
1613 builtin_type_float =
1614 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1616 "float", (struct objfile *) NULL);
1617 builtin_type_double =
1618 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1620 "double", (struct objfile *) NULL);
1621 builtin_type_long_double =
1622 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1624 "long double", (struct objfile *) NULL);
1625 builtin_type_complex =
1626 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1628 "complex", (struct objfile *) NULL);
1629 builtin_type_double_complex =
1630 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1632 "double complex", (struct objfile *) NULL);
1634 add_language (&c_language_defn);
1635 add_language (&cplus_language_defn);