1 /* YACC parser for C expressions, for GDB.
3 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994, 2002, 2006, 2007, 2008,
4 2009, 2010 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
19 /* Parse a C expression from text in a string, and return the result
20 as a struct expression pointer. That structure contains arithmetic
21 operations in reverse polish, with constants represented by
22 operations that are followed by special data. See expression.h for
23 the details of the format. What is important here is that it can
24 be built up sequentially during the process of parsing; the lower
25 levels of the tree always come first in the result.
27 Note that malloc's and realloc's in this file are transformed to
28 xmalloc and xrealloc respectively by the same sed command in the
29 makefile that remaps any other malloc/realloc inserted by the
30 parser generator. Doing this with #defines and trying to control
31 the interaction with include files (<malloc.h> and <stdlib.h> for
32 example) just became too messy, particularly when such includes can
33 be inserted at random times by the parser generator. */
38 #include "gdb_string.h"
40 #include "expression.h"
42 #include "objc-lang.h" /* For objc language constructs. */
45 #include "parser-defs.h"
48 #include "bfd.h" /* Required by objfiles.h. */
49 #include "symfile.h" /* Required by objfiles.h. */
50 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols. */
52 #include "completer.h" /* For skip_quoted(). */
55 #define parse_type builtin_type (parse_gdbarch)
57 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
58 etc), as well as gratuitiously global symbol names, so we can have
59 multiple yacc generated parsers in gdb. Note that these are only
60 the variables produced by yacc. If other parser generators (bison,
61 byacc, etc) produce additional global names that conflict at link
62 time, then those parser generators need to be fixed instead of
63 adding those names to this list. */
65 #define yymaxdepth objc_maxdepth
66 #define yyparse objc_parse
67 #define yylex objc_lex
68 #define yyerror objc_error
69 #define yylval objc_lval
70 #define yychar objc_char
71 #define yydebug objc_debug
72 #define yypact objc_pact
75 #define yydef objc_def
76 #define yychk objc_chk
77 #define yypgo objc_pgo
78 #define yyact objc_act
79 #define yyexca objc_exca
80 #define yyerrflag objc_errflag
81 #define yynerrs objc_nerrs
85 #define yy_yys objc_yys
86 #define yystate objc_state
87 #define yytmp objc_tmp
89 #define yy_yyv objc_yyv
90 #define yyval objc_val
91 #define yylloc objc_lloc
92 #define yyreds objc_reds /* With YYDEBUG defined */
93 #define yytoks objc_toks /* With YYDEBUG defined */
94 #define yyname objc_name /* With YYDEBUG defined */
95 #define yyrule objc_rule /* With YYDEBUG defined */
96 #define yylhs objc_yylhs
97 #define yylen objc_yylen
98 #define yydefred objc_yydefred
99 #define yydgoto objc_yydgoto
100 #define yysindex objc_yysindex
101 #define yyrindex objc_yyrindex
102 #define yygindex objc_yygindex
103 #define yytable objc_yytable
104 #define yycheck objc_yycheck
107 #define YYDEBUG 0 /* Default to no yydebug support. */
121 /* Although the yacc "value" of an expression is not used,
122 since the result is stored in the structure being created,
123 other node types do have values. */
140 struct symtoken ssym;
143 enum exp_opcode opcode;
144 struct internalvar *ivar;
145 struct objc_class_str class;
152 /* YYSTYPE gets defined by %union. */
154 parse_number (char *, int, int, YYSTYPE *);
157 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
159 %type <tval> type typebase
160 %type <tvec> nonempty_typelist
161 /* %type <bval> block */
163 /* Fancy type parsing. */
164 %type <voidval> func_mod direct_abs_decl abs_decl
166 %type <lval> array_mod
168 %token <typed_val_int> INT
169 %token <typed_val_float> FLOAT
171 /* Both NAME and TYPENAME tokens represent symbols in the input, and
172 both convey their data as strings. But a TYPENAME is a string that
173 happens to be defined as a typedef or builtin type name (such as
174 int or char) and a NAME is any other symbol. Contexts where this
175 distinction is not important can use the nonterminal "name", which
176 matches either NAME or TYPENAME. */
179 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
180 %token <sval> SELECTOR /* ObjC "@selector" pseudo-operator */
181 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
182 %token <tsym> TYPENAME
183 %token <class> CLASSNAME /* ObjC Class name */
185 %type <ssym> name_not_typename
186 %type <tsym> typename
188 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
189 but which would parse as a valid number in the current input radix.
190 E.g. "c" when input_radix==16. Depending on the parse, it will be
191 turned into a name or into a number. */
193 %token <ssym> NAME_OR_INT
195 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
199 /* Special type cases, put in to allow the parser to distinguish
200 different legal basetypes. */
201 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
203 %token <voidval> VARIABLE
205 %token <opcode> ASSIGN_MODIFY
209 %right '=' ASSIGN_MODIFY
217 %left '<' '>' LEQ GEQ
222 %right UNARY INCREMENT DECREMENT
223 %right ARROW '.' '[' '('
224 %token <ssym> BLOCKNAME
236 { write_exp_elt_opcode(OP_TYPE);
237 write_exp_elt_type($1);
238 write_exp_elt_opcode(OP_TYPE);}
241 /* Expressions, including the comma operator. */
244 { write_exp_elt_opcode (BINOP_COMMA); }
247 /* Expressions, not including the comma operator. */
248 exp : '*' exp %prec UNARY
249 { write_exp_elt_opcode (UNOP_IND); }
252 exp : '&' exp %prec UNARY
253 { write_exp_elt_opcode (UNOP_ADDR); }
256 exp : '-' exp %prec UNARY
257 { write_exp_elt_opcode (UNOP_NEG); }
260 exp : '!' exp %prec UNARY
261 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
264 exp : '~' exp %prec UNARY
265 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
268 exp : INCREMENT exp %prec UNARY
269 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
272 exp : DECREMENT exp %prec UNARY
273 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
276 exp : exp INCREMENT %prec UNARY
277 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
280 exp : exp DECREMENT %prec UNARY
281 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
284 exp : SIZEOF exp %prec UNARY
285 { write_exp_elt_opcode (UNOP_SIZEOF); }
289 { write_exp_elt_opcode (STRUCTOP_PTR);
290 write_exp_string ($3);
291 write_exp_elt_opcode (STRUCTOP_PTR); }
294 exp : exp ARROW qualified_name
295 { /* exp->type::name becomes exp->*(&type::name) */
296 /* Note: this doesn't work if name is a
297 static member! FIXME */
298 write_exp_elt_opcode (UNOP_ADDR);
299 write_exp_elt_opcode (STRUCTOP_MPTR); }
301 exp : exp ARROW '*' exp
302 { write_exp_elt_opcode (STRUCTOP_MPTR); }
306 { write_exp_elt_opcode (STRUCTOP_STRUCT);
307 write_exp_string ($3);
308 write_exp_elt_opcode (STRUCTOP_STRUCT); }
312 exp : exp '.' qualified_name
313 { /* exp.type::name becomes exp.*(&type::name) */
314 /* Note: this doesn't work if name is a
315 static member! FIXME */
316 write_exp_elt_opcode (UNOP_ADDR);
317 write_exp_elt_opcode (STRUCTOP_MEMBER); }
320 exp : exp '.' '*' exp
321 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
324 exp : exp '[' exp1 ']'
325 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
328 * The rules below parse ObjC message calls of the form:
329 * '[' target selector {':' argument}* ']'
336 class = lookup_objc_class (parse_gdbarch,
337 copy_name ($2.stoken));
339 error ("%s is not an ObjC Class",
340 copy_name ($2.stoken));
341 write_exp_elt_opcode (OP_LONG);
342 write_exp_elt_type (parse_type->builtin_int);
343 write_exp_elt_longcst ((LONGEST) class);
344 write_exp_elt_opcode (OP_LONG);
348 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
350 write_exp_elt_opcode (OP_OBJC_MSGCALL);
356 write_exp_elt_opcode (OP_LONG);
357 write_exp_elt_type (parse_type->builtin_int);
358 write_exp_elt_longcst ((LONGEST) $2.class);
359 write_exp_elt_opcode (OP_LONG);
363 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
365 write_exp_elt_opcode (OP_OBJC_MSGCALL);
372 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
374 write_exp_elt_opcode (OP_OBJC_MSGCALL);
379 { add_msglist(&$1, 0); }
387 msgarg : name ':' exp
388 { add_msglist(&$1, 1); }
389 | ':' exp /* Unnamed arg. */
390 { add_msglist(0, 1); }
391 | ',' exp /* Variable number of args. */
392 { add_msglist(0, 0); }
396 /* This is to save the value of arglist_len
397 being accumulated by an outer function call. */
398 { start_arglist (); }
399 arglist ')' %prec ARROW
400 { write_exp_elt_opcode (OP_FUNCALL);
401 write_exp_elt_longcst ((LONGEST) end_arglist ());
402 write_exp_elt_opcode (OP_FUNCALL); }
406 { start_arglist (); }
416 arglist : arglist ',' exp %prec ABOVE_COMMA
421 { $$ = end_arglist () - 1; }
423 exp : lcurly arglist rcurly %prec ARROW
424 { write_exp_elt_opcode (OP_ARRAY);
425 write_exp_elt_longcst ((LONGEST) 0);
426 write_exp_elt_longcst ((LONGEST) $3);
427 write_exp_elt_opcode (OP_ARRAY); }
430 exp : lcurly type rcurly exp %prec UNARY
431 { write_exp_elt_opcode (UNOP_MEMVAL);
432 write_exp_elt_type ($2);
433 write_exp_elt_opcode (UNOP_MEMVAL); }
436 exp : '(' type ')' exp %prec UNARY
437 { write_exp_elt_opcode (UNOP_CAST);
438 write_exp_elt_type ($2);
439 write_exp_elt_opcode (UNOP_CAST); }
446 /* Binary operators in order of decreasing precedence. */
449 { write_exp_elt_opcode (BINOP_REPEAT); }
453 { write_exp_elt_opcode (BINOP_MUL); }
457 { write_exp_elt_opcode (BINOP_DIV); }
461 { write_exp_elt_opcode (BINOP_REM); }
465 { write_exp_elt_opcode (BINOP_ADD); }
469 { write_exp_elt_opcode (BINOP_SUB); }
473 { write_exp_elt_opcode (BINOP_LSH); }
477 { write_exp_elt_opcode (BINOP_RSH); }
481 { write_exp_elt_opcode (BINOP_EQUAL); }
484 exp : exp NOTEQUAL exp
485 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
489 { write_exp_elt_opcode (BINOP_LEQ); }
493 { write_exp_elt_opcode (BINOP_GEQ); }
497 { write_exp_elt_opcode (BINOP_LESS); }
501 { write_exp_elt_opcode (BINOP_GTR); }
505 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
509 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
513 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
517 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
521 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
524 exp : exp '?' exp ':' exp %prec '?'
525 { write_exp_elt_opcode (TERNOP_COND); }
529 { write_exp_elt_opcode (BINOP_ASSIGN); }
532 exp : exp ASSIGN_MODIFY exp
533 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
534 write_exp_elt_opcode ($2);
535 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
539 { write_exp_elt_opcode (OP_LONG);
540 write_exp_elt_type ($1.type);
541 write_exp_elt_longcst ((LONGEST)($1.val));
542 write_exp_elt_opcode (OP_LONG); }
547 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
548 write_exp_elt_opcode (OP_LONG);
549 write_exp_elt_type (val.typed_val_int.type);
550 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
551 write_exp_elt_opcode (OP_LONG);
557 { write_exp_elt_opcode (OP_DOUBLE);
558 write_exp_elt_type ($1.type);
559 write_exp_elt_dblcst ($1.dval);
560 write_exp_elt_opcode (OP_DOUBLE); }
567 /* Already written by write_dollar_variable. */
572 write_exp_elt_opcode (OP_OBJC_SELECTOR);
573 write_exp_string ($1);
574 write_exp_elt_opcode (OP_OBJC_SELECTOR); }
577 exp : SIZEOF '(' type ')' %prec UNARY
578 { write_exp_elt_opcode (OP_LONG);
579 write_exp_elt_type (parse_type->builtin_int);
581 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
582 write_exp_elt_opcode (OP_LONG); }
586 { /* C strings are converted into array
587 constants with an explicit null byte
588 added at the end. Thus the array upper
589 bound is the string length. There is no
590 such thing in C as a completely empty
592 char *sp = $1.ptr; int count = $1.length;
595 write_exp_elt_opcode (OP_LONG);
596 write_exp_elt_type (parse_type->builtin_char);
597 write_exp_elt_longcst ((LONGEST)(*sp++));
598 write_exp_elt_opcode (OP_LONG);
600 write_exp_elt_opcode (OP_LONG);
601 write_exp_elt_type (parse_type->builtin_char);
602 write_exp_elt_longcst ((LONGEST)'\0');
603 write_exp_elt_opcode (OP_LONG);
604 write_exp_elt_opcode (OP_ARRAY);
605 write_exp_elt_longcst ((LONGEST) 0);
606 write_exp_elt_longcst ((LONGEST) ($1.length));
607 write_exp_elt_opcode (OP_ARRAY); }
610 exp : NSSTRING /* ObjC NextStep NSString constant
611 * of the form '@' '"' string '"'.
613 { write_exp_elt_opcode (OP_OBJC_NSSTRING);
614 write_exp_string ($1);
615 write_exp_elt_opcode (OP_OBJC_NSSTRING); }
621 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
625 lookup_symtab (copy_name ($1.stoken));
627 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
629 error ("No file or function \"%s\".",
630 copy_name ($1.stoken));
635 block : block COLONCOLON name
637 = lookup_symbol (copy_name ($3), $1,
638 VAR_DOMAIN, (int *) NULL);
639 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
640 error ("No function \"%s\" in specified context.",
642 $$ = SYMBOL_BLOCK_VALUE (tem); }
645 variable: block COLONCOLON name
646 { struct symbol *sym;
647 sym = lookup_symbol (copy_name ($3), $1,
648 VAR_DOMAIN, (int *) NULL);
650 error ("No symbol \"%s\" in specified context.",
653 write_exp_elt_opcode (OP_VAR_VALUE);
654 /* block_found is set by lookup_symbol. */
655 write_exp_elt_block (block_found);
656 write_exp_elt_sym (sym);
657 write_exp_elt_opcode (OP_VAR_VALUE); }
660 qualified_name: typebase COLONCOLON name
662 struct type *type = $1;
663 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
664 && TYPE_CODE (type) != TYPE_CODE_UNION)
665 error ("`%s' is not defined as an aggregate type.",
668 write_exp_elt_opcode (OP_SCOPE);
669 write_exp_elt_type (type);
670 write_exp_string ($3);
671 write_exp_elt_opcode (OP_SCOPE);
673 | typebase COLONCOLON '~' name
675 struct type *type = $1;
676 struct stoken tmp_token;
677 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
678 && TYPE_CODE (type) != TYPE_CODE_UNION)
679 error ("`%s' is not defined as an aggregate type.",
682 if (strcmp (type_name_no_tag (type), $4.ptr) != 0)
683 error ("invalid destructor `%s::~%s'",
684 type_name_no_tag (type), $4.ptr);
686 tmp_token.ptr = (char*) alloca ($4.length + 2);
687 tmp_token.length = $4.length + 1;
688 tmp_token.ptr[0] = '~';
689 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
690 tmp_token.ptr[tmp_token.length] = 0;
691 write_exp_elt_opcode (OP_SCOPE);
692 write_exp_elt_type (type);
693 write_exp_string (tmp_token);
694 write_exp_elt_opcode (OP_SCOPE);
698 variable: qualified_name
701 char *name = copy_name ($2);
703 struct minimal_symbol *msymbol;
706 lookup_symbol (name, (const struct block *) NULL,
707 VAR_DOMAIN, (int *) NULL);
710 write_exp_elt_opcode (OP_VAR_VALUE);
711 write_exp_elt_block (NULL);
712 write_exp_elt_sym (sym);
713 write_exp_elt_opcode (OP_VAR_VALUE);
717 msymbol = lookup_minimal_symbol (name, NULL, NULL);
719 write_exp_msymbol (msymbol);
720 else if (!have_full_symbols () && !have_partial_symbols ())
721 error ("No symbol table is loaded. Use the \"file\" command.");
723 error ("No symbol \"%s\" in current context.", name);
727 variable: name_not_typename
728 { struct symbol *sym = $1.sym;
732 if (symbol_read_needs_frame (sym))
734 if (innermost_block == 0 ||
735 contained_in (block_found,
737 innermost_block = block_found;
740 write_exp_elt_opcode (OP_VAR_VALUE);
741 /* We want to use the selected frame, not
742 another more inner frame which happens to
743 be in the same block. */
744 write_exp_elt_block (NULL);
745 write_exp_elt_sym (sym);
746 write_exp_elt_opcode (OP_VAR_VALUE);
748 else if ($1.is_a_field_of_this)
750 /* C++/ObjC: it hangs off of `this'/'self'.
751 Must not inadvertently convert from a
752 method call to data ref. */
753 if (innermost_block == 0 ||
754 contained_in (block_found, innermost_block))
755 innermost_block = block_found;
756 write_exp_elt_opcode (OP_OBJC_SELF);
757 write_exp_elt_opcode (OP_OBJC_SELF);
758 write_exp_elt_opcode (STRUCTOP_PTR);
759 write_exp_string ($1.stoken);
760 write_exp_elt_opcode (STRUCTOP_PTR);
764 struct minimal_symbol *msymbol;
765 char *arg = copy_name ($1.stoken);
768 lookup_minimal_symbol (arg, NULL, NULL);
770 write_exp_msymbol (msymbol);
771 else if (!have_full_symbols () &&
772 !have_partial_symbols ())
773 error ("No symbol table is loaded. Use the \"file\" command.");
775 error ("No symbol \"%s\" in current context.",
776 copy_name ($1.stoken));
783 /* "const" and "volatile" are curently ignored. A type
784 qualifier before the type is currently handled in the
785 typebase rule. The reason for recognizing these here
786 (shift/reduce conflicts) might be obsolete now that some
787 pointer to member rules have been deleted. */
788 | typebase CONST_KEYWORD
789 | typebase VOLATILE_KEYWORD
791 { $$ = follow_types ($1); }
792 | typebase CONST_KEYWORD abs_decl
793 { $$ = follow_types ($1); }
794 | typebase VOLATILE_KEYWORD abs_decl
795 { $$ = follow_types ($1); }
799 { push_type (tp_pointer); $$ = 0; }
801 { push_type (tp_pointer); $$ = $2; }
803 { push_type (tp_reference); $$ = 0; }
805 { push_type (tp_reference); $$ = $2; }
809 direct_abs_decl: '(' abs_decl ')'
811 | direct_abs_decl array_mod
814 push_type (tp_array);
819 push_type (tp_array);
823 | direct_abs_decl func_mod
824 { push_type (tp_function); }
826 { push_type (tp_function); }
837 | '(' nonempty_typelist ')'
838 { free ($2); $$ = 0; }
841 /* We used to try to recognize more pointer to member types here, but
842 that didn't work (shift/reduce conflicts meant that these rules
843 never got executed). The problem is that
844 int (foo::bar::baz::bizzle)
845 is a function type but
846 int (foo::bar::baz::bizzle::*)
847 is a pointer to member type. Stroustrup loses again! */
852 typebase /* Implements (approximately): (type-qualifier)* type-specifier. */
858 error ("No symbol \"%s\" in current context.",
859 copy_name($1.stoken));
864 { $$ = parse_type->builtin_int; }
866 { $$ = parse_type->builtin_long; }
868 { $$ = parse_type->builtin_short; }
870 { $$ = parse_type->builtin_long; }
871 | UNSIGNED LONG INT_KEYWORD
872 { $$ = parse_type->builtin_unsigned_long; }
874 { $$ = parse_type->builtin_long_long; }
875 | LONG LONG INT_KEYWORD
876 { $$ = parse_type->builtin_long_long; }
878 { $$ = parse_type->builtin_unsigned_long_long; }
879 | UNSIGNED LONG LONG INT_KEYWORD
880 { $$ = parse_type->builtin_unsigned_long_long; }
882 { $$ = parse_type->builtin_short; }
883 | UNSIGNED SHORT INT_KEYWORD
884 { $$ = parse_type->builtin_unsigned_short; }
886 { $$ = parse_type->builtin_double; }
887 | LONG DOUBLE_KEYWORD
888 { $$ = parse_type->builtin_long_double; }
890 { $$ = lookup_struct (copy_name ($2),
891 expression_context_block); }
893 { $$ = lookup_struct (copy_name ($2),
894 expression_context_block); }
896 { $$ = lookup_union (copy_name ($2),
897 expression_context_block); }
899 { $$ = lookup_enum (copy_name ($2),
900 expression_context_block); }
902 { $$ = lookup_unsigned_typename (parse_language,
904 TYPE_NAME($2.type)); }
906 { $$ = parse_type->builtin_unsigned_int; }
907 | SIGNED_KEYWORD typename
908 { $$ = lookup_signed_typename (parse_language,
910 TYPE_NAME($2.type)); }
912 { $$ = parse_type->builtin_int; }
913 | TEMPLATE name '<' type '>'
914 { $$ = lookup_template_type(copy_name($2), $4,
915 expression_context_block);
917 /* "const" and "volatile" are curently ignored. A type
918 qualifier after the type is handled in the ptype rule. I
919 think these could be too. */
920 | CONST_KEYWORD typebase { $$ = $2; }
921 | VOLATILE_KEYWORD typebase { $$ = $2; }
927 $$.stoken.ptr = "int";
928 $$.stoken.length = 3;
929 $$.type = parse_type->builtin_int;
933 $$.stoken.ptr = "long";
934 $$.stoken.length = 4;
935 $$.type = parse_type->builtin_long;
939 $$.stoken.ptr = "short";
940 $$.stoken.length = 5;
941 $$.type = parse_type->builtin_short;
947 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
948 $<ivec>$[0] = 1; /* Number of types in vector. */
951 | nonempty_typelist ',' type
952 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
953 $$ = (struct type **) realloc ((char *) $1, len);
954 $$[$<ivec>$[0]] = $3;
958 name : NAME { $$ = $1.stoken; }
959 | BLOCKNAME { $$ = $1.stoken; }
960 | TYPENAME { $$ = $1.stoken; }
961 | CLASSNAME { $$ = $1.stoken; }
962 | NAME_OR_INT { $$ = $1.stoken; }
965 name_not_typename : NAME
967 /* These would be useful if name_not_typename was useful, but it is
968 just a fake for "variable", so these cause reduce/reduce conflicts
969 because the parser can't tell whether NAME_OR_INT is a
970 name_not_typename (=variable, =exp) or just an exp. If
971 name_not_typename was ever used in an lvalue context where only a
972 name could occur, this might be useful. */
978 /* Take care of parsing a number (anything that starts with a digit).
979 Set yylval and return the token type; update lexptr. LEN is the
980 number of characters in it. */
982 /*** Needs some error checking for the float case. ***/
985 parse_number (p, len, parsed_float, putithere)
991 /* FIXME: Shouldn't these be unsigned? We don't deal with negative
992 values here, and we do kind of silly things like cast to
1000 int base = input_radix;
1003 /* Number of "L" suffixes encountered. */
1006 /* We have found a "L" or "U" suffix. */
1007 int found_suffix = 0;
1009 unsigned LONGEST high_bit;
1010 struct type *signed_type;
1011 struct type *unsigned_type;
1017 /* It's a float since it contains a point or an exponent. */
1019 sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%c",
1020 &putithere->typed_val_float.dval, &c);
1022 /* See if it has `f' or `l' suffix (float or long double). */
1024 c = tolower (p[len - 1]);
1027 putithere->typed_val_float.type = parse_type->builtin_float;
1029 putithere->typed_val_float.type = parse_type->builtin_long_double;
1030 else if (isdigit (c) || c == '.')
1031 putithere->typed_val_float.type = parse_type->builtin_double;
1038 /* Handle base-switching prefixes 0x, 0t, 0d, and 0. */
1072 if (c >= 'A' && c <= 'Z')
1074 if (c != 'l' && c != 'u')
1076 if (c >= '0' && c <= '9')
1084 if (base > 10 && c >= 'a' && c <= 'f')
1088 n += i = c - 'a' + 10;
1101 return ERROR; /* Char not a digit. */
1104 return ERROR; /* Invalid digit in this base. */
1106 /* Portably test for overflow (only works for nonzero values, so
1107 make a second check for zero). FIXME: Can't we just make n
1108 and prevn unsigned and avoid this? */
1109 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1110 unsigned_p = 1; /* Try something unsigned. */
1112 /* Portably test for unsigned overflow.
1113 FIXME: This check is wrong; for example it doesn't find
1114 overflow on 0x123456789 when LONGEST is 32 bits. */
1115 if (c != 'l' && c != 'u' && n != 0)
1117 if ((unsigned_p && (unsigned LONGEST) prevn >= (unsigned LONGEST) n))
1118 error ("Numeric constant too large.");
1123 /* An integer constant is an int, a long, or a long long. An L
1124 suffix forces it to be long; an LL suffix forces it to be long
1125 long. If not forced to a larger size, it gets the first type of
1126 the above that it fits in. To figure out whether it fits, we
1127 shift it right and see whether anything remains. Note that we
1128 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1129 operation, because many compilers will warn about such a shift
1130 (which always produces a zero result). Sometimes gdbarch_int_bit
1131 or gdbarch_long_int will be that big, sometimes not. To deal with
1132 the case where it is we just always shift the value more than
1133 once, with fewer bits each time. */
1135 un = (unsigned LONGEST)n >> 2;
1137 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1139 high_bit = ((unsigned LONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1141 /* A large decimal (not hex or octal) constant (between INT_MAX
1142 and UINT_MAX) is a long or unsigned long, according to ANSI,
1143 never an unsigned int, but this code treats it as unsigned
1144 int. This probably should be fixed. GCC gives a warning on
1147 unsigned_type = parse_type->builtin_unsigned_int;
1148 signed_type = parse_type->builtin_int;
1150 else if (long_p <= 1
1151 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1153 high_bit = ((unsigned LONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1154 unsigned_type = parse_type->builtin_unsigned_long;
1155 signed_type = parse_type->builtin_long;
1159 high_bit = (((unsigned LONGEST)1)
1160 << (gdbarch_long_long_bit (parse_gdbarch) - 32 - 1)
1164 /* A long long does not fit in a LONGEST. */
1166 (unsigned LONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
1167 unsigned_type = parse_type->builtin_unsigned_long_long;
1168 signed_type = parse_type->builtin_long_long;
1171 putithere->typed_val_int.val = n;
1173 /* If the high bit of the worked out type is set then this number
1174 has to be unsigned. */
1176 if (unsigned_p || (n & high_bit))
1178 putithere->typed_val_int.type = unsigned_type;
1182 putithere->typed_val_int.type = signed_type;
1192 enum exp_opcode opcode;
1195 static const struct token tokentab3[] =
1197 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1198 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1201 static const struct token tokentab2[] =
1203 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1204 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1205 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1206 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1207 {"%=", ASSIGN_MODIFY, BINOP_REM},
1208 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1209 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1210 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1211 {"++", INCREMENT, BINOP_END},
1212 {"--", DECREMENT, BINOP_END},
1213 {"->", ARROW, BINOP_END},
1214 {"&&", ANDAND, BINOP_END},
1215 {"||", OROR, BINOP_END},
1216 {"::", COLONCOLON, BINOP_END},
1217 {"<<", LSH, BINOP_END},
1218 {">>", RSH, BINOP_END},
1219 {"==", EQUAL, BINOP_END},
1220 {"!=", NOTEQUAL, BINOP_END},
1221 {"<=", LEQ, BINOP_END},
1222 {">=", GEQ, BINOP_END}
1225 /* Read one token, getting characters through lexptr. */
1236 static char *tempbuf;
1237 static int tempbufsize;
1242 /* See if it is a special token of length 3. */
1243 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1244 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
1247 yylval.opcode = tokentab3[i].opcode;
1248 return tokentab3[i].token;
1251 /* See if it is a special token of length 2. */
1252 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1253 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
1256 yylval.opcode = tokentab2[i].opcode;
1257 return tokentab2[i].token;
1261 switch (tokchr = *tokstart)
1273 /* We either have a character constant ('0' or '\177' for
1274 example) or we have a quoted symbol reference ('foo(int,int)'
1275 in C++ for example). */
1279 c = parse_escape (&lexptr);
1281 error ("Empty character constant.");
1283 yylval.typed_val_int.val = c;
1284 yylval.typed_val_int.type = parse_type->builtin_char;
1289 namelen = skip_quoted (tokstart) - tokstart;
1292 lexptr = tokstart + namelen;
1293 if (lexptr[-1] != '\'')
1294 error ("Unmatched single quote.");
1299 error ("Invalid character constant.");
1309 if (paren_depth == 0)
1316 if (comma_terminates && paren_depth == 0)
1322 /* Might be a floating point number. */
1323 if (lexptr[1] < '0' || lexptr[1] > '9')
1324 goto symbol; /* Nope, must be a symbol. */
1325 /* FALL THRU into number case. */
1338 /* It's a number. */
1339 int got_dot = 0, got_e = 0, toktype = FLOAT;
1340 /* Initialize toktype to anything other than ERROR. */
1342 int hex = input_radix > 10;
1343 int local_radix = input_radix;
1344 if (tokchr == '0' && (p[1] == 'x' || p[1] == 'X'))
1350 else if (tokchr == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1359 /* This test includes !hex because 'e' is a valid hex digit
1360 and thus does not indicate a floating point number when
1361 the radix is hex. */
1363 if (!hex && (*p == 'e' || *p == 'E'))
1365 toktype = ERROR; /* Only one 'e' in a float. */
1368 /* This test does not include !hex, because a '.' always
1369 indicates a decimal floating point number regardless of
1373 toktype = ERROR; /* Only one '.' in a float. */
1376 else if (got_e && (p[-1] == 'e' || p[-1] == 'E') &&
1377 (*p == '-' || *p == '+'))
1378 /* This is the sign of the exponent, not the end of the
1381 /* Always take decimal digits; parse_number handles radix
1383 else if (*p >= '0' && *p <= '9')
1385 /* We will take letters only if hex is true, and only up
1386 to what the input radix would permit. FSF was content
1387 to rely on parse_number to validate; but it leaks. */
1388 else if (*p >= 'a' && *p <= 'z')
1390 if (!hex || *p >= ('a' + local_radix - 10))
1393 else if (*p >= 'A' && *p <= 'Z')
1395 if (!hex || *p >= ('A' + local_radix - 10))
1400 if (toktype != ERROR)
1401 toktype = parse_number (tokstart, p - tokstart,
1402 got_dot | got_e, &yylval);
1403 if (toktype == ERROR)
1405 char *err_copy = (char *) alloca (p - tokstart + 1);
1407 memcpy (err_copy, tokstart, p - tokstart);
1408 err_copy[p - tokstart] = 0;
1409 error ("Invalid number \"%s\".", err_copy);
1426 case '@': /* Moved out below. */
1442 if (strncmp(tokstart, "@selector", 9) == 0)
1444 tokptr = strchr(tokstart, '(');
1447 error ("Missing '(' in @selector(...)");
1450 tokptr++; /* Skip the '('. */
1452 /* Grow the static temp buffer if necessary, including
1453 allocating the first one on demand. */
1454 if (tempbufindex + 1 >= tempbufsize)
1456 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1458 tempbuf[tempbufindex++] = *tokptr++;
1459 } while ((*tokptr != ')') && (*tokptr != '\0'));
1460 if (*tokptr++ != ')')
1462 error ("Missing ')' in @selector(...)");
1464 tempbuf[tempbufindex] = '\0';
1465 yylval.sval.ptr = tempbuf;
1466 yylval.sval.length = tempbufindex;
1470 if (tokstart[1] != '"')
1475 /* ObjC NextStep NSString constant: fall thru and parse like
1481 /* Build the gdb internal form of the input string in tempbuf,
1482 translating any standard C escape forms seen. Note that the
1483 buffer is null byte terminated *only* for the convenience of
1484 debugging gdb itself and printing the buffer contents when
1485 the buffer contains no embedded nulls. Gdb does not depend
1486 upon the buffer being null byte terminated, it uses the
1487 length string instead. This allows gdb to handle C strings
1488 (as well as strings in other languages) with embedded null
1491 tokptr = ++tokstart;
1495 /* Grow the static temp buffer if necessary, including
1496 allocating the first one on demand. */
1497 if (tempbufindex + 1 >= tempbufsize)
1499 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1505 /* Do nothing, loop will terminate. */
1509 c = parse_escape (&tokptr);
1514 tempbuf[tempbufindex++] = c;
1517 tempbuf[tempbufindex++] = *tokptr++;
1520 } while ((*tokptr != '"') && (*tokptr != '\0'));
1521 if (*tokptr++ != '"')
1523 error ("Unterminated string in expression.");
1525 tempbuf[tempbufindex] = '\0'; /* See note above. */
1526 yylval.sval.ptr = tempbuf;
1527 yylval.sval.length = tempbufindex;
1529 return (tokchr == '@' ? NSSTRING : STRING);
1532 if (!(tokchr == '_' || tokchr == '$' ||
1533 (tokchr >= 'a' && tokchr <= 'z') || (tokchr >= 'A' && tokchr <= 'Z')))
1534 /* We must have come across a bad character (e.g. ';'). */
1535 error ("Invalid character '%c' in expression.", c);
1537 /* It's a name. See how long it is. */
1539 for (c = tokstart[namelen];
1540 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1541 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1546 while (tokstart[++i] && tokstart[i] != '>');
1547 if (tokstart[i] == '>')
1550 c = tokstart[++namelen];
1553 /* The token "if" terminates the expression and is NOT
1554 removed from the input stream. */
1555 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1564 /* Catch specific keywords. Should be done with a data structure. */
1568 if (strncmp (tokstart, "unsigned", 8) == 0)
1570 if (parse_language->la_language == language_cplus
1571 && strncmp (tokstart, "template", 8) == 0)
1573 if (strncmp (tokstart, "volatile", 8) == 0)
1574 return VOLATILE_KEYWORD;
1577 if (strncmp (tokstart, "struct", 6) == 0)
1579 if (strncmp (tokstart, "signed", 6) == 0)
1580 return SIGNED_KEYWORD;
1581 if (strncmp (tokstart, "sizeof", 6) == 0)
1583 if (strncmp (tokstart, "double", 6) == 0)
1584 return DOUBLE_KEYWORD;
1587 if ((parse_language->la_language == language_cplus)
1588 && strncmp (tokstart, "class", 5) == 0)
1590 if (strncmp (tokstart, "union", 5) == 0)
1592 if (strncmp (tokstart, "short", 5) == 0)
1594 if (strncmp (tokstart, "const", 5) == 0)
1595 return CONST_KEYWORD;
1598 if (strncmp (tokstart, "enum", 4) == 0)
1600 if (strncmp (tokstart, "long", 4) == 0)
1604 if (strncmp (tokstart, "int", 3) == 0)
1611 yylval.sval.ptr = tokstart;
1612 yylval.sval.length = namelen;
1614 if (*tokstart == '$')
1616 write_dollar_variable (yylval.sval);
1620 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1621 functions or symtabs. If this is not so, then ...
1622 Use token-type TYPENAME for symbols that happen to be defined
1623 currently as names of types; NAME for other symbols.
1624 The caller is not constrained to care about the distinction. */
1626 char *tmp = copy_name (yylval.sval);
1628 int is_a_field_of_this = 0, *need_this;
1631 if (parse_language->la_language == language_cplus ||
1632 parse_language->la_language == language_objc)
1633 need_this = &is_a_field_of_this;
1635 need_this = (int *) NULL;
1637 sym = lookup_symbol (tmp, expression_context_block,
1640 /* Call lookup_symtab, not lookup_partial_symtab, in case there
1641 are no psymtabs (coff, xcoff, or some future change to blow
1642 away the psymtabs once symbols are read). */
1643 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1644 lookup_symtab (tmp))
1646 yylval.ssym.sym = sym;
1647 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1650 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1653 /* Despite the following flaw, we need to keep this code
1654 enabled. Because we can get called from
1655 check_stub_method, if we don't handle nested types then
1656 it screws many operations in any program which uses
1658 /* In "A::x", if x is a member function of A and there
1659 happens to be a type (nested or not, since the stabs
1660 don't make that distinction) named x, then this code
1661 incorrectly thinks we are dealing with nested types
1662 rather than a member function. */
1666 struct symbol *best_sym;
1668 /* Look ahead to detect nested types. This probably should
1669 be done in the grammar, but trying seemed to introduce a
1670 lot of shift/reduce and reduce/reduce conflicts. It's
1671 possible that it could be done, though. Or perhaps a
1672 non-grammar, but less ad hoc, approach would work well. */
1674 /* Since we do not currently have any way of distinguishing
1675 a nested type from a non-nested one (the stabs don't tell
1676 us whether a type is nested), we just ignore the
1683 /* Skip whitespace. */
1684 while (*p == ' ' || *p == '\t' || *p == '\n')
1686 if (*p == ':' && p[1] == ':')
1688 /* Skip the `::'. */
1690 /* Skip whitespace. */
1691 while (*p == ' ' || *p == '\t' || *p == '\n')
1694 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1695 || (*p >= 'a' && *p <= 'z')
1696 || (*p >= 'A' && *p <= 'Z'))
1700 struct symbol *cur_sym;
1701 /* As big as the whole rest of the expression,
1702 which is at least big enough. */
1703 char *ncopy = alloca (strlen (tmp) +
1704 strlen (namestart) + 3);
1708 memcpy (tmp1, tmp, strlen (tmp));
1709 tmp1 += strlen (tmp);
1710 memcpy (tmp1, "::", 2);
1712 memcpy (tmp1, namestart, p - namestart);
1713 tmp1[p - namestart] = '\0';
1714 cur_sym = lookup_symbol (ncopy,
1715 expression_context_block,
1716 VAR_DOMAIN, (int *) NULL);
1719 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1737 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1739 yylval.tsym.type = SYMBOL_TYPE (sym);
1744 = language_lookup_primitive_type_by_name (parse_language,
1745 parse_gdbarch, tmp);
1746 if (yylval.tsym.type != NULL)
1749 /* See if it's an ObjC classname. */
1752 CORE_ADDR Class = lookup_objc_class (parse_gdbarch, tmp);
1755 yylval.class.class = Class;
1756 if ((sym = lookup_struct_typedef (tmp,
1757 expression_context_block,
1759 yylval.class.type = SYMBOL_TYPE (sym);
1764 /* Input names that aren't symbols but ARE valid hex numbers,
1765 when the input radix permits them, can be names or numbers
1766 depending on the parse. Note we support radixes > 16 here. */
1768 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1769 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1771 YYSTYPE newlval; /* Its value is ignored. */
1772 hextype = parse_number (tokstart, namelen, 0, &newlval);
1775 yylval.ssym.sym = sym;
1776 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1781 /* Any other kind of symbol. */
1782 yylval.ssym.sym = sym;
1783 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1792 if (*lexptr == '\0')
1793 error("A %s near end of expression.", (msg ? msg : "error"));
1795 error ("A %s in expression, near `%s'.", (msg ? msg : "error"),