1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Parse a C expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result. */
37 #include "expression.h"
38 #include "parser-defs.h"
45 /* If current_type is non-NULL, it is a signal to the lexer that we have
46 just parsed: 'TYPE ::' and so if an identifier is seen, the lexer must
47 search for it in TYPE. This lex-time search is needed to parse
48 C++ nested types, as in: 'TYPE :: NESTED_TYPE', since this must
49 parse as a type, not a (non-type) identifier. */
51 static struct type *current_type = NULL;
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 typebase_coloncolon qualified_type
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
155 %token <tval> NESTED_TYPE
157 %type <ssym> name_not_typename
158 %type <tsym> typename
160 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
161 but which would parse as a valid number in the current input radix.
162 E.g. "c" when input_radix==16. Depending on the parse, it will be
163 turned into a name or into a number. NAME_OR_UINT ditto. */
165 %token <ssym> NAME_OR_INT NAME_OR_UINT
167 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
171 /* Special type cases, put in to allow the parser to distinguish different
173 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD
175 %token <lval> LAST REGNAME
177 %token <ivar> VARIABLE
179 %token <opcode> ASSIGN_MODIFY
186 %right '=' ASSIGN_MODIFY
194 %left '<' '>' LEQ GEQ
199 %right UNARY INCREMENT DECREMENT
200 %right ARROW '.' '[' '('
201 %token <ssym> BLOCKNAME
206 /* Ensure that if the generated parser contains any calls to malloc/realloc,
207 that they get mapped to xmalloc/xrealloc. We have to do this here
208 rather than earlier in the file because this is the first point after
209 the place where the SVR4 yacc includes <malloc.h>, and if we do it
210 before that, then the remapped declarations in <malloc.h> will collide
211 with the ones in "defs.h". */
213 #define malloc xmalloc
214 #define realloc xrealloc
225 { write_exp_elt_opcode(OP_TYPE);
226 write_exp_elt_type($1);
227 write_exp_elt_opcode(OP_TYPE);}
230 /* Expressions, including the comma operator. */
233 { write_exp_elt_opcode (BINOP_COMMA); }
236 /* Expressions, not including the comma operator. */
237 exp : '*' exp %prec UNARY
238 { write_exp_elt_opcode (UNOP_IND); }
240 exp : '&' exp %prec UNARY
241 { write_exp_elt_opcode (UNOP_ADDR); }
243 exp : '-' exp %prec UNARY
244 { write_exp_elt_opcode (UNOP_NEG); }
247 exp : '!' exp %prec UNARY
248 { write_exp_elt_opcode (UNOP_ZEROP); }
251 exp : '~' exp %prec UNARY
252 { write_exp_elt_opcode (UNOP_LOGNOT); }
255 exp : INCREMENT exp %prec UNARY
256 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
259 exp : DECREMENT exp %prec UNARY
260 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
263 exp : exp INCREMENT %prec UNARY
264 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
267 exp : exp DECREMENT %prec UNARY
268 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
271 exp : SIZEOF exp %prec UNARY
272 { write_exp_elt_opcode (UNOP_SIZEOF); }
276 { write_exp_elt_opcode (STRUCTOP_PTR);
277 write_exp_elt_type (NULL);
278 write_exp_string ($3);
279 write_exp_elt_opcode (STRUCTOP_PTR); }
282 exp : exp ARROW typebase_coloncolon name
283 { write_exp_elt_opcode (STRUCTOP_PTR);
284 write_exp_elt_type ($3);
285 write_exp_string ($4);
286 write_exp_elt_opcode (STRUCTOP_PTR); }
288 exp : exp ARROW '*' exp
289 { write_exp_elt_opcode (STRUCTOP_MPTR); }
293 { write_exp_elt_opcode (STRUCTOP_STRUCT);
294 write_exp_elt_type (NULL);
295 write_exp_string ($3);
296 write_exp_elt_opcode (STRUCTOP_STRUCT); }
299 exp : exp '.' typebase_coloncolon name
300 { write_exp_elt_opcode (STRUCTOP_STRUCT);
301 write_exp_elt_type ($3);
302 write_exp_string ($4);
303 write_exp_elt_opcode (STRUCTOP_STRUCT); }
306 exp : exp '.' '*' exp
307 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
310 exp : exp '[' exp1 ']'
311 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
315 /* This is to save the value of arglist_len
316 being accumulated by an outer function call. */
317 { start_arglist (); }
318 arglist ')' %prec ARROW
319 { write_exp_elt_opcode (OP_FUNCALL);
320 write_exp_elt_longcst ((LONGEST) end_arglist ());
321 write_exp_elt_opcode (OP_FUNCALL); }
331 arglist : arglist ',' exp %prec ABOVE_COMMA
335 exp : '{' type '}' exp %prec UNARY
336 { write_exp_elt_opcode (UNOP_MEMVAL);
337 write_exp_elt_type ($2);
338 write_exp_elt_opcode (UNOP_MEMVAL); }
341 exp : '(' type ')' exp %prec UNARY
342 { write_exp_elt_opcode (UNOP_CAST);
343 write_exp_elt_type ($2);
344 write_exp_elt_opcode (UNOP_CAST); }
351 /* Binary operators in order of decreasing precedence. */
354 { write_exp_elt_opcode (BINOP_REPEAT); }
358 { write_exp_elt_opcode (BINOP_MUL); }
362 { write_exp_elt_opcode (BINOP_DIV); }
366 { write_exp_elt_opcode (BINOP_REM); }
370 { write_exp_elt_opcode (BINOP_ADD); }
374 { write_exp_elt_opcode (BINOP_SUB); }
378 { write_exp_elt_opcode (BINOP_LSH); }
382 { write_exp_elt_opcode (BINOP_RSH); }
386 { write_exp_elt_opcode (BINOP_EQUAL); }
389 exp : exp NOTEQUAL exp
390 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
394 { write_exp_elt_opcode (BINOP_LEQ); }
398 { write_exp_elt_opcode (BINOP_GEQ); }
402 { write_exp_elt_opcode (BINOP_LESS); }
406 { write_exp_elt_opcode (BINOP_GTR); }
410 { write_exp_elt_opcode (BINOP_LOGAND); }
414 { write_exp_elt_opcode (BINOP_LOGXOR); }
418 { write_exp_elt_opcode (BINOP_LOGIOR); }
422 { write_exp_elt_opcode (BINOP_AND); }
426 { write_exp_elt_opcode (BINOP_OR); }
429 exp : exp '?' exp ':' exp %prec '?'
430 { write_exp_elt_opcode (TERNOP_COND); }
434 { write_exp_elt_opcode (BINOP_ASSIGN); }
437 exp : exp ASSIGN_MODIFY exp
438 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
439 write_exp_elt_opcode ($2);
440 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
444 { write_exp_elt_opcode (OP_LONG);
445 if ($1 == (int) $1 || $1 == (unsigned int) $1)
446 write_exp_elt_type (builtin_type_int);
448 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
449 write_exp_elt_longcst ((LONGEST) $1);
450 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.lval == (int) val.lval ||
458 val.lval == (unsigned int) val.lval)
459 write_exp_elt_type (builtin_type_int);
461 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
462 write_exp_elt_longcst (val.lval);
463 write_exp_elt_opcode (OP_LONG); }
468 write_exp_elt_opcode (OP_LONG);
469 if ($1 == (unsigned int) $1)
470 write_exp_elt_type (builtin_type_unsigned_int);
472 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
473 write_exp_elt_longcst ((LONGEST) $1);
474 write_exp_elt_opcode (OP_LONG);
480 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
481 write_exp_elt_opcode (OP_LONG);
482 if (val.ulval == (unsigned int) val.ulval)
483 write_exp_elt_type (builtin_type_unsigned_int);
485 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
486 write_exp_elt_longcst ((LONGEST)val.ulval);
487 write_exp_elt_opcode (OP_LONG);
492 { write_exp_elt_opcode (OP_LONG);
493 write_exp_elt_type (builtin_type_char);
494 write_exp_elt_longcst ((LONGEST) $1);
495 write_exp_elt_opcode (OP_LONG); }
499 { write_exp_elt_opcode (OP_DOUBLE);
500 write_exp_elt_type (builtin_type_double);
501 write_exp_elt_dblcst ($1);
502 write_exp_elt_opcode (OP_DOUBLE); }
509 { write_exp_elt_opcode (OP_LAST);
510 write_exp_elt_longcst ((LONGEST) $1);
511 write_exp_elt_opcode (OP_LAST); }
515 { write_exp_elt_opcode (OP_REGISTER);
516 write_exp_elt_longcst ((LONGEST) $1);
517 write_exp_elt_opcode (OP_REGISTER); }
521 { write_exp_elt_opcode (OP_INTERNALVAR);
522 write_exp_elt_intern ($1);
523 write_exp_elt_opcode (OP_INTERNALVAR); }
526 exp : SIZEOF '(' type ')' %prec UNARY
527 { write_exp_elt_opcode (OP_LONG);
528 write_exp_elt_type (builtin_type_int);
529 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
530 write_exp_elt_opcode (OP_LONG); }
534 { write_exp_elt_opcode (OP_STRING);
535 write_exp_string ($1);
536 write_exp_elt_opcode (OP_STRING); }
541 { write_exp_elt_opcode (OP_THIS);
542 write_exp_elt_opcode (OP_THIS); }
550 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
554 lookup_symtab (copy_name ($1.stoken));
556 $$ = BLOCKVECTOR_BLOCK
557 (BLOCKVECTOR (tem), STATIC_BLOCK);
559 error ("No file or function \"%s\".",
560 copy_name ($1.stoken));
565 block : block COLONCOLON name
567 = lookup_symbol (copy_name ($3), $1,
568 VAR_NAMESPACE, 0, NULL);
569 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
570 error ("No function \"%s\" in specified context.",
572 $$ = SYMBOL_BLOCK_VALUE (tem); }
575 variable: block COLONCOLON name
576 { struct symbol *sym;
577 sym = lookup_symbol (copy_name ($3), $1,
578 VAR_NAMESPACE, 0, NULL);
580 error ("No symbol \"%s\" in specified context.",
583 write_exp_elt_opcode (OP_VAR_VALUE);
584 write_exp_elt_sym (sym);
585 write_exp_elt_opcode (OP_VAR_VALUE); }
588 typebase_coloncolon : typebase COLONCOLON { current_type = $1; $$ = $1; }
590 qualified_name: typebase_coloncolon name
592 struct type *type = $1;
593 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
594 && TYPE_CODE (type) != TYPE_CODE_UNION)
595 error ("`%s' is not defined as an aggregate type.",
598 write_exp_elt_opcode (OP_SCOPE);
599 write_exp_elt_type (type);
600 write_exp_string ($2);
601 write_exp_elt_opcode (OP_SCOPE);
604 | typebase_coloncolon '~' name
606 struct type *type = $1;
607 struct stoken tmp_token;
608 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
609 && TYPE_CODE (type) != TYPE_CODE_UNION)
610 error ("`%s' is not defined as an aggregate type.",
613 if (strcmp (type_name_no_tag (type), $3.ptr))
614 error ("invalid destructor `%s::~%s'",
615 type_name_no_tag (type), $3.ptr);
617 tmp_token.ptr = (char*) alloca ($3.length + 2);
618 tmp_token.length = $3.length + 1;
619 tmp_token.ptr[0] = '~';
620 memcpy (tmp_token.ptr+1, $3.ptr, $3.length);
621 tmp_token.ptr[tmp_token.length] = 0;
622 write_exp_elt_opcode (OP_SCOPE);
623 write_exp_elt_type (type);
624 write_exp_string (tmp_token);
625 write_exp_elt_opcode (OP_SCOPE);
630 variable: qualified_name
633 char *name = copy_name ($2);
635 struct minimal_symbol *msymbol;
638 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
641 write_exp_elt_opcode (OP_VAR_VALUE);
642 write_exp_elt_sym (sym);
643 write_exp_elt_opcode (OP_VAR_VALUE);
647 msymbol = lookup_minimal_symbol (name,
648 (struct objfile *) NULL);
651 write_exp_elt_opcode (OP_LONG);
652 write_exp_elt_type (builtin_type_int);
653 write_exp_elt_longcst ((LONGEST) msymbol -> address);
654 write_exp_elt_opcode (OP_LONG);
655 write_exp_elt_opcode (UNOP_MEMVAL);
656 if (msymbol -> type == mst_data ||
657 msymbol -> type == mst_bss)
658 write_exp_elt_type (builtin_type_int);
659 else if (msymbol -> type == mst_text)
660 write_exp_elt_type (lookup_function_type (builtin_type_int));
662 write_exp_elt_type (builtin_type_char);
663 write_exp_elt_opcode (UNOP_MEMVAL);
666 if (!have_full_symbols () && !have_partial_symbols ())
667 error ("No symbol table is loaded. Use the \"file\" command.");
669 error ("No symbol \"%s\" in current context.", name);
673 variable: name_not_typename
674 { struct symbol *sym = $1.sym;
678 switch (SYMBOL_CLASS (sym))
686 if (innermost_block == 0 ||
687 contained_in (block_found,
689 innermost_block = block_found;
696 case LOC_CONST_BYTES:
698 /* In this case the expression can
699 be evaluated regardless of what
700 frame we are in, so there is no
701 need to check for the
702 innermost_block. These cases are
703 listed so that gcc -Wall will
704 report types that may not have
709 write_exp_elt_opcode (OP_VAR_VALUE);
710 write_exp_elt_sym (sym);
711 write_exp_elt_opcode (OP_VAR_VALUE);
713 else if ($1.is_a_field_of_this)
715 /* C++: it hangs off of `this'. Must
716 not inadvertently convert from a method call
718 if (innermost_block == 0 ||
719 contained_in (block_found, innermost_block))
720 innermost_block = block_found;
721 write_exp_elt_opcode (OP_THIS);
722 write_exp_elt_opcode (OP_THIS);
723 write_exp_elt_opcode (STRUCTOP_PTR);
724 write_exp_string ($1.stoken);
725 write_exp_elt_opcode (STRUCTOP_PTR);
729 struct minimal_symbol *msymbol;
730 register char *arg = copy_name ($1.stoken);
732 msymbol = lookup_minimal_symbol (arg,
733 (struct objfile *) NULL);
736 write_exp_elt_opcode (OP_LONG);
737 write_exp_elt_type (builtin_type_int);
738 write_exp_elt_longcst ((LONGEST) msymbol -> address);
739 write_exp_elt_opcode (OP_LONG);
740 write_exp_elt_opcode (UNOP_MEMVAL);
741 if (msymbol -> type == mst_data ||
742 msymbol -> type == mst_bss)
743 write_exp_elt_type (builtin_type_int);
744 else if (msymbol -> type == mst_text)
745 write_exp_elt_type (lookup_function_type (builtin_type_int));
747 write_exp_elt_type (builtin_type_char);
748 write_exp_elt_opcode (UNOP_MEMVAL);
750 else if (!have_full_symbols () && !have_partial_symbols ())
751 error ("No symbol table is loaded. Use the \"file\" command.");
753 error ("No symbol \"%s\" in current context.",
754 copy_name ($1.stoken));
763 /* This is where the interesting stuff happens. */
766 struct type *follow_type = $1;
775 follow_type = lookup_pointer_type (follow_type);
778 follow_type = lookup_reference_type (follow_type);
781 array_size = pop_type_int ();
782 if (array_size != -1)
783 follow_type = create_array_type (follow_type,
786 follow_type = lookup_pointer_type (follow_type);
789 follow_type = lookup_function_type (follow_type);
797 { push_type (tp_pointer); $$ = 0; }
799 { push_type (tp_pointer); $$ = $2; }
801 { push_type (tp_reference); $$ = 0; }
803 { push_type (tp_reference); $$ = $2; }
807 direct_abs_decl: '(' abs_decl ')'
809 | direct_abs_decl array_mod
812 push_type (tp_array);
817 push_type (tp_array);
820 | direct_abs_decl func_mod
821 { push_type (tp_function); }
823 { push_type (tp_function); }
834 | '(' nonempty_typelist ')'
835 { free ((PTR)$2); $$ = 0; }
838 qualified_type: typebase_coloncolon NESTED_TYPE
839 { $$ = $2; current_type = NULL; }
844 | typebase_coloncolon '*'
845 { $$ = lookup_member_type (builtin_type_int, $1);
846 current_type = NULL; }
847 | type '(' typebase COLONCOLON '*' ')'
848 { $$ = lookup_member_type ($1, $3); }
849 | type '(' typebase COLONCOLON '*' ')' '(' ')'
850 { $$ = lookup_member_type
851 (lookup_function_type ($1), $3); }
852 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
853 { $$ = lookup_member_type
854 (lookup_function_type ($1), $3);
856 /* "const" and "volatile" are curently ignored. */
857 | CONST_KEYWORD type { $$ = $2; }
858 | VOLATILE_KEYWORD type { $$ = $2; }
865 { $$ = builtin_type_int; }
867 { $$ = builtin_type_long; }
869 { $$ = builtin_type_short; }
871 { $$ = builtin_type_long; }
872 | UNSIGNED LONG INT_KEYWORD
873 { $$ = builtin_type_unsigned_long; }
875 { $$ = builtin_type_long_long; }
876 | LONG LONG INT_KEYWORD
877 { $$ = builtin_type_long_long; }
879 { $$ = builtin_type_unsigned_long_long; }
880 | UNSIGNED LONG LONG INT_KEYWORD
881 { $$ = builtin_type_unsigned_long_long; }
883 { $$ = builtin_type_short; }
884 | UNSIGNED SHORT INT_KEYWORD
885 { $$ = builtin_type_unsigned_short; }
887 { $$ = lookup_struct (copy_name ($2),
888 expression_context_block); }
890 { $$ = lookup_struct (copy_name ($2),
891 expression_context_block); }
893 { $$ = lookup_union (copy_name ($2),
894 expression_context_block); }
896 { $$ = lookup_enum (copy_name ($2),
897 expression_context_block); }
898 | STRUCT qualified_type { $$ = check_struct ($2); }
899 | CLASS qualified_type { $$ = check_struct ($2); }
900 | UNION qualified_type { $$ = check_union ($2); }
901 | ENUM qualified_type { $$ = check_enum ($2); }
903 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
905 { $$ = builtin_type_unsigned_int; }
906 | SIGNED_KEYWORD typename
907 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
909 { $$ = builtin_type_int; }
910 | TEMPLATE name '<' type '>'
911 { $$ = lookup_template_type(copy_name($2), $4,
912 expression_context_block);
919 $$.stoken.ptr = "int";
920 $$.stoken.length = 3;
921 $$.type = builtin_type_int;
925 $$.stoken.ptr = "long";
926 $$.stoken.length = 4;
927 $$.type = builtin_type_long;
931 $$.stoken.ptr = "short";
932 $$.stoken.length = 5;
933 $$.type = builtin_type_short;
939 { $$ = (struct type **) xmalloc (sizeof (struct type *) * 2);
940 $<ivec>$[0] = 1; /* Number of types in vector */
943 | nonempty_typelist ',' type
944 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
945 $$ = (struct type **) xrealloc ((char *) $1, len);
946 $$[$<ivec>$[0]] = $3;
950 name : NAME { $$ = $1.stoken; }
951 | BLOCKNAME { $$ = $1.stoken; }
952 | TYPENAME { $$ = $1.stoken; }
953 | NAME_OR_INT { $$ = $1.stoken; }
954 | NAME_OR_UINT { $$ = $1.stoken; }
957 name_not_typename : NAME
959 /* These would be useful if name_not_typename was useful, but it is just
960 a fake for "variable", so these cause reduce/reduce conflicts because
961 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
962 =exp) or just an exp. If name_not_typename was ever used in an lvalue
963 context where only a name could occur, this might be useful.
971 /* Take care of parsing a number (anything that starts with a digit).
972 Set yylval and return the token type; update lexptr.
973 LEN is the number of characters in it. */
975 /*** Needs some error checking for the float case ***/
978 parse_number (p, len, parsed_float, putithere)
984 register LONGEST n = 0;
985 register LONGEST prevn = 0;
988 register int base = input_radix;
993 /* It's a float since it contains a point or an exponent. */
994 putithere->dval = atof (p);
998 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1032 if (c >= 'A' && c <= 'Z')
1034 if (c != 'l' && c != 'u')
1036 if (c >= '0' && c <= '9')
1040 if (base > 10 && c >= 'a' && c <= 'f')
1041 n += i = c - 'a' + 10;
1042 else if (len == 0 && c == 'l')
1044 else if (len == 0 && c == 'u')
1047 return ERROR; /* Char not a digit */
1050 return ERROR; /* Invalid digit in this base */
1051 /* Portably test for overflow (only works for nonzero values, so make
1052 a second check for zero). */
1053 if((prevn >= n) && n != 0)
1054 unsigned_p=1; /* Try something unsigned */
1055 /* If range checking enabled, portably test for unsigned overflow. */
1056 if(RANGE_CHECK && n!=0)
1058 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
1059 range_error("Overflow on numeric constant.");
1066 putithere->ulval = n;
1071 putithere->lval = n;
1080 enum exp_opcode opcode;
1083 const static struct token tokentab3[] =
1085 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1086 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1089 const static struct token tokentab2[] =
1091 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1092 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1093 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1094 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1095 {"%=", ASSIGN_MODIFY, BINOP_REM},
1096 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1097 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1098 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1099 {"++", INCREMENT, BINOP_END},
1100 {"--", DECREMENT, BINOP_END},
1101 {"->", ARROW, BINOP_END},
1102 {"&&", ANDAND, BINOP_END},
1103 {"||", OROR, BINOP_END},
1104 {"::", COLONCOLON, BINOP_END},
1105 {"<<", LSH, BINOP_END},
1106 {">>", RSH, BINOP_END},
1107 {"==", EQUAL, BINOP_END},
1108 {"!=", NOTEQUAL, BINOP_END},
1109 {"<=", LEQ, BINOP_END},
1110 {">=", GEQ, BINOP_END}
1113 /* Read one token, getting characters through lexptr. */
1119 register int namelen;
1120 register unsigned i;
1121 register char *tokstart;
1126 /* See if it is a special token of length 3. */
1127 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1128 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1131 yylval.opcode = tokentab3[i].opcode;
1132 return tokentab3[i].token;
1135 /* See if it is a special token of length 2. */
1136 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1137 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1140 yylval.opcode = tokentab2[i].opcode;
1141 return tokentab2[i].token;
1144 switch (c = *tokstart)
1156 /* We either have a character constant ('0' or '\177' for example)
1157 or we have a quoted symbol reference ('foo(int,int)' in C++
1162 c = parse_escape (&lexptr);
1167 namelen = skip_quoted (tokstart) - tokstart;
1170 lexptr = tokstart + namelen;
1175 error ("Invalid character constant.");
1185 if (paren_depth == 0)
1192 if (comma_terminates && paren_depth == 0)
1198 /* Might be a floating point number. */
1199 if (lexptr[1] < '0' || lexptr[1] > '9')
1200 goto symbol; /* Nope, must be a symbol. */
1201 /* FALL THRU into number case. */
1214 /* It's a number. */
1215 int got_dot = 0, got_e = 0, toktype;
1216 register char *p = tokstart;
1217 int hex = input_radix > 10;
1219 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1224 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1232 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1233 got_dot = got_e = 1;
1234 else if (!hex && !got_dot && *p == '.')
1236 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1237 && (*p == '-' || *p == '+'))
1238 /* This is the sign of the exponent, not the end of the
1241 /* We will take any letters or digits. parse_number will
1242 complain if past the radix, or if L or U are not final. */
1243 else if ((*p < '0' || *p > '9')
1244 && ((*p < 'a' || *p > 'z')
1245 && (*p < 'A' || *p > 'Z')))
1248 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1249 if (toktype == ERROR)
1251 char *err_copy = (char *) alloca (p - tokstart + 1);
1253 memcpy (err_copy, tokstart, p - tokstart);
1254 err_copy[p - tokstart] = 0;
1255 error ("Invalid number \"%s\".", err_copy);
1286 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1289 c = tokstart[++namelen];
1290 if (c >= '0' && c <= '9')
1292 c = tokstart[++namelen];
1293 if (c >= '0' && c <= '9')
1294 c = tokstart[++namelen];
1297 yylval.sval.ptr = tokstart + 1;
1298 yylval.sval.length = namelen - 1;
1299 lexptr += namelen + 1;
1303 if (!(c == '_' || c == '$'
1304 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1305 /* We must have come across a bad character (e.g. ';'). */
1306 error ("Invalid character '%c' in expression.", c);
1308 /* It's a name. See how long it is. */
1310 for (c = tokstart[namelen];
1311 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1312 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1313 c = tokstart[++namelen])
1316 /* The token "if" terminates the expression and is NOT
1317 removed from the input stream. */
1318 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1325 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1326 and $$digits (equivalent to $<-digits> if you could type that).
1327 Make token type LAST, and put the number (the digits) in yylval. */
1330 if (*tokstart == '$')
1332 register int negate = 0;
1334 /* Double dollar means negate the number and add -1 as well.
1335 Thus $$ alone means -1. */
1336 if (namelen >= 2 && tokstart[1] == '$')
1343 /* Just dollars (one or two) */
1344 yylval.lval = - negate;
1347 /* Is the rest of the token digits? */
1348 for (; c < namelen; c++)
1349 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1353 yylval.lval = atoi (tokstart + 1 + negate);
1355 yylval.lval = - yylval.lval;
1360 /* Handle tokens that refer to machine registers:
1361 $ followed by a register name. */
1363 if (*tokstart == '$') {
1364 for (c = 0; c < NUM_REGS; c++)
1365 if (namelen - 1 == strlen (reg_names[c])
1366 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1371 for (c = 0; c < num_std_regs; c++)
1372 if (namelen - 1 == strlen (std_regs[c].name)
1373 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1375 yylval.lval = std_regs[c].regnum;
1379 /* Catch specific keywords. Should be done with a data structure. */
1383 if (!strncmp (tokstart, "unsigned", 8))
1385 if (current_language->la_language == language_cplus
1386 && !strncmp (tokstart, "template", 8))
1388 if (!strncmp (tokstart, "volatile", 8))
1389 return VOLATILE_KEYWORD;
1392 if (!strncmp (tokstart, "struct", 6))
1394 if (!strncmp (tokstart, "signed", 6))
1395 return SIGNED_KEYWORD;
1396 if (!strncmp (tokstart, "sizeof", 6))
1400 if (current_language->la_language == language_cplus
1401 && !strncmp (tokstart, "class", 5))
1403 if (!strncmp (tokstart, "union", 5))
1405 if (!strncmp (tokstart, "short", 5))
1407 if (!strncmp (tokstart, "const", 5))
1408 return CONST_KEYWORD;
1411 if (!strncmp (tokstart, "enum", 4))
1413 if (!strncmp (tokstart, "long", 4))
1415 if (current_language->la_language == language_cplus
1416 && !strncmp (tokstart, "this", 4))
1418 static const char this_name[] =
1419 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1421 if (lookup_symbol (this_name, expression_context_block,
1422 VAR_NAMESPACE, 0, NULL))
1427 if (!strncmp (tokstart, "int", 3))
1434 yylval.sval.ptr = tokstart;
1435 yylval.sval.length = namelen;
1437 /* Any other names starting in $ are debugger internal variables. */
1439 if (*tokstart == '$')
1441 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1448 find_nested_type (current_type, copy_name (yylval.sval));
1456 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1457 functions or symtabs. If this is not so, then ...
1458 Use token-type TYPENAME for symbols that happen to be defined
1459 currently as names of types; NAME for other symbols.
1460 The caller is not constrained to care about the distinction. */
1462 char *tmp = copy_name (yylval.sval);
1464 int is_a_field_of_this = 0;
1467 sym = lookup_symbol (tmp, expression_context_block,
1469 current_language->la_language == language_cplus
1470 ? &is_a_field_of_this : NULL,
1472 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1473 lookup_partial_symtab (tmp))
1475 yylval.ssym.sym = sym;
1476 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1479 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1481 yylval.tsym.type = SYMBOL_TYPE (sym);
1484 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1487 /* Input names that aren't symbols but ARE valid hex numbers,
1488 when the input radix permits them, can be names or numbers
1489 depending on the parse. Note we support radixes > 16 here. */
1491 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1492 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1494 YYSTYPE newlval; /* Its value is ignored. */
1495 hextype = parse_number (tokstart, namelen, 0, &newlval);
1498 yylval.ssym.sym = sym;
1499 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1502 if (hextype == UINT)
1504 yylval.ssym.sym = sym;
1505 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1506 return NAME_OR_UINT;
1510 /* Any other kind of symbol */
1511 yylval.ssym.sym = sym;
1512 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1521 error (msg ? msg : "Invalid syntax in expression.");
1524 /* Table mapping opcodes into strings for printing operators
1525 and precedences of the operators. */
1527 const static struct op_print c_op_print_tab[] =
1529 {",", BINOP_COMMA, PREC_COMMA, 0},
1530 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1531 {"||", BINOP_OR, PREC_OR, 0},
1532 {"&&", BINOP_AND, PREC_AND, 0},
1533 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1534 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1535 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1536 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1537 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1538 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1539 {">=", BINOP_GEQ, PREC_ORDER, 0},
1540 {">", BINOP_GTR, PREC_ORDER, 0},
1541 {"<", BINOP_LESS, PREC_ORDER, 0},
1542 {">>", BINOP_RSH, PREC_SHIFT, 0},
1543 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1544 {"+", BINOP_ADD, PREC_ADD, 0},
1545 {"-", BINOP_SUB, PREC_ADD, 0},
1546 {"*", BINOP_MUL, PREC_MUL, 0},
1547 {"/", BINOP_DIV, PREC_MUL, 0},
1548 {"%", BINOP_REM, PREC_MUL, 0},
1549 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1550 {"-", UNOP_NEG, PREC_PREFIX, 0},
1551 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1552 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1553 {"*", UNOP_IND, PREC_PREFIX, 0},
1554 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1555 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1556 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1557 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1559 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1562 /* These variables point to the objects
1563 representing the predefined C data types. */
1565 struct type *builtin_type_void;
1566 struct type *builtin_type_char;
1567 struct type *builtin_type_short;
1568 struct type *builtin_type_int;
1569 struct type *builtin_type_long;
1570 struct type *builtin_type_long_long;
1571 struct type *builtin_type_signed_char;
1572 struct type *builtin_type_unsigned_char;
1573 struct type *builtin_type_unsigned_short;
1574 struct type *builtin_type_unsigned_int;
1575 struct type *builtin_type_unsigned_long;
1576 struct type *builtin_type_unsigned_long_long;
1577 struct type *builtin_type_float;
1578 struct type *builtin_type_double;
1579 struct type *builtin_type_long_double;
1580 struct type *builtin_type_complex;
1581 struct type *builtin_type_double_complex;
1583 struct type ** const (c_builtin_types[]) =
1587 &builtin_type_short,
1589 &builtin_type_float,
1590 &builtin_type_double,
1592 &builtin_type_long_long,
1593 &builtin_type_signed_char,
1594 &builtin_type_unsigned_char,
1595 &builtin_type_unsigned_short,
1596 &builtin_type_unsigned_int,
1597 &builtin_type_unsigned_long,
1598 &builtin_type_unsigned_long_long,
1599 &builtin_type_long_double,
1600 &builtin_type_complex,
1601 &builtin_type_double_complex,
1605 const struct language_defn c_language_defn = {
1606 "c", /* Language name */
1613 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1614 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1615 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1616 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1617 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1618 c_op_print_tab, /* expression operators for printing */
1622 const struct language_defn cplus_language_defn = {
1623 "c++", /* Language name */
1630 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1631 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1632 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1633 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1634 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1635 c_op_print_tab, /* expression operators for printing */
1640 _initialize_c_exp ()
1643 init_type (TYPE_CODE_VOID, 1,
1645 "void", (struct objfile *) NULL);
1647 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1649 "char", (struct objfile *) NULL);
1650 builtin_type_signed_char =
1651 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1653 "signed char", (struct objfile *) NULL);
1654 builtin_type_unsigned_char =
1655 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1657 "unsigned char", (struct objfile *) NULL);
1658 builtin_type_short =
1659 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1661 "short", (struct objfile *) NULL);
1662 builtin_type_unsigned_short =
1663 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1665 "unsigned short", (struct objfile *) NULL);
1667 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1669 "int", (struct objfile *) NULL);
1670 builtin_type_unsigned_int =
1671 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1673 "unsigned int", (struct objfile *) NULL);
1675 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1677 "long", (struct objfile *) NULL);
1678 builtin_type_unsigned_long =
1679 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1681 "unsigned long", (struct objfile *) NULL);
1682 builtin_type_long_long =
1683 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1685 "long long", (struct objfile *) NULL);
1686 builtin_type_unsigned_long_long =
1687 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1689 "unsigned long long", (struct objfile *) NULL);
1690 builtin_type_float =
1691 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1693 "float", (struct objfile *) NULL);
1694 builtin_type_double =
1695 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1697 "double", (struct objfile *) NULL);
1698 builtin_type_long_double =
1699 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1701 "long double", (struct objfile *) NULL);
1702 builtin_type_complex =
1703 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1705 "complex", (struct objfile *) NULL);
1706 builtin_type_double_complex =
1707 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1709 "double complex", (struct objfile *) NULL);
1711 add_language (&c_language_defn);
1712 add_language (&cplus_language_defn);