1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Parse a C expression from text in a string, and return the result
22 as a struct expression pointer. That structure contains arithmetic
23 operations in reverse polish, with constants represented by
24 operations that are followed by special data. See expression.h for
25 the details of the format. What is important here is that it can
26 be built up sequentially during the process of parsing; the lower
27 levels of the tree always come first in the result.
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the
32 parser generator. Doing this with #defines and trying to control
33 the interaction with include files (<malloc.h> and <stdlib.h> for
34 example) just became too messy, particularly when such includes can
35 be inserted at random times by the parser generator. */
40 #include "gdb_string.h"
42 #include "expression.h"
44 #include "objc-lang.h" /* For objc language constructs. */
47 #include "parser-defs.h"
50 #include "bfd.h" /* Required by objfiles.h. */
51 #include "symfile.h" /* Required by objfiles.h. */
52 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols. */
54 #include "completer.h" /* For skip_quoted(). */
56 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
57 etc), as well as gratuitiously global symbol names, so we can have
58 multiple yacc generated parsers in gdb. Note that these are only
59 the variables produced by yacc. If other parser generators (bison,
60 byacc, etc) produce additional global names that conflict at link
61 time, then those parser generators need to be fixed instead of
62 adding those names to this list. */
64 #define yymaxdepth objc_maxdepth
65 #define yyparse objc_parse
66 #define yylex objc_lex
67 #define yyerror objc_error
68 #define yylval objc_lval
69 #define yychar objc_char
70 #define yydebug objc_debug
71 #define yypact objc_pact
74 #define yydef objc_def
75 #define yychk objc_chk
76 #define yypgo objc_pgo
77 #define yyact objc_act
78 #define yyexca objc_exca
79 #define yyerrflag objc_errflag
80 #define yynerrs objc_nerrs
84 #define yy_yys objc_yys
85 #define yystate objc_state
86 #define yytmp objc_tmp
88 #define yy_yyv objc_yyv
89 #define yyval objc_val
90 #define yylloc objc_lloc
91 #define yyreds objc_reds /* With YYDEBUG defined */
92 #define yytoks objc_toks /* With YYDEBUG defined */
93 #define yyname objc_name /* With YYDEBUG defined */
94 #define yyrule objc_rule /* With YYDEBUG defined */
95 #define yylhs objc_yylhs
96 #define yylen objc_yylen
97 #define yydefred objc_yydefred
98 #define yydgoto objc_yydgoto
99 #define yysindex objc_yysindex
100 #define yyrindex objc_yyrindex
101 #define yygindex objc_yygindex
102 #define yytable objc_yytable
103 #define yycheck objc_yycheck
106 #define YYDEBUG 0 /* Default to no yydebug support. */
110 yyparse PARAMS ((void));
113 yylex PARAMS ((void));
116 yyerror PARAMS ((char *));
120 /* Although the yacc "value" of an expression is not used,
121 since the result is stored in the structure being created,
122 other node types do have values. */
139 struct symtoken ssym;
142 enum exp_opcode opcode;
143 struct internalvar *ivar;
144 struct objc_class_str class;
151 /* YYSTYPE gets defined by %union. */
153 parse_number PARAMS ((char *, int, int, YYSTYPE *));
156 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
158 %type <tval> type typebase
159 %type <tvec> nonempty_typelist
160 /* %type <bval> block */
162 /* Fancy type parsing. */
163 %type <voidval> func_mod direct_abs_decl abs_decl
165 %type <lval> array_mod
167 %token <typed_val_int> INT
168 %token <typed_val_float> FLOAT
170 /* Both NAME and TYPENAME tokens represent symbols in the input, and
171 both convey their data as strings. But a TYPENAME is a string that
172 happens to be defined as a typedef or builtin type name (such as
173 int or char) and a NAME is any other symbol. Contexts where this
174 distinction is not important can use the nonterminal "name", which
175 matches either NAME or TYPENAME. */
178 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
179 %token <sval> SELECTOR /* ObjC "@selector" pseudo-operator */
180 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
181 %token <tsym> TYPENAME
182 %token <class> CLASSNAME /* ObjC Class name */
184 %type <ssym> name_not_typename
185 %type <tsym> typename
187 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
188 but which would parse as a valid number in the current input radix.
189 E.g. "c" when input_radix==16. Depending on the parse, it will be
190 turned into a name or into a number. */
192 %token <ssym> NAME_OR_INT
194 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
198 /* Special type cases, put in to allow the parser to distinguish
199 different legal basetypes. */
200 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
202 %token <voidval> VARIABLE
204 %token <opcode> ASSIGN_MODIFY
211 %right '=' ASSIGN_MODIFY
219 %left '<' '>' LEQ GEQ
224 %right UNARY INCREMENT DECREMENT
225 %right ARROW '.' '[' '('
226 %token <ssym> BLOCKNAME
238 { write_exp_elt_opcode(OP_TYPE);
239 write_exp_elt_type($1);
240 write_exp_elt_opcode(OP_TYPE);}
243 /* Expressions, including the comma operator. */
246 { write_exp_elt_opcode (BINOP_COMMA); }
249 /* Expressions, not including the comma operator. */
250 exp : '*' exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_IND); }
253 exp : '&' exp %prec UNARY
254 { 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 (copy_name ($2.stoken));
338 error ("%s is not an ObjC Class",
339 copy_name ($2.stoken));
340 write_exp_elt_opcode (OP_LONG);
341 write_exp_elt_type (builtin_type_int);
342 write_exp_elt_longcst ((LONGEST) class);
343 write_exp_elt_opcode (OP_LONG);
347 { write_exp_elt_opcode (OP_MSGCALL);
349 write_exp_elt_opcode (OP_MSGCALL);
355 write_exp_elt_opcode (OP_LONG);
356 write_exp_elt_type (builtin_type_int);
357 write_exp_elt_longcst ((LONGEST) $2.class);
358 write_exp_elt_opcode (OP_LONG);
362 { write_exp_elt_opcode (OP_MSGCALL);
364 write_exp_elt_opcode (OP_MSGCALL);
371 { write_exp_elt_opcode (OP_MSGCALL);
373 write_exp_elt_opcode (OP_MSGCALL);
378 { add_msglist(&$1, 0); }
386 msgarg : name ':' exp
387 { add_msglist(&$1, 1); }
388 | ':' exp /* Unnamed arg. */
389 { add_msglist(0, 1); }
390 | ',' exp /* Variable number of args. */
391 { add_msglist(0, 0); }
395 /* This is to save the value of arglist_len
396 being accumulated by an outer function call. */
397 { start_arglist (); }
398 arglist ')' %prec ARROW
399 { write_exp_elt_opcode (OP_FUNCALL);
400 write_exp_elt_longcst ((LONGEST) end_arglist ());
401 write_exp_elt_opcode (OP_FUNCALL); }
405 { start_arglist (); }
415 arglist : arglist ',' exp %prec ABOVE_COMMA
420 { $$ = end_arglist () - 1; }
422 exp : lcurly arglist rcurly %prec ARROW
423 { write_exp_elt_opcode (OP_ARRAY);
424 write_exp_elt_longcst ((LONGEST) 0);
425 write_exp_elt_longcst ((LONGEST) $3);
426 write_exp_elt_opcode (OP_ARRAY); }
429 exp : lcurly type rcurly exp %prec UNARY
430 { write_exp_elt_opcode (UNOP_MEMVAL);
431 write_exp_elt_type ($2);
432 write_exp_elt_opcode (UNOP_MEMVAL); }
435 exp : '(' type ')' exp %prec UNARY
436 { write_exp_elt_opcode (UNOP_CAST);
437 write_exp_elt_type ($2);
438 write_exp_elt_opcode (UNOP_CAST); }
445 /* Binary operators in order of decreasing precedence. */
448 { write_exp_elt_opcode (BINOP_REPEAT); }
452 { write_exp_elt_opcode (BINOP_MUL); }
456 { write_exp_elt_opcode (BINOP_DIV); }
460 { write_exp_elt_opcode (BINOP_REM); }
464 { write_exp_elt_opcode (BINOP_ADD); }
468 { write_exp_elt_opcode (BINOP_SUB); }
472 { write_exp_elt_opcode (BINOP_LSH); }
476 { write_exp_elt_opcode (BINOP_RSH); }
480 { write_exp_elt_opcode (BINOP_EQUAL); }
483 exp : exp NOTEQUAL exp
484 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
488 { write_exp_elt_opcode (BINOP_LEQ); }
492 { write_exp_elt_opcode (BINOP_GEQ); }
496 { write_exp_elt_opcode (BINOP_LESS); }
500 { write_exp_elt_opcode (BINOP_GTR); }
504 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
508 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
512 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
516 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
520 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
523 exp : exp '?' exp ':' exp %prec '?'
524 { write_exp_elt_opcode (TERNOP_COND); }
528 { write_exp_elt_opcode (BINOP_ASSIGN); }
531 exp : exp ASSIGN_MODIFY exp
532 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
533 write_exp_elt_opcode ($2);
534 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
538 { write_exp_elt_opcode (OP_LONG);
539 write_exp_elt_type ($1.type);
540 write_exp_elt_longcst ((LONGEST)($1.val));
541 write_exp_elt_opcode (OP_LONG); }
546 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
547 write_exp_elt_opcode (OP_LONG);
548 write_exp_elt_type (val.typed_val_int.type);
549 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
550 write_exp_elt_opcode (OP_LONG);
556 { write_exp_elt_opcode (OP_DOUBLE);
557 write_exp_elt_type ($1.type);
558 write_exp_elt_dblcst ($1.dval);
559 write_exp_elt_opcode (OP_DOUBLE); }
566 /* Already written by write_dollar_variable. */
571 write_exp_elt_opcode (OP_SELECTOR);
572 write_exp_string ($1);
573 write_exp_elt_opcode (OP_SELECTOR); }
575 exp : SIZEOF '(' type ')' %prec UNARY
576 { write_exp_elt_opcode (OP_LONG);
577 write_exp_elt_type (builtin_type_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 (builtin_type_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 (builtin_type_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_NSSTRING);
612 write_exp_string ($1);
613 write_exp_elt_opcode (OP_NSSTRING); }
618 { write_exp_elt_opcode (OP_THIS);
619 write_exp_elt_opcode (OP_THIS); }
627 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
631 lookup_symtab (copy_name ($1.stoken));
633 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
635 error ("No file or function \"%s\".",
636 copy_name ($1.stoken));
641 block : block COLONCOLON name
643 = lookup_symbol (copy_name ($3), $1,
644 VAR_NAMESPACE, (int *) NULL,
645 (struct symtab **) NULL);
646 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
647 error ("No function \"%s\" in specified context.",
649 $$ = SYMBOL_BLOCK_VALUE (tem); }
652 variable: block COLONCOLON name
653 { struct symbol *sym;
654 sym = lookup_symbol (copy_name ($3), $1,
655 VAR_NAMESPACE, (int *) NULL,
656 (struct symtab **) NULL);
658 error ("No symbol \"%s\" in specified context.",
661 write_exp_elt_opcode (OP_VAR_VALUE);
662 /* block_found is set by lookup_symbol. */
663 write_exp_elt_block (block_found);
664 write_exp_elt_sym (sym);
665 write_exp_elt_opcode (OP_VAR_VALUE); }
668 qualified_name: typebase COLONCOLON name
670 struct type *type = $1;
671 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
672 && TYPE_CODE (type) != TYPE_CODE_UNION)
673 error ("`%s' is not defined as an aggregate type.",
676 write_exp_elt_opcode (OP_SCOPE);
677 write_exp_elt_type (type);
678 write_exp_string ($3);
679 write_exp_elt_opcode (OP_SCOPE);
681 | typebase COLONCOLON '~' name
683 struct type *type = $1;
684 struct stoken tmp_token;
685 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
686 && TYPE_CODE (type) != TYPE_CODE_UNION)
687 error ("`%s' is not defined as an aggregate type.",
690 if (!STREQ (type_name_no_tag (type), $4.ptr))
691 error ("invalid destructor `%s::~%s'",
692 type_name_no_tag (type), $4.ptr);
694 tmp_token.ptr = (char*) alloca ($4.length + 2);
695 tmp_token.length = $4.length + 1;
696 tmp_token.ptr[0] = '~';
697 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
698 tmp_token.ptr[tmp_token.length] = 0;
699 write_exp_elt_opcode (OP_SCOPE);
700 write_exp_elt_type (type);
701 write_exp_string (tmp_token);
702 write_exp_elt_opcode (OP_SCOPE);
706 variable: qualified_name
709 char *name = copy_name ($2);
711 struct minimal_symbol *msymbol;
714 lookup_symbol (name, (const struct block *) NULL,
715 VAR_NAMESPACE, (int *) NULL,
716 (struct symtab **) NULL);
719 write_exp_elt_opcode (OP_VAR_VALUE);
720 write_exp_elt_block (NULL);
721 write_exp_elt_sym (sym);
722 write_exp_elt_opcode (OP_VAR_VALUE);
726 msymbol = lookup_minimal_symbol (name, NULL, NULL);
729 write_exp_msymbol (msymbol,
730 lookup_function_type (builtin_type_int),
734 if (!have_full_symbols () && !have_partial_symbols ())
735 error ("No symbol table is loaded. Use the \"file\" command.");
737 error ("No symbol \"%s\" in current context.", name);
741 variable: name_not_typename
742 { struct symbol *sym = $1.sym;
746 if (symbol_read_needs_frame (sym))
748 if (innermost_block == 0 ||
749 contained_in (block_found,
751 innermost_block = block_found;
754 write_exp_elt_opcode (OP_VAR_VALUE);
755 /* We want to use the selected frame, not
756 another more inner frame which happens to
757 be in the same block. */
758 write_exp_elt_block (NULL);
759 write_exp_elt_sym (sym);
760 write_exp_elt_opcode (OP_VAR_VALUE);
762 else if ($1.is_a_field_of_this)
764 /* C++/ObjC: it hangs off of `this'/'self'.
765 Must not inadvertently convert from a
766 method call to data ref. */
767 if (innermost_block == 0 ||
768 contained_in (block_found, innermost_block))
769 innermost_block = block_found;
770 write_exp_elt_opcode (OP_SELF);
771 write_exp_elt_opcode (OP_SELF);
772 write_exp_elt_opcode (STRUCTOP_PTR);
773 write_exp_string ($1.stoken);
774 write_exp_elt_opcode (STRUCTOP_PTR);
778 struct minimal_symbol *msymbol;
779 register char *arg = copy_name ($1.stoken);
782 lookup_minimal_symbol (arg, NULL, NULL);
785 write_exp_msymbol (msymbol,
786 lookup_function_type (builtin_type_int),
789 else if (!have_full_symbols () &&
790 !have_partial_symbols ())
791 error ("No symbol table is loaded. Use the \"file\" command.");
793 error ("No symbol \"%s\" in current context.",
794 copy_name ($1.stoken));
801 /* "const" and "volatile" are curently ignored. A type
802 qualifier before the type is currently handled in the
803 typebase rule. The reason for recognizing these here
804 (shift/reduce conflicts) might be obsolete now that some
805 pointer to member rules have been deleted. */
806 | typebase CONST_KEYWORD
807 | typebase VOLATILE_KEYWORD
809 { $$ = follow_types ($1); }
810 | typebase CONST_KEYWORD abs_decl
811 { $$ = follow_types ($1); }
812 | typebase VOLATILE_KEYWORD abs_decl
813 { $$ = follow_types ($1); }
817 { push_type (tp_pointer); $$ = 0; }
819 { push_type (tp_pointer); $$ = $2; }
821 { push_type (tp_reference); $$ = 0; }
823 { push_type (tp_reference); $$ = $2; }
827 direct_abs_decl: '(' abs_decl ')'
829 | direct_abs_decl array_mod
832 push_type (tp_array);
837 push_type (tp_array);
841 | direct_abs_decl func_mod
842 { push_type (tp_function); }
844 { push_type (tp_function); }
855 | '(' nonempty_typelist ')'
856 { free ((PTR)$2); $$ = 0; }
859 /* We used to try to recognize more pointer to member types here, but
860 that didn't work (shift/reduce conflicts meant that these rules
861 never got executed). The problem is that
862 int (foo::bar::baz::bizzle)
863 is a function type but
864 int (foo::bar::baz::bizzle::*)
865 is a pointer to member type. Stroustrup loses again! */
868 | typebase COLONCOLON '*'
869 { $$ = lookup_member_type (builtin_type_int, $1); }
872 typebase /* Implements (approximately): (type-qualifier)* type-specifier. */
878 error ("No symbol \"%s\" in current context.",
879 copy_name($1.stoken));
884 { $$ = builtin_type_int; }
886 { $$ = builtin_type_long; }
888 { $$ = builtin_type_short; }
890 { $$ = builtin_type_long; }
891 | UNSIGNED LONG INT_KEYWORD
892 { $$ = builtin_type_unsigned_long; }
894 { $$ = builtin_type_long_long; }
895 | LONG LONG INT_KEYWORD
896 { $$ = builtin_type_long_long; }
898 { $$ = builtin_type_unsigned_long_long; }
899 | UNSIGNED LONG LONG INT_KEYWORD
900 { $$ = builtin_type_unsigned_long_long; }
902 { $$ = builtin_type_short; }
903 | UNSIGNED SHORT INT_KEYWORD
904 { $$ = builtin_type_unsigned_short; }
906 { $$ = builtin_type_double; }
907 | LONG DOUBLE_KEYWORD
908 { $$ = builtin_type_long_double; }
910 { $$ = lookup_struct (copy_name ($2),
911 expression_context_block); }
913 { $$ = lookup_struct (copy_name ($2),
914 expression_context_block); }
916 { $$ = lookup_union (copy_name ($2),
917 expression_context_block); }
919 { $$ = lookup_enum (copy_name ($2),
920 expression_context_block); }
922 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
924 { $$ = builtin_type_unsigned_int; }
925 | SIGNED_KEYWORD typename
926 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
928 { $$ = builtin_type_int; }
929 | TEMPLATE name '<' type '>'
930 { $$ = lookup_template_type(copy_name($2), $4,
931 expression_context_block);
933 /* "const" and "volatile" are curently ignored. A type
934 qualifier after the type is handled in the ptype rule. I
935 think these could be too. */
936 | CONST_KEYWORD typebase { $$ = $2; }
937 | VOLATILE_KEYWORD typebase { $$ = $2; }
943 $$.stoken.ptr = "int";
944 $$.stoken.length = 3;
945 $$.type = builtin_type_int;
949 $$.stoken.ptr = "long";
950 $$.stoken.length = 4;
951 $$.type = builtin_type_long;
955 $$.stoken.ptr = "short";
956 $$.stoken.length = 5;
957 $$.type = builtin_type_short;
963 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
964 $<ivec>$[0] = 1; /* Number of types in vector. */
967 | nonempty_typelist ',' type
968 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
969 $$ = (struct type **) realloc ((char *) $1, len);
970 $$[$<ivec>$[0]] = $3;
974 name : NAME { $$ = $1.stoken; }
975 | BLOCKNAME { $$ = $1.stoken; }
976 | TYPENAME { $$ = $1.stoken; }
977 | CLASSNAME { $$ = $1.stoken; }
978 | NAME_OR_INT { $$ = $1.stoken; }
981 name_not_typename : NAME
983 /* These would be useful if name_not_typename was useful, but it is
984 just a fake for "variable", so these cause reduce/reduce conflicts
985 because the parser can't tell whether NAME_OR_INT is a
986 name_not_typename (=variable, =exp) or just an exp. If
987 name_not_typename was ever used in an lvalue context where only a
988 name could occur, this might be useful. */
994 /* Take care of parsing a number (anything that starts with a digit).
995 Set yylval and return the token type; update lexptr. LEN is the
996 number of characters in it. */
998 /*** Needs some error checking for the float case. ***/
1001 parse_number (p, len, parsed_float, putithere)
1007 /* FIXME: Shouldn't these be unsigned? We don't deal with negative
1008 values here, and we do kind of silly things like cast to
1010 register LONGEST n = 0;
1011 register LONGEST prevn = 0;
1012 unsigned LONGEST un;
1016 register int base = input_radix;
1019 /* Number of "L" suffixes encountered. */
1022 /* We have found a "L" or "U" suffix. */
1023 int found_suffix = 0;
1025 unsigned LONGEST high_bit;
1026 struct type *signed_type;
1027 struct type *unsigned_type;
1033 /* It's a float since it contains a point or an exponent. */
1035 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
1036 sscanf (p, "%g", &putithere->typed_val_float.dval);
1037 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
1038 sscanf (p, "%lg", &putithere->typed_val_float.dval);
1041 #ifdef PRINTF_HAS_LONG_DOUBLE
1042 sscanf (p, "%Lg", &putithere->typed_val_float.dval);
1044 /* Scan it into a double, then assign it to the long double.
1045 This at least wins with values representable in the range
1048 sscanf (p, "%lg", &temp);
1049 putithere->typed_val_float.dval = temp;
1053 /* See if it has `f' or `l' suffix (float or long double). */
1055 c = tolower (p[len - 1]);
1058 putithere->typed_val_float.type = builtin_type_float;
1060 putithere->typed_val_float.type = builtin_type_long_double;
1061 else if (isdigit (c) || c == '.')
1062 putithere->typed_val_float.type = builtin_type_double;
1069 /* Handle base-switching prefixes 0x, 0t, 0d, and 0. */
1103 if (c >= 'A' && c <= 'Z')
1105 if (c != 'l' && c != 'u')
1107 if (c >= '0' && c <= '9')
1115 if (base > 10 && c >= 'a' && c <= 'f')
1119 n += i = c - 'a' + 10;
1132 return ERROR; /* Char not a digit. */
1135 return ERROR; /* Invalid digit in this base. */
1137 /* Portably test for overflow (only works for nonzero values, so
1138 make a second check for zero). FIXME: Can't we just make n
1139 and prevn unsigned and avoid this? */
1140 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1141 unsigned_p = 1; /* Try something unsigned. */
1143 /* Portably test for unsigned overflow.
1144 FIXME: This check is wrong; for example it doesn't find
1145 overflow on 0x123456789 when LONGEST is 32 bits. */
1146 if (c != 'l' && c != 'u' && n != 0)
1148 if ((unsigned_p && (unsigned LONGEST) prevn >= (unsigned LONGEST) n))
1149 error ("Numeric constant too large.");
1154 /* An integer constant is an int, a long, or a long long. An L
1155 suffix forces it to be long; an LL suffix forces it to be long
1156 long. If not forced to a larger size, it gets the first type of
1157 the above that it fits in. To figure out whether it fits, we
1158 shift it right and see whether anything remains. Note that we
1159 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1160 operation, because many compilers will warn about such a shift
1161 (which always produces a zero result). Sometimes TARGET_INT_BIT
1162 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1163 the case where it is we just always shift the value more than
1164 once, with fewer bits each time. */
1166 un = (unsigned LONGEST)n >> 2;
1168 && (un >> (TARGET_INT_BIT - 2)) == 0)
1170 high_bit = ((unsigned LONGEST)1) << (TARGET_INT_BIT-1);
1172 /* A large decimal (not hex or octal) constant (between INT_MAX
1173 and UINT_MAX) is a long or unsigned long, according to ANSI,
1174 never an unsigned int, but this code treats it as unsigned
1175 int. This probably should be fixed. GCC gives a warning on
1178 unsigned_type = builtin_type_unsigned_int;
1179 signed_type = builtin_type_int;
1181 else if (long_p <= 1
1182 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1184 high_bit = ((unsigned LONGEST)1) << (TARGET_LONG_BIT-1);
1185 unsigned_type = builtin_type_unsigned_long;
1186 signed_type = builtin_type_long;
1190 high_bit = (((unsigned LONGEST)1)
1191 << (TARGET_LONG_LONG_BIT - 32 - 1)
1195 /* A long long does not fit in a LONGEST. */
1197 (unsigned LONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
1198 unsigned_type = builtin_type_unsigned_long_long;
1199 signed_type = builtin_type_long_long;
1202 putithere->typed_val_int.val = n;
1204 /* If the high bit of the worked out type is set then this number
1205 has to be unsigned. */
1207 if (unsigned_p || (n & high_bit))
1209 putithere->typed_val_int.type = unsigned_type;
1213 putithere->typed_val_int.type = signed_type;
1223 enum exp_opcode opcode;
1226 static const struct token tokentab3[] =
1228 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1229 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1232 static const struct token tokentab2[] =
1234 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1235 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1236 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1237 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1238 {"%=", ASSIGN_MODIFY, BINOP_REM},
1239 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1240 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1241 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1242 {"++", INCREMENT, BINOP_END},
1243 {"--", DECREMENT, BINOP_END},
1244 {"->", ARROW, BINOP_END},
1245 {"&&", ANDAND, BINOP_END},
1246 {"||", OROR, BINOP_END},
1247 {"::", COLONCOLON, BINOP_END},
1248 {"<<", LSH, BINOP_END},
1249 {">>", RSH, BINOP_END},
1250 {"==", EQUAL, BINOP_END},
1251 {"!=", NOTEQUAL, BINOP_END},
1252 {"<=", LEQ, BINOP_END},
1253 {">=", GEQ, BINOP_END}
1256 /* Read one token, getting characters through lexptr. */
1267 static char *tempbuf;
1268 static int tempbufsize;
1273 /* See if it is a special token of length 3. */
1274 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1275 if (STREQN (tokstart, tokentab3[i].operator, 3))
1278 yylval.opcode = tokentab3[i].opcode;
1279 return tokentab3[i].token;
1282 /* See if it is a special token of length 2. */
1283 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1284 if (STREQN (tokstart, tokentab2[i].operator, 2))
1287 yylval.opcode = tokentab2[i].opcode;
1288 return tokentab2[i].token;
1291 switch (tokchr = *tokstart)
1303 /* We either have a character constant ('0' or '\177' for
1304 example) or we have a quoted symbol reference ('foo(int,int)'
1305 in C++ for example). */
1309 c = parse_escape (&lexptr);
1311 error ("Empty character constant.");
1313 yylval.typed_val_int.val = c;
1314 yylval.typed_val_int.type = builtin_type_char;
1319 namelen = skip_quoted (tokstart,
1320 get_gdb_completer_word_break_characters())
1324 lexptr = tokstart + namelen;
1325 if (lexptr[-1] != '\'')
1326 error ("Unmatched single quote.");
1331 error ("Invalid character constant.");
1341 if (paren_depth == 0)
1348 if (comma_terminates && paren_depth == 0)
1354 /* Might be a floating point number. */
1355 if (lexptr[1] < '0' || lexptr[1] > '9')
1356 goto symbol; /* Nope, must be a symbol. */
1357 /* FALL THRU into number case. */
1370 /* It's a number. */
1371 int got_dot = 0, got_e = 0, toktype = FLOAT;
1372 /* Initialize toktype to anything other than ERROR. */
1373 register char *p = tokstart;
1374 int hex = input_radix > 10;
1375 int local_radix = input_radix;
1376 if (tokchr == '0' && (p[1] == 'x' || p[1] == 'X'))
1382 else if (tokchr == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1391 /* This test includes !hex because 'e' is a valid hex digit
1392 and thus does not indicate a floating point number when
1393 the radix is hex. */
1395 if (!hex && (*p == 'e' || *p == 'E'))
1397 toktype = ERROR; /* Only one 'e' in a float. */
1400 /* This test does not include !hex, because a '.' always
1401 indicates a decimal floating point number regardless of
1405 toktype = ERROR; /* Only one '.' in a float. */
1408 else if (got_e && (p[-1] == 'e' || p[-1] == 'E') &&
1409 (*p == '-' || *p == '+'))
1410 /* This is the sign of the exponent, not the end of the
1413 /* Always take decimal digits; parse_number handles radix
1415 else if (*p >= '0' && *p <= '9')
1417 /* We will take letters only if hex is true, and only up
1418 to what the input radix would permit. FSF was content
1419 to rely on parse_number to validate; but it leaks. */
1420 else if (*p >= 'a' && *p <= 'z')
1422 if (!hex || *p >= ('a' + local_radix - 10))
1425 else if (*p >= 'A' && *p <= 'Z')
1427 if (!hex || *p >= ('A' + local_radix - 10))
1432 if (toktype != ERROR)
1433 toktype = parse_number (tokstart, p - tokstart,
1434 got_dot | got_e, &yylval);
1435 if (toktype == ERROR)
1437 char *err_copy = (char *) alloca (p - tokstart + 1);
1439 memcpy (err_copy, tokstart, p - tokstart);
1440 err_copy[p - tokstart] = 0;
1441 error ("Invalid number \"%s\".", err_copy);
1458 case '@': /* Moved out below. */
1474 if (strncmp(tokstart, "@selector", 9) == 0)
1476 tokptr = strchr(tokstart, '(');
1479 error ("Missing '(' in @selector(...)");
1482 tokptr++; /* Skip the '('. */
1484 /* Grow the static temp buffer if necessary, including
1485 allocating the first one on demand. */
1486 if (tempbufindex + 1 >= tempbufsize)
1488 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1490 tempbuf[tempbufindex++] = *tokptr++;
1491 } while ((*tokptr != ')') && (*tokptr != '\0'));
1492 if (*tokptr++ != ')')
1494 error ("Missing ')' in @selector(...)");
1496 tempbuf[tempbufindex] = '\0';
1497 yylval.sval.ptr = tempbuf;
1498 yylval.sval.length = tempbufindex;
1502 if (tokstart[1] != '"')
1507 /* ObjC NextStep NSString constant: fall thru and parse like
1513 /* Build the gdb internal form of the input string in tempbuf,
1514 translating any standard C escape forms seen. Note that the
1515 buffer is null byte terminated *only* for the convenience of
1516 debugging gdb itself and printing the buffer contents when
1517 the buffer contains no embedded nulls. Gdb does not depend
1518 upon the buffer being null byte terminated, it uses the
1519 length string instead. This allows gdb to handle C strings
1520 (as well as strings in other languages) with embedded null
1523 tokptr = ++tokstart;
1527 /* Grow the static temp buffer if necessary, including
1528 allocating the first one on demand. */
1529 if (tempbufindex + 1 >= tempbufsize)
1531 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1537 /* Do nothing, loop will terminate. */
1541 c = parse_escape (&tokptr);
1546 tempbuf[tempbufindex++] = c;
1549 tempbuf[tempbufindex++] = *tokptr++;
1552 } while ((*tokptr != '"') && (*tokptr != '\0'));
1553 if (*tokptr++ != '"')
1555 error ("Unterminated string in expression.");
1557 tempbuf[tempbufindex] = '\0'; /* See note above. */
1558 yylval.sval.ptr = tempbuf;
1559 yylval.sval.length = tempbufindex;
1561 return (tokchr == '@' ? NSSTRING : STRING);
1564 if (!(tokchr == '_' || tokchr == '$' ||
1565 (tokchr >= 'a' && tokchr <= 'z') || (tokchr >= 'A' && tokchr <= 'Z')))
1566 /* We must have come across a bad character (e.g. ';'). */
1567 error ("Invalid character '%c' in expression.", c);
1569 /* It's a name. See how long it is. */
1571 for (c = tokstart[namelen];
1572 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1573 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1578 while (tokstart[++i] && tokstart[i] != '>');
1579 if (tokstart[i] == '>')
1582 c = tokstart[++namelen];
1585 /* The token "if" terminates the expression and is NOT
1586 removed from the input stream. */
1587 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1596 /* Catch specific keywords. Should be done with a data structure. */
1600 if (STREQN (tokstart, "unsigned", 8))
1602 if (current_language->la_language == language_cplus
1603 && STREQN (tokstart, "template", 8))
1605 if (STREQN (tokstart, "volatile", 8))
1606 return VOLATILE_KEYWORD;
1609 if (STREQN (tokstart, "struct", 6))
1611 if (STREQN (tokstart, "signed", 6))
1612 return SIGNED_KEYWORD;
1613 if (STREQN (tokstart, "sizeof", 6))
1615 if (STREQN (tokstart, "double", 6))
1616 return DOUBLE_KEYWORD;
1619 if ((current_language->la_language == language_cplus)
1620 && STREQN (tokstart, "class", 5))
1622 if (STREQN (tokstart, "union", 5))
1624 if (STREQN (tokstart, "short", 5))
1626 if (STREQN (tokstart, "const", 5))
1627 return CONST_KEYWORD;
1630 if (STREQN (tokstart, "enum", 4))
1632 if (STREQN (tokstart, "long", 4))
1634 if (current_language->la_language == language_cplus
1635 && STREQN (tokstart, "this", 4))
1637 static const char this_name[] = {
1638 CPLUS_MARKER, 't', 'h', 'i', 's', '\0'
1641 if (lookup_symbol (this_name, expression_context_block,
1642 VAR_NAMESPACE, (int *) NULL,
1643 (struct symtab **) NULL))
1648 if (STREQN (tokstart, "int", 3))
1655 yylval.sval.ptr = tokstart;
1656 yylval.sval.length = namelen;
1658 if (*tokstart == '$')
1660 write_dollar_variable (yylval.sval);
1664 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1665 functions or symtabs. If this is not so, then ...
1666 Use token-type TYPENAME for symbols that happen to be defined
1667 currently as names of types; NAME for other symbols.
1668 The caller is not constrained to care about the distinction. */
1670 char *tmp = copy_name (yylval.sval);
1672 int is_a_field_of_this = 0, *need_this;
1675 if (current_language->la_language == language_cplus ||
1676 current_language->la_language == language_objc)
1677 need_this = &is_a_field_of_this;
1679 need_this = (int *) NULL;
1681 sym = lookup_symbol (tmp, expression_context_block,
1684 (struct symtab **) NULL);
1685 /* Call lookup_symtab, not lookup_partial_symtab, in case there
1686 are no psymtabs (coff, xcoff, or some future change to blow
1687 away the psymtabs once symbols are read). */
1688 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1689 lookup_symtab (tmp))
1691 yylval.ssym.sym = sym;
1692 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1695 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1698 /* Despite the following flaw, we need to keep this code
1699 enabled. Because we can get called from
1700 check_stub_method, if we don't handle nested types then
1701 it screws many operations in any program which uses
1703 /* In "A::x", if x is a member function of A and there
1704 happens to be a type (nested or not, since the stabs
1705 don't make that distinction) named x, then this code
1706 incorrectly thinks we are dealing with nested types
1707 rather than a member function. */
1711 struct symbol *best_sym;
1713 /* Look ahead to detect nested types. This probably should
1714 be done in the grammar, but trying seemed to introduce a
1715 lot of shift/reduce and reduce/reduce conflicts. It's
1716 possible that it could be done, though. Or perhaps a
1717 non-grammar, but less ad hoc, approach would work well. */
1719 /* Since we do not currently have any way of distinguishing
1720 a nested type from a non-nested one (the stabs don't tell
1721 us whether a type is nested), we just ignore the
1728 /* Skip whitespace. */
1729 while (*p == ' ' || *p == '\t' || *p == '\n')
1731 if (*p == ':' && p[1] == ':')
1733 /* Skip the `::'. */
1735 /* Skip whitespace. */
1736 while (*p == ' ' || *p == '\t' || *p == '\n')
1739 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1740 || (*p >= 'a' && *p <= 'z')
1741 || (*p >= 'A' && *p <= 'Z'))
1745 struct symbol *cur_sym;
1746 /* As big as the whole rest of the expression,
1747 which is at least big enough. */
1748 char *ncopy = alloca (strlen (tmp) +
1749 strlen (namestart) + 3);
1753 memcpy (tmp1, tmp, strlen (tmp));
1754 tmp1 += strlen (tmp);
1755 memcpy (tmp1, "::", 2);
1757 memcpy (tmp1, namestart, p - namestart);
1758 tmp1[p - namestart] = '\0';
1759 cur_sym = lookup_symbol (ncopy,
1760 expression_context_block,
1761 VAR_NAMESPACE, (int *) NULL,
1762 (struct symtab **) NULL);
1765 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1783 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1785 yylval.tsym.type = SYMBOL_TYPE (sym);
1789 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1792 /* See if it's an ObjC classname. */
1795 CORE_ADDR Class = lookup_objc_class(tmp);
1798 extern struct symbol *lookup_struct_typedef();
1799 yylval.class.class = Class;
1800 if ((sym = lookup_struct_typedef (tmp,
1801 expression_context_block,
1803 yylval.class.type = SYMBOL_TYPE (sym);
1808 /* Input names that aren't symbols but ARE valid hex numbers,
1809 when the input radix permits them, can be names or numbers
1810 depending on the parse. Note we support radixes > 16 here. */
1812 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1813 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1815 YYSTYPE newlval; /* Its value is ignored. */
1816 hextype = parse_number (tokstart, namelen, 0, &newlval);
1819 yylval.ssym.sym = sym;
1820 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1825 /* Any other kind of symbol. */
1826 yylval.ssym.sym = sym;
1827 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1836 if (*lexptr == '\0')
1837 error("A %s near end of expression.", (msg ? msg : "error"));
1839 error ("A %s in expression, near `%s'.", (msg ? msg : "error"),