1 /* YACC parser for C expressions, for GDB.
3 Copyright (C) 1986, 1989-1991, 1993-1994, 2002, 2006-2012 Free
4 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. */
112 static int yylex (void);
114 void yyerror (char *);
118 /* Although the yacc "value" of an expression is not used,
119 since the result is stored in the structure being created,
120 other node types do have values. */
137 struct symtoken ssym;
140 enum exp_opcode opcode;
141 struct internalvar *ivar;
142 struct objc_class_str class;
149 /* YYSTYPE gets defined by %union. */
150 static int parse_number (char *, int, int, YYSTYPE *);
153 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
155 %type <tval> type typebase
156 %type <tvec> nonempty_typelist
157 /* %type <bval> block */
159 /* Fancy type parsing. */
160 %type <voidval> func_mod direct_abs_decl abs_decl
162 %type <lval> array_mod
164 %token <typed_val_int> INT
165 %token <typed_val_float> FLOAT
167 /* Both NAME and TYPENAME tokens represent symbols in the input, and
168 both convey their data as strings. But a TYPENAME is a string that
169 happens to be defined as a typedef or builtin type name (such as
170 int or char) and a NAME is any other symbol. Contexts where this
171 distinction is not important can use the nonterminal "name", which
172 matches either NAME or TYPENAME. */
175 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
176 %token <sval> SELECTOR /* ObjC "@selector" pseudo-operator */
177 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
178 %token <tsym> TYPENAME
179 %token <class> CLASSNAME /* ObjC Class name */
181 %type <ssym> name_not_typename
182 %type <tsym> typename
184 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
185 but which would parse as a valid number in the current input radix.
186 E.g. "c" when input_radix==16. Depending on the parse, it will be
187 turned into a name or into a number. */
189 %token <ssym> NAME_OR_INT
191 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
195 /* Special type cases, put in to allow the parser to distinguish
196 different legal basetypes. */
197 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
199 %token <voidval> VARIABLE
201 %token <opcode> ASSIGN_MODIFY
205 %right '=' ASSIGN_MODIFY
213 %left '<' '>' LEQ GEQ
218 %right UNARY INCREMENT DECREMENT
219 %right ARROW '.' '[' '('
220 %token <ssym> BLOCKNAME
232 { write_exp_elt_opcode(OP_TYPE);
233 write_exp_elt_type($1);
234 write_exp_elt_opcode(OP_TYPE);}
237 /* Expressions, including the comma operator. */
240 { write_exp_elt_opcode (BINOP_COMMA); }
243 /* Expressions, not including the comma operator. */
244 exp : '*' exp %prec UNARY
245 { write_exp_elt_opcode (UNOP_IND); }
248 exp : '&' exp %prec UNARY
249 { write_exp_elt_opcode (UNOP_ADDR); }
252 exp : '-' exp %prec UNARY
253 { write_exp_elt_opcode (UNOP_NEG); }
256 exp : '!' exp %prec UNARY
257 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
260 exp : '~' exp %prec UNARY
261 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
264 exp : INCREMENT exp %prec UNARY
265 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
268 exp : DECREMENT exp %prec UNARY
269 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
272 exp : exp INCREMENT %prec UNARY
273 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
276 exp : exp DECREMENT %prec UNARY
277 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
280 exp : SIZEOF exp %prec UNARY
281 { write_exp_elt_opcode (UNOP_SIZEOF); }
285 { write_exp_elt_opcode (STRUCTOP_PTR);
286 write_exp_string ($3);
287 write_exp_elt_opcode (STRUCTOP_PTR); }
290 exp : exp ARROW qualified_name
291 { /* exp->type::name becomes exp->*(&type::name) */
292 /* Note: this doesn't work if name is a
293 static member! FIXME */
294 write_exp_elt_opcode (UNOP_ADDR);
295 write_exp_elt_opcode (STRUCTOP_MPTR); }
297 exp : exp ARROW '*' exp
298 { write_exp_elt_opcode (STRUCTOP_MPTR); }
302 { write_exp_elt_opcode (STRUCTOP_STRUCT);
303 write_exp_string ($3);
304 write_exp_elt_opcode (STRUCTOP_STRUCT); }
308 exp : exp '.' qualified_name
309 { /* exp.type::name becomes exp.*(&type::name) */
310 /* Note: this doesn't work if name is a
311 static member! FIXME */
312 write_exp_elt_opcode (UNOP_ADDR);
313 write_exp_elt_opcode (STRUCTOP_MEMBER); }
316 exp : exp '.' '*' exp
317 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
320 exp : exp '[' exp1 ']'
321 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
324 * The rules below parse ObjC message calls of the form:
325 * '[' target selector {':' argument}* ']'
332 class = lookup_objc_class (parse_gdbarch,
333 copy_name ($2.stoken));
335 error (_("%s is not an ObjC Class"),
336 copy_name ($2.stoken));
337 write_exp_elt_opcode (OP_LONG);
338 write_exp_elt_type (parse_type->builtin_int);
339 write_exp_elt_longcst ((LONGEST) class);
340 write_exp_elt_opcode (OP_LONG);
344 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
346 write_exp_elt_opcode (OP_OBJC_MSGCALL);
352 write_exp_elt_opcode (OP_LONG);
353 write_exp_elt_type (parse_type->builtin_int);
354 write_exp_elt_longcst ((LONGEST) $2.class);
355 write_exp_elt_opcode (OP_LONG);
359 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
361 write_exp_elt_opcode (OP_OBJC_MSGCALL);
368 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
370 write_exp_elt_opcode (OP_OBJC_MSGCALL);
375 { add_msglist(&$1, 0); }
383 msgarg : name ':' exp
384 { add_msglist(&$1, 1); }
385 | ':' exp /* Unnamed arg. */
386 { add_msglist(0, 1); }
387 | ',' exp /* Variable number of args. */
388 { add_msglist(0, 0); }
392 /* This is to save the value of arglist_len
393 being accumulated by an outer function call. */
394 { start_arglist (); }
395 arglist ')' %prec ARROW
396 { write_exp_elt_opcode (OP_FUNCALL);
397 write_exp_elt_longcst ((LONGEST) end_arglist ());
398 write_exp_elt_opcode (OP_FUNCALL); }
402 { start_arglist (); }
412 arglist : arglist ',' exp %prec ABOVE_COMMA
417 { $$ = end_arglist () - 1; }
419 exp : lcurly arglist rcurly %prec ARROW
420 { write_exp_elt_opcode (OP_ARRAY);
421 write_exp_elt_longcst ((LONGEST) 0);
422 write_exp_elt_longcst ((LONGEST) $3);
423 write_exp_elt_opcode (OP_ARRAY); }
426 exp : lcurly type rcurly exp %prec UNARY
427 { write_exp_elt_opcode (UNOP_MEMVAL);
428 write_exp_elt_type ($2);
429 write_exp_elt_opcode (UNOP_MEMVAL); }
432 exp : '(' type ')' exp %prec UNARY
433 { write_exp_elt_opcode (UNOP_CAST);
434 write_exp_elt_type ($2);
435 write_exp_elt_opcode (UNOP_CAST); }
442 /* Binary operators in order of decreasing precedence. */
445 { write_exp_elt_opcode (BINOP_REPEAT); }
449 { write_exp_elt_opcode (BINOP_MUL); }
453 { write_exp_elt_opcode (BINOP_DIV); }
457 { write_exp_elt_opcode (BINOP_REM); }
461 { write_exp_elt_opcode (BINOP_ADD); }
465 { write_exp_elt_opcode (BINOP_SUB); }
469 { write_exp_elt_opcode (BINOP_LSH); }
473 { write_exp_elt_opcode (BINOP_RSH); }
477 { write_exp_elt_opcode (BINOP_EQUAL); }
480 exp : exp NOTEQUAL exp
481 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
485 { write_exp_elt_opcode (BINOP_LEQ); }
489 { write_exp_elt_opcode (BINOP_GEQ); }
493 { write_exp_elt_opcode (BINOP_LESS); }
497 { write_exp_elt_opcode (BINOP_GTR); }
501 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
505 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
509 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
513 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
517 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
520 exp : exp '?' exp ':' exp %prec '?'
521 { write_exp_elt_opcode (TERNOP_COND); }
525 { write_exp_elt_opcode (BINOP_ASSIGN); }
528 exp : exp ASSIGN_MODIFY exp
529 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
530 write_exp_elt_opcode ($2);
531 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
535 { write_exp_elt_opcode (OP_LONG);
536 write_exp_elt_type ($1.type);
537 write_exp_elt_longcst ((LONGEST)($1.val));
538 write_exp_elt_opcode (OP_LONG); }
543 parse_number ($1.stoken.ptr,
544 $1.stoken.length, 0, &val);
545 write_exp_elt_opcode (OP_LONG);
546 write_exp_elt_type (val.typed_val_int.type);
547 write_exp_elt_longcst ((LONGEST)
548 val.typed_val_int.val);
549 write_exp_elt_opcode (OP_LONG);
555 { write_exp_elt_opcode (OP_DOUBLE);
556 write_exp_elt_type ($1.type);
557 write_exp_elt_dblcst ($1.dval);
558 write_exp_elt_opcode (OP_DOUBLE); }
565 /* Already written by write_dollar_variable. */
570 write_exp_elt_opcode (OP_OBJC_SELECTOR);
571 write_exp_string ($1);
572 write_exp_elt_opcode (OP_OBJC_SELECTOR); }
575 exp : SIZEOF '(' type ')' %prec UNARY
576 { write_exp_elt_opcode (OP_LONG);
577 write_exp_elt_type (parse_type->builtin_int);
579 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
580 write_exp_elt_opcode (OP_LONG); }
584 { /* C strings are converted into array
585 constants with an explicit null byte
586 added at the end. Thus the array upper
587 bound is the string length. There is no
588 such thing in C as a completely empty
590 char *sp = $1.ptr; int count = $1.length;
593 write_exp_elt_opcode (OP_LONG);
594 write_exp_elt_type (parse_type->builtin_char);
595 write_exp_elt_longcst ((LONGEST)(*sp++));
596 write_exp_elt_opcode (OP_LONG);
598 write_exp_elt_opcode (OP_LONG);
599 write_exp_elt_type (parse_type->builtin_char);
600 write_exp_elt_longcst ((LONGEST)'\0');
601 write_exp_elt_opcode (OP_LONG);
602 write_exp_elt_opcode (OP_ARRAY);
603 write_exp_elt_longcst ((LONGEST) 0);
604 write_exp_elt_longcst ((LONGEST) ($1.length));
605 write_exp_elt_opcode (OP_ARRAY); }
608 exp : NSSTRING /* ObjC NextStep NSString constant
609 * of the form '@' '"' string '"'.
611 { write_exp_elt_opcode (OP_OBJC_NSSTRING);
612 write_exp_string ($1);
613 write_exp_elt_opcode (OP_OBJC_NSSTRING); }
619 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
623 lookup_symtab (copy_name ($1.stoken));
625 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem),
628 error (_("No file or function \"%s\"."),
629 copy_name ($1.stoken));
634 block : block COLONCOLON name
636 = lookup_symbol (copy_name ($3), $1,
637 VAR_DOMAIN, (int *) NULL);
638 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
639 error (_("No function \"%s\" in specified context."),
641 $$ = SYMBOL_BLOCK_VALUE (tem); }
644 variable: block COLONCOLON name
645 { struct symbol *sym;
646 sym = lookup_symbol (copy_name ($3), $1,
647 VAR_DOMAIN, (int *) NULL);
649 error (_("No symbol \"%s\" in specified context."),
651 if (symbol_read_needs_frame (sym))
653 if (innermost_block == 0
654 || contained_in (block_found,
656 innermost_block = block_found;
659 write_exp_elt_opcode (OP_VAR_VALUE);
660 /* block_found is set by lookup_symbol. */
661 write_exp_elt_block (block_found);
662 write_exp_elt_sym (sym);
663 write_exp_elt_opcode (OP_VAR_VALUE); }
666 qualified_name: typebase COLONCOLON name
668 struct type *type = $1;
669 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
670 && TYPE_CODE (type) != TYPE_CODE_UNION)
671 error (_("`%s' is not defined as an aggregate type."),
674 write_exp_elt_opcode (OP_SCOPE);
675 write_exp_elt_type (type);
676 write_exp_string ($3);
677 write_exp_elt_opcode (OP_SCOPE);
679 | typebase COLONCOLON '~' name
681 struct type *type = $1;
682 struct stoken tmp_token;
683 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
684 && TYPE_CODE (type) != TYPE_CODE_UNION)
685 error (_("`%s' is not defined as an aggregate type."),
688 if (strcmp (type_name_no_tag (type), $4.ptr) != 0)
689 error (_("invalid destructor `%s::~%s'"),
690 type_name_no_tag (type), $4.ptr);
692 tmp_token.ptr = (char*) alloca ($4.length + 2);
693 tmp_token.length = $4.length + 1;
694 tmp_token.ptr[0] = '~';
695 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
696 tmp_token.ptr[tmp_token.length] = 0;
697 write_exp_elt_opcode (OP_SCOPE);
698 write_exp_elt_type (type);
699 write_exp_string (tmp_token);
700 write_exp_elt_opcode (OP_SCOPE);
704 variable: qualified_name
707 char *name = copy_name ($2);
709 struct minimal_symbol *msymbol;
712 lookup_symbol (name, (const struct block *) NULL,
713 VAR_DOMAIN, (int *) NULL);
716 write_exp_elt_opcode (OP_VAR_VALUE);
717 write_exp_elt_block (NULL);
718 write_exp_elt_sym (sym);
719 write_exp_elt_opcode (OP_VAR_VALUE);
723 msymbol = lookup_minimal_symbol (name, NULL, NULL);
725 write_exp_msymbol (msymbol);
726 else if (!have_full_symbols ()
727 && !have_partial_symbols ())
728 error (_("No symbol table is loaded. "
729 "Use the \"file\" command."));
731 error (_("No symbol \"%s\" in current context."),
736 variable: name_not_typename
737 { struct symbol *sym = $1.sym;
741 if (symbol_read_needs_frame (sym))
743 if (innermost_block == 0 ||
744 contained_in (block_found,
746 innermost_block = block_found;
749 write_exp_elt_opcode (OP_VAR_VALUE);
750 /* We want to use the selected frame, not
751 another more inner frame which happens to
752 be in the same block. */
753 write_exp_elt_block (NULL);
754 write_exp_elt_sym (sym);
755 write_exp_elt_opcode (OP_VAR_VALUE);
757 else if ($1.is_a_field_of_this)
759 /* C++/ObjC: it hangs off of `this'/'self'.
760 Must not inadvertently convert from a
761 method call to data ref. */
762 if (innermost_block == 0 ||
763 contained_in (block_found, innermost_block))
764 innermost_block = block_found;
765 write_exp_elt_opcode (OP_THIS);
766 write_exp_elt_opcode (OP_THIS);
767 write_exp_elt_opcode (STRUCTOP_PTR);
768 write_exp_string ($1.stoken);
769 write_exp_elt_opcode (STRUCTOP_PTR);
773 struct minimal_symbol *msymbol;
774 char *arg = copy_name ($1.stoken);
777 lookup_minimal_symbol (arg, NULL, NULL);
779 write_exp_msymbol (msymbol);
780 else if (!have_full_symbols () &&
781 !have_partial_symbols ())
782 error (_("No symbol table is loaded. "
783 "Use the \"file\" command."));
785 error (_("No symbol \"%s\" in current context."),
786 copy_name ($1.stoken));
793 /* "const" and "volatile" are curently ignored. A type
794 qualifier before the type is currently handled in the
795 typebase rule. The reason for recognizing these here
796 (shift/reduce conflicts) might be obsolete now that some
797 pointer to member rules have been deleted. */
798 | typebase CONST_KEYWORD
799 | typebase VOLATILE_KEYWORD
801 { $$ = follow_types ($1); }
802 | typebase CONST_KEYWORD abs_decl
803 { $$ = follow_types ($1); }
804 | typebase VOLATILE_KEYWORD abs_decl
805 { $$ = follow_types ($1); }
809 { push_type (tp_pointer); $$ = 0; }
811 { push_type (tp_pointer); $$ = $2; }
813 { push_type (tp_reference); $$ = 0; }
815 { push_type (tp_reference); $$ = $2; }
819 direct_abs_decl: '(' abs_decl ')'
821 | direct_abs_decl array_mod
824 push_type (tp_array);
829 push_type (tp_array);
833 | direct_abs_decl func_mod
834 { push_type (tp_function); }
836 { push_type (tp_function); }
847 | '(' nonempty_typelist ')'
848 { free ($2); $$ = 0; }
851 /* We used to try to recognize more pointer to member types here, but
852 that didn't work (shift/reduce conflicts meant that these rules
853 never got executed). The problem is that
854 int (foo::bar::baz::bizzle)
855 is a function type but
856 int (foo::bar::baz::bizzle::*)
857 is a pointer to member type. Stroustrup loses again! */
862 typebase /* Implements (approximately): (type-qualifier)* type-specifier. */
868 error (_("No symbol \"%s\" in current context."),
869 copy_name($1.stoken));
874 { $$ = parse_type->builtin_int; }
876 { $$ = parse_type->builtin_long; }
878 { $$ = parse_type->builtin_short; }
880 { $$ = parse_type->builtin_long; }
881 | UNSIGNED LONG INT_KEYWORD
882 { $$ = parse_type->builtin_unsigned_long; }
884 { $$ = parse_type->builtin_long_long; }
885 | LONG LONG INT_KEYWORD
886 { $$ = parse_type->builtin_long_long; }
888 { $$ = parse_type->builtin_unsigned_long_long; }
889 | UNSIGNED LONG LONG INT_KEYWORD
890 { $$ = parse_type->builtin_unsigned_long_long; }
892 { $$ = parse_type->builtin_short; }
893 | UNSIGNED SHORT INT_KEYWORD
894 { $$ = parse_type->builtin_unsigned_short; }
896 { $$ = parse_type->builtin_double; }
897 | LONG DOUBLE_KEYWORD
898 { $$ = parse_type->builtin_long_double; }
900 { $$ = lookup_struct (copy_name ($2),
901 expression_context_block); }
903 { $$ = lookup_struct (copy_name ($2),
904 expression_context_block); }
906 { $$ = lookup_union (copy_name ($2),
907 expression_context_block); }
909 { $$ = lookup_enum (copy_name ($2),
910 expression_context_block); }
912 { $$ = lookup_unsigned_typename (parse_language,
914 TYPE_NAME($2.type)); }
916 { $$ = parse_type->builtin_unsigned_int; }
917 | SIGNED_KEYWORD typename
918 { $$ = lookup_signed_typename (parse_language,
920 TYPE_NAME($2.type)); }
922 { $$ = parse_type->builtin_int; }
923 | TEMPLATE name '<' type '>'
924 { $$ = lookup_template_type(copy_name($2), $4,
925 expression_context_block);
927 /* "const" and "volatile" are curently ignored. A type
928 qualifier after the type is handled in the ptype rule. I
929 think these could be too. */
930 | CONST_KEYWORD typebase { $$ = $2; }
931 | VOLATILE_KEYWORD typebase { $$ = $2; }
937 $$.stoken.ptr = "int";
938 $$.stoken.length = 3;
939 $$.type = parse_type->builtin_int;
943 $$.stoken.ptr = "long";
944 $$.stoken.length = 4;
945 $$.type = parse_type->builtin_long;
949 $$.stoken.ptr = "short";
950 $$.stoken.length = 5;
951 $$.type = parse_type->builtin_short;
957 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
958 $<ivec>$[0] = 1; /* Number of types in vector. */
961 | nonempty_typelist ',' type
962 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
963 $$ = (struct type **) realloc ((char *) $1, len);
964 $$[$<ivec>$[0]] = $3;
968 name : NAME { $$ = $1.stoken; }
969 | BLOCKNAME { $$ = $1.stoken; }
970 | TYPENAME { $$ = $1.stoken; }
971 | CLASSNAME { $$ = $1.stoken; }
972 | NAME_OR_INT { $$ = $1.stoken; }
975 name_not_typename : NAME
977 /* These would be useful if name_not_typename was useful, but it is
978 just a fake for "variable", so these cause reduce/reduce conflicts
979 because the parser can't tell whether NAME_OR_INT is a
980 name_not_typename (=variable, =exp) or just an exp. If
981 name_not_typename was ever used in an lvalue context where only a
982 name could occur, this might be useful. */
988 /* Take care of parsing a number (anything that starts with a digit).
989 Set yylval and return the token type; update lexptr. LEN is the
990 number of characters in it. */
992 /*** Needs some error checking for the float case. ***/
995 parse_number (p, len, parsed_float, putithere)
1001 /* FIXME: Shouldn't these be unsigned? We don't deal with negative
1002 values here, and we do kind of silly things like cast to
1006 unsigned LONGEST un;
1010 int base = input_radix;
1013 /* Number of "L" suffixes encountered. */
1016 /* We have found a "L" or "U" suffix. */
1017 int found_suffix = 0;
1019 unsigned LONGEST high_bit;
1020 struct type *signed_type;
1021 struct type *unsigned_type;
1025 if (! parse_c_float (parse_gdbarch, p, len,
1026 &putithere->typed_val_float.dval,
1027 &putithere->typed_val_float.type))
1032 /* Handle base-switching prefixes 0x, 0t, 0d, and 0. */
1066 if (c >= 'A' && c <= 'Z')
1068 if (c != 'l' && c != 'u')
1070 if (c >= '0' && c <= '9')
1078 if (base > 10 && c >= 'a' && c <= 'f')
1082 n += i = c - 'a' + 10;
1095 return ERROR; /* Char not a digit. */
1098 return ERROR; /* Invalid digit in this base. */
1100 /* Portably test for overflow (only works for nonzero values, so
1101 make a second check for zero). FIXME: Can't we just make n
1102 and prevn unsigned and avoid this? */
1103 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1104 unsigned_p = 1; /* Try something unsigned. */
1106 /* Portably test for unsigned overflow.
1107 FIXME: This check is wrong; for example it doesn't find
1108 overflow on 0x123456789 when LONGEST is 32 bits. */
1109 if (c != 'l' && c != 'u' && n != 0)
1111 if ((unsigned_p && (unsigned LONGEST) prevn >= (unsigned LONGEST) n))
1112 error (_("Numeric constant too large."));
1117 /* An integer constant is an int, a long, or a long long. An L
1118 suffix forces it to be long; an LL suffix forces it to be long
1119 long. If not forced to a larger size, it gets the first type of
1120 the above that it fits in. To figure out whether it fits, we
1121 shift it right and see whether anything remains. Note that we
1122 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1123 operation, because many compilers will warn about such a shift
1124 (which always produces a zero result). Sometimes gdbarch_int_bit
1125 or gdbarch_long_int will be that big, sometimes not. To deal with
1126 the case where it is we just always shift the value more than
1127 once, with fewer bits each time. */
1129 un = (unsigned LONGEST)n >> 2;
1131 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1134 = ((unsigned LONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1136 /* A large decimal (not hex or octal) constant (between INT_MAX
1137 and UINT_MAX) is a long or unsigned long, according to ANSI,
1138 never an unsigned int, but this code treats it as unsigned
1139 int. This probably should be fixed. GCC gives a warning on
1142 unsigned_type = parse_type->builtin_unsigned_int;
1143 signed_type = parse_type->builtin_int;
1145 else if (long_p <= 1
1146 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1149 = ((unsigned LONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1150 unsigned_type = parse_type->builtin_unsigned_long;
1151 signed_type = parse_type->builtin_long;
1155 high_bit = (((unsigned LONGEST)1)
1156 << (gdbarch_long_long_bit (parse_gdbarch) - 32 - 1)
1160 /* A long long does not fit in a LONGEST. */
1162 (unsigned LONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
1163 unsigned_type = parse_type->builtin_unsigned_long_long;
1164 signed_type = parse_type->builtin_long_long;
1167 putithere->typed_val_int.val = n;
1169 /* If the high bit of the worked out type is set then this number
1170 has to be unsigned. */
1172 if (unsigned_p || (n & high_bit))
1174 putithere->typed_val_int.type = unsigned_type;
1178 putithere->typed_val_int.type = signed_type;
1188 enum exp_opcode opcode;
1191 static const struct token tokentab3[] =
1193 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1194 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1197 static const struct token tokentab2[] =
1199 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1200 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1201 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1202 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1203 {"%=", ASSIGN_MODIFY, BINOP_REM},
1204 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1205 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1206 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1207 {"++", INCREMENT, BINOP_END},
1208 {"--", DECREMENT, BINOP_END},
1209 {"->", ARROW, BINOP_END},
1210 {"&&", ANDAND, BINOP_END},
1211 {"||", OROR, BINOP_END},
1212 {"::", COLONCOLON, BINOP_END},
1213 {"<<", LSH, BINOP_END},
1214 {">>", RSH, BINOP_END},
1215 {"==", EQUAL, BINOP_END},
1216 {"!=", NOTEQUAL, BINOP_END},
1217 {"<=", LEQ, BINOP_END},
1218 {">=", GEQ, BINOP_END}
1221 /* Read one token, getting characters through lexptr. */
1232 static char *tempbuf;
1233 static int tempbufsize;
1238 /* See if it is a special token of length 3. */
1239 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1240 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
1243 yylval.opcode = tokentab3[i].opcode;
1244 return tokentab3[i].token;
1247 /* See if it is a special token of length 2. */
1248 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1249 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
1252 yylval.opcode = tokentab2[i].opcode;
1253 return tokentab2[i].token;
1257 switch (tokchr = *tokstart)
1269 /* We either have a character constant ('0' or '\177' for
1270 example) or we have a quoted symbol reference ('foo(int,int)'
1271 in C++ for example). */
1275 c = parse_escape (parse_gdbarch, &lexptr);
1277 error (_("Empty character constant."));
1279 yylval.typed_val_int.val = c;
1280 yylval.typed_val_int.type = parse_type->builtin_char;
1285 namelen = skip_quoted (tokstart) - tokstart;
1288 lexptr = tokstart + namelen;
1289 if (lexptr[-1] != '\'')
1290 error (_("Unmatched single quote."));
1295 error (_("Invalid character constant."));
1305 if (paren_depth == 0)
1312 if (comma_terminates && paren_depth == 0)
1318 /* Might be a floating point number. */
1319 if (lexptr[1] < '0' || lexptr[1] > '9')
1320 goto symbol; /* Nope, must be a symbol. */
1321 /* FALL THRU into number case. */
1334 /* It's a number. */
1335 int got_dot = 0, got_e = 0, toktype = FLOAT;
1336 /* Initialize toktype to anything other than ERROR. */
1338 int hex = input_radix > 10;
1339 int local_radix = input_radix;
1340 if (tokchr == '0' && (p[1] == 'x' || p[1] == 'X'))
1346 else if (tokchr == '0' && (p[1]=='t' || p[1]=='T'
1347 || p[1]=='d' || p[1]=='D'))
1356 /* This test includes !hex because 'e' is a valid hex digit
1357 and thus does not indicate a floating point number when
1358 the radix is hex. */
1360 if (!hex && (*p == 'e' || *p == 'E'))
1362 toktype = ERROR; /* Only one 'e' in a float. */
1365 /* This test does not include !hex, because a '.' always
1366 indicates a decimal floating point number regardless of
1370 toktype = ERROR; /* Only one '.' in a float. */
1373 else if (got_e && (p[-1] == 'e' || p[-1] == 'E') &&
1374 (*p == '-' || *p == '+'))
1375 /* This is the sign of the exponent, not the end of the
1378 /* Always take decimal digits; parse_number handles radix
1380 else if (*p >= '0' && *p <= '9')
1382 /* We will take letters only if hex is true, and only up
1383 to what the input radix would permit. FSF was content
1384 to rely on parse_number to validate; but it leaks. */
1385 else if (*p >= 'a' && *p <= 'z')
1387 if (!hex || *p >= ('a' + local_radix - 10))
1390 else if (*p >= 'A' && *p <= 'Z')
1392 if (!hex || *p >= ('A' + local_radix - 10))
1397 if (toktype != ERROR)
1398 toktype = parse_number (tokstart, p - tokstart,
1399 got_dot | got_e, &yylval);
1400 if (toktype == ERROR)
1402 char *err_copy = (char *) alloca (p - tokstart + 1);
1404 memcpy (err_copy, tokstart, p - tokstart);
1405 err_copy[p - tokstart] = 0;
1406 error (_("Invalid number \"%s\"."), err_copy);
1423 case '@': /* Moved out below. */
1439 if (strncmp(tokstart, "@selector", 9) == 0)
1441 tokptr = strchr(tokstart, '(');
1444 error (_("Missing '(' in @selector(...)"));
1447 tokptr++; /* Skip the '('. */
1449 /* Grow the static temp buffer if necessary, including
1450 allocating the first one on demand. */
1451 if (tempbufindex + 1 >= tempbufsize)
1453 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1455 tempbuf[tempbufindex++] = *tokptr++;
1456 } while ((*tokptr != ')') && (*tokptr != '\0'));
1457 if (*tokptr++ != ')')
1459 error (_("Missing ')' in @selector(...)"));
1461 tempbuf[tempbufindex] = '\0';
1462 yylval.sval.ptr = tempbuf;
1463 yylval.sval.length = tempbufindex;
1467 if (tokstart[1] != '"')
1472 /* ObjC NextStep NSString constant: fall thru and parse like
1478 /* Build the gdb internal form of the input string in tempbuf,
1479 translating any standard C escape forms seen. Note that the
1480 buffer is null byte terminated *only* for the convenience of
1481 debugging gdb itself and printing the buffer contents when
1482 the buffer contains no embedded nulls. Gdb does not depend
1483 upon the buffer being null byte terminated, it uses the
1484 length string instead. This allows gdb to handle C strings
1485 (as well as strings in other languages) with embedded null
1488 tokptr = ++tokstart;
1492 /* Grow the static temp buffer if necessary, including
1493 allocating the first one on demand. */
1494 if (tempbufindex + 1 >= tempbufsize)
1496 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1502 /* Do nothing, loop will terminate. */
1506 c = parse_escape (parse_gdbarch, &tokptr);
1511 tempbuf[tempbufindex++] = c;
1514 tempbuf[tempbufindex++] = *tokptr++;
1517 } while ((*tokptr != '"') && (*tokptr != '\0'));
1518 if (*tokptr++ != '"')
1520 error (_("Unterminated string in expression."));
1522 tempbuf[tempbufindex] = '\0'; /* See note above. */
1523 yylval.sval.ptr = tempbuf;
1524 yylval.sval.length = tempbufindex;
1526 return (tokchr == '@' ? NSSTRING : STRING);
1529 if (!(tokchr == '_' || tokchr == '$' ||
1530 (tokchr >= 'a' && tokchr <= 'z') || (tokchr >= 'A' && tokchr <= 'Z')))
1531 /* We must have come across a bad character (e.g. ';'). */
1532 error (_("Invalid character '%c' in expression."), c);
1534 /* It's a name. See how long it is. */
1536 for (c = tokstart[namelen];
1537 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1538 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1543 while (tokstart[++i] && tokstart[i] != '>');
1544 if (tokstart[i] == '>')
1547 c = tokstart[++namelen];
1550 /* The token "if" terminates the expression and is NOT
1551 removed from the input stream. */
1552 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1561 /* Catch specific keywords. Should be done with a data structure. */
1565 if (strncmp (tokstart, "unsigned", 8) == 0)
1567 if (parse_language->la_language == language_cplus
1568 && strncmp (tokstart, "template", 8) == 0)
1570 if (strncmp (tokstart, "volatile", 8) == 0)
1571 return VOLATILE_KEYWORD;
1574 if (strncmp (tokstart, "struct", 6) == 0)
1576 if (strncmp (tokstart, "signed", 6) == 0)
1577 return SIGNED_KEYWORD;
1578 if (strncmp (tokstart, "sizeof", 6) == 0)
1580 if (strncmp (tokstart, "double", 6) == 0)
1581 return DOUBLE_KEYWORD;
1584 if ((parse_language->la_language == language_cplus)
1585 && strncmp (tokstart, "class", 5) == 0)
1587 if (strncmp (tokstart, "union", 5) == 0)
1589 if (strncmp (tokstart, "short", 5) == 0)
1591 if (strncmp (tokstart, "const", 5) == 0)
1592 return CONST_KEYWORD;
1595 if (strncmp (tokstart, "enum", 4) == 0)
1597 if (strncmp (tokstart, "long", 4) == 0)
1601 if (strncmp (tokstart, "int", 3) == 0)
1608 yylval.sval.ptr = tokstart;
1609 yylval.sval.length = namelen;
1611 if (*tokstart == '$')
1613 write_dollar_variable (yylval.sval);
1617 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1618 functions or symtabs. If this is not so, then ...
1619 Use token-type TYPENAME for symbols that happen to be defined
1620 currently as names of types; NAME for other symbols.
1621 The caller is not constrained to care about the distinction. */
1623 char *tmp = copy_name (yylval.sval);
1625 int is_a_field_of_this = 0, *need_this;
1628 if (parse_language->la_language == language_cplus ||
1629 parse_language->la_language == language_objc)
1630 need_this = &is_a_field_of_this;
1632 need_this = (int *) NULL;
1634 sym = lookup_symbol (tmp, expression_context_block,
1637 /* Call lookup_symtab, not lookup_partial_symtab, in case there
1638 are no psymtabs (coff, xcoff, or some future change to blow
1639 away the psymtabs once symbols are read). */
1640 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1641 lookup_symtab (tmp))
1643 yylval.ssym.sym = sym;
1644 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1647 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1650 /* Despite the following flaw, we need to keep this code
1651 enabled. Because we can get called from
1652 check_stub_method, if we don't handle nested types then
1653 it screws many operations in any program which uses
1655 /* In "A::x", if x is a member function of A and there
1656 happens to be a type (nested or not, since the stabs
1657 don't make that distinction) named x, then this code
1658 incorrectly thinks we are dealing with nested types
1659 rather than a member function. */
1663 struct symbol *best_sym;
1665 /* Look ahead to detect nested types. This probably should
1666 be done in the grammar, but trying seemed to introduce a
1667 lot of shift/reduce and reduce/reduce conflicts. It's
1668 possible that it could be done, though. Or perhaps a
1669 non-grammar, but less ad hoc, approach would work well. */
1671 /* Since we do not currently have any way of distinguishing
1672 a nested type from a non-nested one (the stabs don't tell
1673 us whether a type is nested), we just ignore the
1680 /* Skip whitespace. */
1681 while (*p == ' ' || *p == '\t' || *p == '\n')
1683 if (*p == ':' && p[1] == ':')
1685 /* Skip the `::'. */
1687 /* Skip whitespace. */
1688 while (*p == ' ' || *p == '\t' || *p == '\n')
1691 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1692 || (*p >= 'a' && *p <= 'z')
1693 || (*p >= 'A' && *p <= 'Z'))
1697 struct symbol *cur_sym;
1698 /* As big as the whole rest of the expression,
1699 which is at least big enough. */
1700 char *ncopy = alloca (strlen (tmp) +
1701 strlen (namestart) + 3);
1705 memcpy (tmp1, tmp, strlen (tmp));
1706 tmp1 += strlen (tmp);
1707 memcpy (tmp1, "::", 2);
1709 memcpy (tmp1, namestart, p - namestart);
1710 tmp1[p - namestart] = '\0';
1711 cur_sym = lookup_symbol (ncopy,
1712 expression_context_block,
1713 VAR_DOMAIN, (int *) NULL);
1716 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1734 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1736 yylval.tsym.type = SYMBOL_TYPE (sym);
1741 = language_lookup_primitive_type_by_name (parse_language,
1742 parse_gdbarch, tmp);
1743 if (yylval.tsym.type != NULL)
1746 /* See if it's an ObjC classname. */
1749 CORE_ADDR Class = lookup_objc_class (parse_gdbarch, tmp);
1752 yylval.class.class = Class;
1753 if ((sym = lookup_struct_typedef (tmp,
1754 expression_context_block,
1756 yylval.class.type = SYMBOL_TYPE (sym);
1761 /* Input names that aren't symbols but ARE valid hex numbers,
1762 when the input radix permits them, can be names or numbers
1763 depending on the parse. Note we support radixes > 16 here. */
1765 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1766 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1768 YYSTYPE newlval; /* Its value is ignored. */
1769 hextype = parse_number (tokstart, namelen, 0, &newlval);
1772 yylval.ssym.sym = sym;
1773 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1778 /* Any other kind of symbol. */
1779 yylval.ssym.sym = sym;
1780 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1789 if (*lexptr == '\0')
1790 error(_("A %s near end of expression."), (msg ? msg : "error"));
1792 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"),