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
79 __yy_bcopy PARAMS ((char *, char *, int));
82 yyparse PARAMS ((void));
85 yylex PARAMS ((void));
88 yyerror PARAMS ((char *));
90 /* #define YYDEBUG 1 */
94 /* Although the yacc "value" of an expression is not used,
95 since the result is stored in the structure being created,
96 other node types do have values. */
101 unsigned LONGEST ulval;
107 struct symtoken ssym;
110 enum exp_opcode opcode;
111 struct internalvar *ivar;
118 /* YYSTYPE gets defined by %union */
120 parse_number PARAMS ((char *, int, int, YYSTYPE *));
123 %type <voidval> exp exp1 type_exp start variable
124 %type <tval> type typebase
125 %type <tvec> nonempty_typelist
126 /* %type <bval> block */
128 /* Fancy type parsing. */
129 %type <voidval> func_mod direct_abs_decl abs_decl
131 %type <lval> array_mod
133 %token <lval> INT CHAR
137 /* Both NAME and TYPENAME tokens represent symbols in the input,
138 and both convey their data as strings.
139 But a TYPENAME is a string that happens to be defined as a typedef
140 or builtin type name (such as int or char)
141 and a NAME is any other symbol.
142 Contexts where this distinction is not important can use the
143 nonterminal "name", which matches either NAME or TYPENAME. */
146 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
147 %token <tsym> TYPENAME
149 %type <ssym> name_not_typename
150 %type <tsym> typename
152 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
153 but which would parse as a valid number in the current input radix.
154 E.g. "c" when input_radix==16. Depending on the parse, it will be
155 turned into a name or into a number. NAME_OR_UINT ditto. */
157 %token <ssym> NAME_OR_INT NAME_OR_UINT
159 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
163 /* Special type cases, put in to allow the parser to distinguish different
165 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD
167 %token <lval> LAST REGNAME
169 %token <ivar> VARIABLE
171 %token <opcode> ASSIGN_MODIFY
178 %right '=' ASSIGN_MODIFY
186 %left '<' '>' LEQ GEQ
191 %right UNARY INCREMENT DECREMENT
192 %right ARROW '.' '[' '('
193 %token <ssym> BLOCKNAME
204 { write_exp_elt_opcode(OP_TYPE);
205 write_exp_elt_type($1);
206 write_exp_elt_opcode(OP_TYPE);}
209 /* Expressions, including the comma operator. */
212 { write_exp_elt_opcode (BINOP_COMMA); }
215 /* Expressions, not including the comma operator. */
216 exp : '*' exp %prec UNARY
217 { write_exp_elt_opcode (UNOP_IND); }
219 exp : '&' exp %prec UNARY
220 { write_exp_elt_opcode (UNOP_ADDR); }
222 exp : '-' exp %prec UNARY
223 { write_exp_elt_opcode (UNOP_NEG); }
226 exp : '!' exp %prec UNARY
227 { write_exp_elt_opcode (UNOP_ZEROP); }
230 exp : '~' exp %prec UNARY
231 { write_exp_elt_opcode (UNOP_LOGNOT); }
234 exp : INCREMENT exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
238 exp : DECREMENT exp %prec UNARY
239 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
242 exp : exp INCREMENT %prec UNARY
243 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
246 exp : exp DECREMENT %prec UNARY
247 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
250 exp : SIZEOF exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_SIZEOF); }
255 { write_exp_elt_opcode (STRUCTOP_PTR);
256 write_exp_string ($3);
257 write_exp_elt_opcode (STRUCTOP_PTR); }
260 exp : exp ARROW '*' exp
261 { write_exp_elt_opcode (STRUCTOP_MPTR); }
265 { write_exp_elt_opcode (STRUCTOP_STRUCT);
266 write_exp_string ($3);
267 write_exp_elt_opcode (STRUCTOP_STRUCT); }
270 exp : exp '.' '*' exp
271 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
274 exp : exp '[' exp1 ']'
275 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
279 /* This is to save the value of arglist_len
280 being accumulated by an outer function call. */
281 { start_arglist (); }
282 arglist ')' %prec ARROW
283 { write_exp_elt_opcode (OP_FUNCALL);
284 write_exp_elt_longcst ((LONGEST) end_arglist ());
285 write_exp_elt_opcode (OP_FUNCALL); }
295 arglist : arglist ',' exp %prec ABOVE_COMMA
299 exp : '{' type '}' exp %prec UNARY
300 { write_exp_elt_opcode (UNOP_MEMVAL);
301 write_exp_elt_type ($2);
302 write_exp_elt_opcode (UNOP_MEMVAL); }
305 exp : '(' type ')' exp %prec UNARY
306 { write_exp_elt_opcode (UNOP_CAST);
307 write_exp_elt_type ($2);
308 write_exp_elt_opcode (UNOP_CAST); }
315 /* Binary operators in order of decreasing precedence. */
318 { write_exp_elt_opcode (BINOP_REPEAT); }
322 { write_exp_elt_opcode (BINOP_MUL); }
326 { write_exp_elt_opcode (BINOP_DIV); }
330 { write_exp_elt_opcode (BINOP_REM); }
334 { write_exp_elt_opcode (BINOP_ADD); }
338 { write_exp_elt_opcode (BINOP_SUB); }
342 { write_exp_elt_opcode (BINOP_LSH); }
346 { write_exp_elt_opcode (BINOP_RSH); }
350 { write_exp_elt_opcode (BINOP_EQUAL); }
353 exp : exp NOTEQUAL exp
354 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
358 { write_exp_elt_opcode (BINOP_LEQ); }
362 { write_exp_elt_opcode (BINOP_GEQ); }
366 { write_exp_elt_opcode (BINOP_LESS); }
370 { write_exp_elt_opcode (BINOP_GTR); }
374 { write_exp_elt_opcode (BINOP_LOGAND); }
378 { write_exp_elt_opcode (BINOP_LOGXOR); }
382 { write_exp_elt_opcode (BINOP_LOGIOR); }
386 { write_exp_elt_opcode (BINOP_AND); }
390 { write_exp_elt_opcode (BINOP_OR); }
393 exp : exp '?' exp ':' exp %prec '?'
394 { write_exp_elt_opcode (TERNOP_COND); }
398 { write_exp_elt_opcode (BINOP_ASSIGN); }
401 exp : exp ASSIGN_MODIFY exp
402 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
403 write_exp_elt_opcode ($2);
404 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
408 { write_exp_elt_opcode (OP_LONG);
409 if ($1 == (int) $1 || $1 == (unsigned int) $1)
410 write_exp_elt_type (builtin_type_int);
412 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
413 write_exp_elt_longcst ((LONGEST) $1);
414 write_exp_elt_opcode (OP_LONG); }
419 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
420 write_exp_elt_opcode (OP_LONG);
421 if (val.lval == (int) val.lval ||
422 val.lval == (unsigned int) val.lval)
423 write_exp_elt_type (builtin_type_int);
425 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
426 write_exp_elt_longcst (val.lval);
427 write_exp_elt_opcode (OP_LONG); }
432 write_exp_elt_opcode (OP_LONG);
433 if ($1 == (unsigned int) $1)
434 write_exp_elt_type (builtin_type_unsigned_int);
436 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
437 write_exp_elt_longcst ((LONGEST) $1);
438 write_exp_elt_opcode (OP_LONG);
444 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
445 write_exp_elt_opcode (OP_LONG);
446 if (val.ulval == (unsigned int) val.ulval)
447 write_exp_elt_type (builtin_type_unsigned_int);
449 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
450 write_exp_elt_longcst ((LONGEST)val.ulval);
451 write_exp_elt_opcode (OP_LONG);
456 { write_exp_elt_opcode (OP_LONG);
457 write_exp_elt_type (builtin_type_char);
458 write_exp_elt_longcst ((LONGEST) $1);
459 write_exp_elt_opcode (OP_LONG); }
463 { write_exp_elt_opcode (OP_DOUBLE);
464 write_exp_elt_type (builtin_type_double);
465 write_exp_elt_dblcst ($1);
466 write_exp_elt_opcode (OP_DOUBLE); }
473 { write_exp_elt_opcode (OP_LAST);
474 write_exp_elt_longcst ((LONGEST) $1);
475 write_exp_elt_opcode (OP_LAST); }
479 { write_exp_elt_opcode (OP_REGISTER);
480 write_exp_elt_longcst ((LONGEST) $1);
481 write_exp_elt_opcode (OP_REGISTER); }
485 { write_exp_elt_opcode (OP_INTERNALVAR);
486 write_exp_elt_intern ($1);
487 write_exp_elt_opcode (OP_INTERNALVAR); }
490 exp : SIZEOF '(' type ')' %prec UNARY
491 { write_exp_elt_opcode (OP_LONG);
492 write_exp_elt_type (builtin_type_int);
493 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
494 write_exp_elt_opcode (OP_LONG); }
498 { write_exp_elt_opcode (OP_STRING);
499 write_exp_string ($1);
500 write_exp_elt_opcode (OP_STRING); }
505 { write_exp_elt_opcode (OP_THIS);
506 write_exp_elt_opcode (OP_THIS); }
514 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
518 lookup_symtab (copy_name ($1.stoken));
520 $$ = BLOCKVECTOR_BLOCK
521 (BLOCKVECTOR (tem), STATIC_BLOCK);
523 error ("No file or function \"%s\".",
524 copy_name ($1.stoken));
529 block : block COLONCOLON name
531 = lookup_symbol (copy_name ($3), $1,
532 VAR_NAMESPACE, 0, NULL);
533 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
534 error ("No function \"%s\" in specified context.",
536 $$ = SYMBOL_BLOCK_VALUE (tem); }
539 variable: block COLONCOLON name
540 { struct symbol *sym;
541 sym = lookup_symbol (copy_name ($3), $1,
542 VAR_NAMESPACE, 0, NULL);
544 error ("No symbol \"%s\" in specified context.",
547 write_exp_elt_opcode (OP_VAR_VALUE);
548 write_exp_elt_sym (sym);
549 write_exp_elt_opcode (OP_VAR_VALUE); }
552 variable: typebase COLONCOLON name
554 struct type *type = $1;
555 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
556 && TYPE_CODE (type) != TYPE_CODE_UNION)
557 error ("`%s' is not defined as an aggregate type.",
560 write_exp_elt_opcode (OP_SCOPE);
561 write_exp_elt_type (type);
562 write_exp_string ($3);
563 write_exp_elt_opcode (OP_SCOPE);
565 | typebase COLONCOLON '~' name
567 struct type *type = $1;
568 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
569 && TYPE_CODE (type) != TYPE_CODE_UNION)
570 error ("`%s' is not defined as an aggregate type.",
573 if (strcmp (type_name_no_tag (type), $4.ptr))
574 error ("invalid destructor `%s::~%s'",
575 type_name_no_tag (type), $4.ptr);
577 write_exp_elt_opcode (OP_SCOPE);
578 write_exp_elt_type (type);
579 write_exp_string ($4);
580 write_exp_elt_opcode (OP_SCOPE);
581 write_exp_elt_opcode (UNOP_LOGNOT);
585 char *name = copy_name ($2);
587 struct minimal_symbol *msymbol;
590 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
593 write_exp_elt_opcode (OP_VAR_VALUE);
594 write_exp_elt_sym (sym);
595 write_exp_elt_opcode (OP_VAR_VALUE);
599 msymbol = lookup_minimal_symbol (name,
600 (struct objfile *) NULL);
603 write_exp_elt_opcode (OP_LONG);
604 write_exp_elt_type (builtin_type_int);
605 write_exp_elt_longcst ((LONGEST) msymbol -> address);
606 write_exp_elt_opcode (OP_LONG);
607 write_exp_elt_opcode (UNOP_MEMVAL);
608 if (msymbol -> type == mst_data ||
609 msymbol -> type == mst_bss)
610 write_exp_elt_type (builtin_type_int);
611 else if (msymbol -> type == mst_text)
612 write_exp_elt_type (lookup_function_type (builtin_type_int));
614 write_exp_elt_type (builtin_type_char);
615 write_exp_elt_opcode (UNOP_MEMVAL);
618 if (!have_full_symbols () && !have_partial_symbols ())
619 error ("No symbol table is loaded. Use the \"file\" command.");
621 error ("No symbol \"%s\" in current context.", name);
625 variable: name_not_typename
626 { struct symbol *sym = $1.sym;
630 switch (SYMBOL_CLASS (sym))
638 if (innermost_block == 0 ||
639 contained_in (block_found,
641 innermost_block = block_found;
648 case LOC_CONST_BYTES:
650 /* In this case the expression can
651 be evaluated regardless of what
652 frame we are in, so there is no
653 need to check for the
654 innermost_block. These cases are
655 listed so that gcc -Wall will
656 report types that may not have
661 write_exp_elt_opcode (OP_VAR_VALUE);
662 write_exp_elt_sym (sym);
663 write_exp_elt_opcode (OP_VAR_VALUE);
665 else if ($1.is_a_field_of_this)
667 /* C++: it hangs off of `this'. Must
668 not inadvertently convert from a method call
670 if (innermost_block == 0 ||
671 contained_in (block_found, innermost_block))
672 innermost_block = block_found;
673 write_exp_elt_opcode (OP_THIS);
674 write_exp_elt_opcode (OP_THIS);
675 write_exp_elt_opcode (STRUCTOP_PTR);
676 write_exp_string ($1.stoken);
677 write_exp_elt_opcode (STRUCTOP_PTR);
681 struct minimal_symbol *msymbol;
682 register char *arg = copy_name ($1.stoken);
684 msymbol = lookup_minimal_symbol (arg,
685 (struct objfile *) NULL);
688 write_exp_elt_opcode (OP_LONG);
689 write_exp_elt_type (builtin_type_int);
690 write_exp_elt_longcst ((LONGEST) msymbol -> address);
691 write_exp_elt_opcode (OP_LONG);
692 write_exp_elt_opcode (UNOP_MEMVAL);
693 if (msymbol -> type == mst_data ||
694 msymbol -> type == mst_bss)
695 write_exp_elt_type (builtin_type_int);
696 else if (msymbol -> type == mst_text)
697 write_exp_elt_type (lookup_function_type (builtin_type_int));
699 write_exp_elt_type (builtin_type_char);
700 write_exp_elt_opcode (UNOP_MEMVAL);
702 else if (!have_full_symbols () && !have_partial_symbols ())
703 error ("No symbol table is loaded. Use the \"file\" command.");
705 error ("No symbol \"%s\" in current context.",
706 copy_name ($1.stoken));
715 /* This is where the interesting stuff happens. */
718 struct type *follow_type = $1;
727 follow_type = lookup_pointer_type (follow_type);
730 follow_type = lookup_reference_type (follow_type);
733 array_size = pop_type_int ();
734 if (array_size != -1)
735 follow_type = create_array_type (follow_type,
738 follow_type = lookup_pointer_type (follow_type);
741 follow_type = lookup_function_type (follow_type);
749 { push_type (tp_pointer); $$ = 0; }
751 { push_type (tp_pointer); $$ = $2; }
753 { push_type (tp_reference); $$ = 0; }
755 { push_type (tp_reference); $$ = $2; }
759 direct_abs_decl: '(' abs_decl ')'
761 | direct_abs_decl array_mod
764 push_type (tp_array);
769 push_type (tp_array);
772 | direct_abs_decl func_mod
773 { push_type (tp_function); }
775 { push_type (tp_function); }
786 | '(' nonempty_typelist ')'
787 { free ($2); $$ = 0; }
791 | typebase COLONCOLON '*'
792 { $$ = lookup_member_type (builtin_type_int, $1); }
793 | type '(' typebase COLONCOLON '*' ')'
794 { $$ = lookup_member_type ($1, $3); }
795 | type '(' typebase COLONCOLON '*' ')' '(' ')'
796 { $$ = lookup_member_type
797 (lookup_function_type ($1), $3); }
798 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
799 { $$ = lookup_member_type
800 (lookup_function_type ($1), $3);
808 { $$ = builtin_type_int; }
810 { $$ = builtin_type_long; }
812 { $$ = builtin_type_short; }
814 { $$ = builtin_type_long; }
815 | UNSIGNED LONG INT_KEYWORD
816 { $$ = builtin_type_unsigned_long; }
818 { $$ = builtin_type_long_long; }
819 | LONG LONG INT_KEYWORD
820 { $$ = builtin_type_long_long; }
822 { $$ = builtin_type_unsigned_long_long; }
823 | UNSIGNED LONG LONG INT_KEYWORD
824 { $$ = builtin_type_unsigned_long_long; }
826 { $$ = builtin_type_short; }
827 | UNSIGNED SHORT INT_KEYWORD
828 { $$ = builtin_type_unsigned_short; }
830 { $$ = lookup_struct (copy_name ($2),
831 expression_context_block); }
833 { $$ = lookup_union (copy_name ($2),
834 expression_context_block); }
836 { $$ = lookup_enum (copy_name ($2),
837 expression_context_block); }
839 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
841 { $$ = builtin_type_unsigned_int; }
842 | SIGNED_KEYWORD typename
845 { $$ = builtin_type_int; }
846 | TEMPLATE name '<' type '>'
847 { $$ = lookup_template_type(copy_name($2), $4,
848 expression_context_block);
855 $$.stoken.ptr = "int";
856 $$.stoken.length = 3;
857 $$.type = builtin_type_int;
861 $$.stoken.ptr = "long";
862 $$.stoken.length = 4;
863 $$.type = builtin_type_long;
867 $$.stoken.ptr = "short";
868 $$.stoken.length = 5;
869 $$.type = builtin_type_short;
875 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
876 $$[0] = (struct type *)0;
879 | nonempty_typelist ',' type
880 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
881 $$ = (struct type **)xrealloc ((char *) $1, len);
882 $$[$<ivec>$[0]] = $3;
886 name : NAME { $$ = $1.stoken; }
887 | BLOCKNAME { $$ = $1.stoken; }
888 | TYPENAME { $$ = $1.stoken; }
889 | NAME_OR_INT { $$ = $1.stoken; }
890 | NAME_OR_UINT { $$ = $1.stoken; }
893 name_not_typename : NAME
895 /* These would be useful if name_not_typename was useful, but it is just
896 a fake for "variable", so these cause reduce/reduce conflicts because
897 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
898 =exp) or just an exp. If name_not_typename was ever used in an lvalue
899 context where only a name could occur, this might be useful.
907 /* Take care of parsing a number (anything that starts with a digit).
908 Set yylval and return the token type; update lexptr.
909 LEN is the number of characters in it. */
911 /*** Needs some error checking for the float case ***/
914 parse_number (p, len, parsed_float, putithere)
920 register LONGEST n = 0;
921 register LONGEST prevn = 0;
924 register int base = input_radix;
929 /* It's a float since it contains a point or an exponent. */
930 putithere->dval = atof (p);
934 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
968 if (c >= 'A' && c <= 'Z')
970 if (c != 'l' && c != 'u')
972 if (c >= '0' && c <= '9')
976 if (base > 10 && c >= 'a' && c <= 'f')
977 n += i = c - 'a' + 10;
978 else if (len == 0 && c == 'l')
980 else if (len == 0 && c == 'u')
983 return ERROR; /* Char not a digit */
986 return ERROR; /* Invalid digit in this base */
987 /* Portably test for overflow (only works for nonzero values, so make
988 a second check for zero). */
989 if((prevn >= n) && n != 0)
990 unsigned_p=1; /* Try something unsigned */
991 /* If range checking enabled, portably test for unsigned overflow. */
992 if(RANGE_CHECK && n!=0)
994 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
995 range_error("Overflow on numeric constant.");
1002 putithere->ulval = n;
1007 putithere->lval = n;
1016 enum exp_opcode opcode;
1019 const static struct token tokentab3[] =
1021 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1022 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1025 const static struct token tokentab2[] =
1027 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1028 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1029 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1030 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1031 {"%=", ASSIGN_MODIFY, BINOP_REM},
1032 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1033 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1034 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1035 {"++", INCREMENT, BINOP_END},
1036 {"--", DECREMENT, BINOP_END},
1037 {"->", ARROW, BINOP_END},
1038 {"&&", ANDAND, BINOP_END},
1039 {"||", OROR, BINOP_END},
1040 {"::", COLONCOLON, BINOP_END},
1041 {"<<", LSH, BINOP_END},
1042 {">>", RSH, BINOP_END},
1043 {"==", EQUAL, BINOP_END},
1044 {"!=", NOTEQUAL, BINOP_END},
1045 {"<=", LEQ, BINOP_END},
1046 {">=", GEQ, BINOP_END}
1049 /* Read one token, getting characters through lexptr. */
1055 register int namelen;
1056 register unsigned i;
1057 register char *tokstart;
1062 /* See if it is a special token of length 3. */
1063 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1064 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1067 yylval.opcode = tokentab3[i].opcode;
1068 return tokentab3[i].token;
1071 /* See if it is a special token of length 2. */
1072 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1073 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1076 yylval.opcode = tokentab2[i].opcode;
1077 return tokentab2[i].token;
1080 switch (c = *tokstart)
1095 c = parse_escape (&lexptr);
1099 error ("Invalid character constant.");
1108 if (paren_depth == 0)
1115 if (comma_terminates && paren_depth == 0)
1121 /* Might be a floating point number. */
1122 if (lexptr[1] < '0' || lexptr[1] > '9')
1123 goto symbol; /* Nope, must be a symbol. */
1124 /* FALL THRU into number case. */
1137 /* It's a number. */
1138 int got_dot = 0, got_e = 0, toktype;
1139 register char *p = tokstart;
1140 int hex = input_radix > 10;
1142 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1147 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1155 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1156 got_dot = got_e = 1;
1157 else if (!hex && !got_dot && *p == '.')
1159 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1160 && (*p == '-' || *p == '+'))
1161 /* This is the sign of the exponent, not the end of the
1164 /* We will take any letters or digits. parse_number will
1165 complain if past the radix, or if L or U are not final. */
1166 else if ((*p < '0' || *p > '9')
1167 && ((*p < 'a' || *p > 'z')
1168 && (*p < 'A' || *p > 'Z')))
1171 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1172 if (toktype == ERROR)
1174 char *err_copy = (char *) alloca (p - tokstart + 1);
1176 bcopy (tokstart, err_copy, p - tokstart);
1177 err_copy[p - tokstart] = 0;
1178 error ("Invalid number \"%s\".", err_copy);
1209 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1212 c = tokstart[++namelen];
1213 if (c >= '0' && c <= '9')
1215 c = tokstart[++namelen];
1216 if (c >= '0' && c <= '9')
1217 c = tokstart[++namelen];
1220 yylval.sval.ptr = tokstart + 1;
1221 yylval.sval.length = namelen - 1;
1222 lexptr += namelen + 1;
1226 if (!(c == '_' || c == '$'
1227 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1228 /* We must have come across a bad character (e.g. ';'). */
1229 error ("Invalid character '%c' in expression.", c);
1231 /* It's a name. See how long it is. */
1233 for (c = tokstart[namelen];
1234 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1235 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1236 c = tokstart[++namelen])
1239 /* The token "if" terminates the expression and is NOT
1240 removed from the input stream. */
1241 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1248 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1249 and $$digits (equivalent to $<-digits> if you could type that).
1250 Make token type LAST, and put the number (the digits) in yylval. */
1252 if (*tokstart == '$')
1254 register int negate = 0;
1256 /* Double dollar means negate the number and add -1 as well.
1257 Thus $$ alone means -1. */
1258 if (namelen >= 2 && tokstart[1] == '$')
1265 /* Just dollars (one or two) */
1266 yylval.lval = - negate;
1269 /* Is the rest of the token digits? */
1270 for (; c < namelen; c++)
1271 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1275 yylval.lval = atoi (tokstart + 1 + negate);
1277 yylval.lval = - yylval.lval;
1282 /* Handle tokens that refer to machine registers:
1283 $ followed by a register name. */
1285 if (*tokstart == '$') {
1286 for (c = 0; c < NUM_REGS; c++)
1287 if (namelen - 1 == strlen (reg_names[c])
1288 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1293 for (c = 0; c < num_std_regs; c++)
1294 if (namelen - 1 == strlen (std_regs[c].name)
1295 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1297 yylval.lval = std_regs[c].regnum;
1301 /* Catch specific keywords. Should be done with a data structure. */
1305 if (!strncmp (tokstart, "unsigned", 8))
1307 if (current_language->la_language == language_cplus
1308 && !strncmp (tokstart, "template", 8))
1312 if (!strncmp (tokstart, "struct", 6))
1314 if (!strncmp (tokstart, "signed", 6))
1315 return SIGNED_KEYWORD;
1316 if (!strncmp (tokstart, "sizeof", 6))
1320 if (!strncmp (tokstart, "union", 5))
1322 if (!strncmp (tokstart, "short", 5))
1326 if (!strncmp (tokstart, "enum", 4))
1328 if (!strncmp (tokstart, "long", 4))
1330 if (current_language->la_language == language_cplus
1331 && !strncmp (tokstart, "this", 4))
1333 static const char this_name[] =
1334 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1336 if (lookup_symbol (this_name, expression_context_block,
1337 VAR_NAMESPACE, 0, NULL))
1342 if (!strncmp (tokstart, "int", 3))
1349 yylval.sval.ptr = tokstart;
1350 yylval.sval.length = namelen;
1352 /* Any other names starting in $ are debugger internal variables. */
1354 if (*tokstart == '$')
1356 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1360 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1361 functions or symtabs. If this is not so, then ...
1362 Use token-type TYPENAME for symbols that happen to be defined
1363 currently as names of types; NAME for other symbols.
1364 The caller is not constrained to care about the distinction. */
1366 char *tmp = copy_name (yylval.sval);
1368 int is_a_field_of_this = 0;
1371 sym = lookup_symbol (tmp, expression_context_block,
1373 current_language->la_language == language_cplus
1374 ? &is_a_field_of_this : NULL,
1376 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1377 lookup_partial_symtab (tmp))
1379 yylval.ssym.sym = sym;
1380 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1383 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1385 yylval.tsym.type = SYMBOL_TYPE (sym);
1388 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1391 /* Input names that aren't symbols but ARE valid hex numbers,
1392 when the input radix permits them, can be names or numbers
1393 depending on the parse. Note we support radixes > 16 here. */
1395 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1396 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1398 YYSTYPE newlval; /* Its value is ignored. */
1399 hextype = parse_number (tokstart, namelen, 0, &newlval);
1402 yylval.ssym.sym = sym;
1403 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1406 if (hextype == UINT)
1408 yylval.ssym.sym = sym;
1409 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1410 return NAME_OR_UINT;
1414 /* Any other kind of symbol */
1415 yylval.ssym.sym = sym;
1416 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1425 error (msg ? msg : "Invalid syntax in expression.");
1428 /* Table mapping opcodes into strings for printing operators
1429 and precedences of the operators. */
1431 const static struct op_print c_op_print_tab[] =
1433 {",", BINOP_COMMA, PREC_COMMA, 0},
1434 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1435 {"||", BINOP_OR, PREC_OR, 0},
1436 {"&&", BINOP_AND, PREC_AND, 0},
1437 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1438 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1439 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1440 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1441 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1442 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1443 {">=", BINOP_GEQ, PREC_ORDER, 0},
1444 {">", BINOP_GTR, PREC_ORDER, 0},
1445 {"<", BINOP_LESS, PREC_ORDER, 0},
1446 {">>", BINOP_RSH, PREC_SHIFT, 0},
1447 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1448 {"+", BINOP_ADD, PREC_ADD, 0},
1449 {"-", BINOP_SUB, PREC_ADD, 0},
1450 {"*", BINOP_MUL, PREC_MUL, 0},
1451 {"/", BINOP_DIV, PREC_MUL, 0},
1452 {"%", BINOP_REM, PREC_MUL, 0},
1453 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1454 {"-", UNOP_NEG, PREC_PREFIX, 0},
1455 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1456 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1457 {"*", UNOP_IND, PREC_PREFIX, 0},
1458 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1459 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1460 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1461 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1463 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1466 /* These variables point to the objects
1467 representing the predefined C data types. */
1469 struct type *builtin_type_void;
1470 struct type *builtin_type_char;
1471 struct type *builtin_type_short;
1472 struct type *builtin_type_int;
1473 struct type *builtin_type_long;
1474 struct type *builtin_type_long_long;
1475 struct type *builtin_type_unsigned_char;
1476 struct type *builtin_type_unsigned_short;
1477 struct type *builtin_type_unsigned_int;
1478 struct type *builtin_type_unsigned_long;
1479 struct type *builtin_type_unsigned_long_long;
1480 struct type *builtin_type_float;
1481 struct type *builtin_type_double;
1482 struct type *builtin_type_long_double;
1483 struct type *builtin_type_complex;
1484 struct type *builtin_type_double_complex;
1486 struct type ** const (c_builtin_types[]) =
1490 &builtin_type_short,
1492 &builtin_type_float,
1493 &builtin_type_double,
1495 &builtin_type_long_long,
1496 &builtin_type_unsigned_char,
1497 &builtin_type_unsigned_short,
1498 &builtin_type_unsigned_int,
1499 &builtin_type_unsigned_long,
1500 &builtin_type_unsigned_long_long,
1501 &builtin_type_long_double,
1502 &builtin_type_complex,
1503 &builtin_type_double_complex,
1507 const struct language_defn c_language_defn = {
1508 "c", /* Language name */
1515 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1516 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1517 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1518 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1519 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1520 c_op_print_tab, /* expression operators for printing */
1524 const struct language_defn cplus_language_defn = {
1525 "c++", /* Language name */
1532 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1533 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1534 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1535 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1536 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1537 c_op_print_tab, /* expression operators for printing */
1542 _initialize_c_exp ()
1545 init_type (TYPE_CODE_VOID, 1,
1547 "void", (struct objfile *) NULL);
1549 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1551 "char", (struct objfile *) NULL);
1552 builtin_type_unsigned_char =
1553 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1555 "unsigned char", (struct objfile *) NULL);
1556 builtin_type_short =
1557 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1559 "short", (struct objfile *) NULL);
1560 builtin_type_unsigned_short =
1561 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1563 "unsigned short", (struct objfile *) NULL);
1565 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1567 "int", (struct objfile *) NULL);
1568 builtin_type_unsigned_int =
1569 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1571 "unsigned int", (struct objfile *) NULL);
1573 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1575 "long", (struct objfile *) NULL);
1576 builtin_type_unsigned_long =
1577 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1579 "unsigned long", (struct objfile *) NULL);
1580 builtin_type_long_long =
1581 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1583 "long long", (struct objfile *) NULL);
1584 builtin_type_unsigned_long_long =
1585 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1587 "unsigned long long", (struct objfile *) NULL);
1588 builtin_type_float =
1589 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1591 "float", (struct objfile *) NULL);
1592 builtin_type_double =
1593 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1595 "double", (struct objfile *) NULL);
1596 builtin_type_long_double =
1597 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1599 "long double", (struct objfile *) NULL);
1600 builtin_type_complex =
1601 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1603 "complex", (struct objfile *) NULL);
1604 builtin_type_double_complex =
1605 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1607 "double complex", (struct objfile *) NULL);
1609 add_language (&c_language_defn);
1610 add_language (&cplus_language_defn);