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,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
30 Note that malloc's and realloc's in this file are transformed to
31 xmalloc and xrealloc respectively by the same sed command in the
32 makefile that remaps any other malloc/realloc inserted by the parser
33 generator. Doing this with #defines and trying to control the interaction
34 with include files (<malloc.h> and <stdlib.h> for example) just became
35 too messy, particularly when such includes can be inserted at random
36 times by the parser generator. */
41 #include "gdb_string.h"
43 #include "expression.h"
45 #include "objc-lang.h" /* for objc language constructs */
48 #include "parser-defs.h"
51 #include "bfd.h" /* Required by objfiles.h. */
52 #include "symfile.h" /* Required by objfiles.h. */
53 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
55 #include "completer.h" /* For skip_quoted(). */
57 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
58 as well as gratuitiously global symbol names, so we can have multiple
59 yacc generated parsers in gdb. Note that these are only the variables
60 produced by yacc. If other parser generators (bison, byacc, etc) produce
61 additional global names that conflict at link time, then those parser
62 generators need to be fixed instead of 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,
171 and both convey their data as strings.
172 But a TYPENAME is a string that happens to be defined as a typedef
173 or builtin type name (such as int or char)
174 and a NAME is any other symbol.
175 Contexts where this distinction is not important can use the
176 nonterminal "name", which 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 different
201 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
203 %token <voidval> VARIABLE
205 %token <opcode> ASSIGN_MODIFY
212 %right '=' ASSIGN_MODIFY
220 %left '<' '>' LEQ GEQ
225 %right UNARY INCREMENT DECREMENT
226 %right ARROW '.' '[' '('
227 %token <ssym> BLOCKNAME
239 { write_exp_elt_opcode(OP_TYPE);
240 write_exp_elt_type($1);
241 write_exp_elt_opcode(OP_TYPE);}
244 /* Expressions, including the comma operator. */
247 { write_exp_elt_opcode (BINOP_COMMA); }
250 /* Expressions, not including the comma operator. */
251 exp : '*' exp %prec UNARY
252 { write_exp_elt_opcode (UNOP_IND); }
254 exp : '&' exp %prec UNARY
255 { write_exp_elt_opcode (UNOP_ADDR); }
257 exp : '-' exp %prec UNARY
258 { write_exp_elt_opcode (UNOP_NEG); }
261 exp : '!' exp %prec UNARY
262 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
265 exp : '~' exp %prec UNARY
266 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
269 exp : INCREMENT exp %prec UNARY
270 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
273 exp : DECREMENT exp %prec UNARY
274 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
277 exp : exp INCREMENT %prec UNARY
278 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
281 exp : exp DECREMENT %prec UNARY
282 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
285 exp : SIZEOF exp %prec UNARY
286 { write_exp_elt_opcode (UNOP_SIZEOF); }
290 { write_exp_elt_opcode (STRUCTOP_PTR);
291 write_exp_string ($3);
292 write_exp_elt_opcode (STRUCTOP_PTR); }
295 exp : exp ARROW qualified_name
296 { /* exp->type::name becomes exp->*(&type::name) */
297 /* Note: this doesn't work if name is a
298 static member! FIXME */
299 write_exp_elt_opcode (UNOP_ADDR);
300 write_exp_elt_opcode (STRUCTOP_MPTR); }
302 exp : exp ARROW '*' exp
303 { write_exp_elt_opcode (STRUCTOP_MPTR); }
307 { write_exp_elt_opcode (STRUCTOP_STRUCT);
308 write_exp_string ($3);
309 write_exp_elt_opcode (STRUCTOP_STRUCT); }
313 exp : exp '.' qualified_name
314 { /* exp.type::name becomes exp.*(&type::name) */
315 /* Note: this doesn't work if name is a
316 static member! FIXME */
317 write_exp_elt_opcode (UNOP_ADDR);
318 write_exp_elt_opcode (STRUCTOP_MEMBER); }
321 exp : exp '.' '*' exp
322 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
325 exp : exp '[' exp1 ']'
326 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
329 * The rules below parse ObjC message calls of the form:
330 * '[' target selector {':' argument}* ']'
337 class = lookup_objc_class (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 (builtin_type_int);
343 write_exp_elt_longcst ((LONGEST) class);
344 write_exp_elt_opcode (OP_LONG);
348 { write_exp_elt_opcode (OP_MSGCALL);
350 write_exp_elt_opcode (OP_MSGCALL);
356 write_exp_elt_opcode (OP_LONG);
357 write_exp_elt_type (builtin_type_int);
358 write_exp_elt_longcst ((LONGEST) $2.class);
359 write_exp_elt_opcode (OP_LONG);
363 { write_exp_elt_opcode (OP_MSGCALL);
365 write_exp_elt_opcode (OP_MSGCALL);
372 { write_exp_elt_opcode (OP_MSGCALL);
374 write_exp_elt_opcode (OP_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_SELECTOR);
573 write_exp_string ($1);
574 write_exp_elt_opcode (OP_SELECTOR); }
576 exp : SIZEOF '(' type ')' %prec UNARY
577 { write_exp_elt_opcode (OP_LONG);
578 write_exp_elt_type (builtin_type_int);
580 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
581 write_exp_elt_opcode (OP_LONG); }
585 { /* C strings are converted into array constants with
586 an explicit null byte added at the end. Thus
587 the array upper bound is the string length.
588 There is no 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 qualifier
802 before the type is currently handled in the typebase rule.
803 The reason for recognizing these here (shift/reduce conflicts)
804 might be obsolete now that some pointer to member rules have
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 never
861 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 qualifier
934 after the type is handled in the ptype rule. I think these could
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 just
984 a fake for "variable", so these cause reduce/reduce conflicts because
985 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
986 =exp) or just an exp. If name_not_typename was ever used in an lvalue
987 context where only a 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.
996 LEN is the 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 values
1008 here, and we do kind of silly things like cast to unsigned. */
1009 register LONGEST n = 0;
1010 register LONGEST prevn = 0;
1011 unsigned LONGEST un;
1015 register int base = input_radix;
1018 /* Number of "L" suffixes encountered. */
1021 /* We have found a "L" or "U" suffix. */
1022 int found_suffix = 0;
1024 unsigned LONGEST high_bit;
1025 struct type *signed_type;
1026 struct type *unsigned_type;
1032 /* It's a float since it contains a point or an exponent. */
1034 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
1035 sscanf (p, "%g", &putithere->typed_val_float.dval);
1036 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
1037 sscanf (p, "%lg", &putithere->typed_val_float.dval);
1040 #ifdef PRINTF_HAS_LONG_DOUBLE
1041 sscanf (p, "%Lg", &putithere->typed_val_float.dval);
1043 /* Scan it into a double, then assign it to the long double.
1044 This at least wins with values representable in the range
1047 sscanf (p, "%lg", &temp);
1048 putithere->typed_val_float.dval = temp;
1052 /* See if it has `f' or `l' suffix (float or long double). */
1054 c = tolower (p[len - 1]);
1057 putithere->typed_val_float.type = builtin_type_float;
1059 putithere->typed_val_float.type = builtin_type_long_double;
1060 else if (isdigit (c) || c == '.')
1061 putithere->typed_val_float.type = builtin_type_double;
1068 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1102 if (c >= 'A' && c <= 'Z')
1104 if (c != 'l' && c != 'u')
1106 if (c >= '0' && c <= '9')
1114 if (base > 10 && c >= 'a' && c <= 'f')
1118 n += i = c - 'a' + 10;
1131 return ERROR; /* Char not a digit */
1134 return ERROR; /* Invalid digit in this base */
1136 /* Portably test for overflow (only works for nonzero values, so make
1137 a second check for zero). FIXME: Can't we just make n and prevn
1138 unsigned and avoid this? */
1139 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1140 unsigned_p = 1; /* Try something unsigned */
1142 /* Portably test for unsigned overflow.
1143 FIXME: This check is wrong; for example it doesn't find overflow
1144 on 0x123456789 when LONGEST is 32 bits. */
1145 if (c != 'l' && c != 'u' && n != 0)
1147 if ((unsigned_p && (unsigned LONGEST) prevn >= (unsigned LONGEST) n))
1148 error ("Numeric constant too large.");
1153 /* An integer constant is an int, a long, or a long long. An L
1154 suffix forces it to be long; an LL suffix forces it to be long
1155 long. If not forced to a larger size, it gets the first type of
1156 the above that it fits in. To figure out whether it fits, we
1157 shift it right and see whether anything remains. Note that we
1158 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1159 operation, because many compilers will warn about such a shift
1160 (which always produces a zero result). Sometimes TARGET_INT_BIT
1161 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1162 the case where it is we just always shift the value more than
1163 once, with fewer bits each time. */
1165 un = (unsigned LONGEST)n >> 2;
1167 && (un >> (TARGET_INT_BIT - 2)) == 0)
1169 high_bit = ((unsigned LONGEST)1) << (TARGET_INT_BIT-1);
1171 /* A large decimal (not hex or octal) constant (between INT_MAX
1172 and UINT_MAX) is a long or unsigned long, according to ANSI,
1173 never an unsigned int, but this code treats it as unsigned
1174 int. This probably should be fixed. GCC gives a warning on
1177 unsigned_type = builtin_type_unsigned_int;
1178 signed_type = builtin_type_int;
1180 else if (long_p <= 1
1181 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1183 high_bit = ((unsigned LONGEST)1) << (TARGET_LONG_BIT-1);
1184 unsigned_type = builtin_type_unsigned_long;
1185 signed_type = builtin_type_long;
1189 high_bit = (((unsigned LONGEST)1)
1190 << (TARGET_LONG_LONG_BIT - 32 - 1)
1194 /* A long long does not fit in a LONGEST. */
1196 (unsigned LONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
1197 unsigned_type = builtin_type_unsigned_long_long;
1198 signed_type = builtin_type_long_long;
1201 putithere->typed_val_int.val = n;
1203 /* If the high bit of the worked out type is set then this number
1204 has to be unsigned. */
1206 if (unsigned_p || (n & high_bit))
1208 putithere->typed_val_int.type = unsigned_type;
1212 putithere->typed_val_int.type = signed_type;
1222 enum exp_opcode opcode;
1225 static const struct token tokentab3[] =
1227 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1228 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1231 static const struct token tokentab2[] =
1233 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1234 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1235 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1236 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1237 {"%=", ASSIGN_MODIFY, BINOP_REM},
1238 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1239 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1240 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1241 {"++", INCREMENT, BINOP_END},
1242 {"--", DECREMENT, BINOP_END},
1243 {"->", ARROW, BINOP_END},
1244 {"&&", ANDAND, BINOP_END},
1245 {"||", OROR, BINOP_END},
1246 {"::", COLONCOLON, BINOP_END},
1247 {"<<", LSH, BINOP_END},
1248 {">>", RSH, BINOP_END},
1249 {"==", EQUAL, BINOP_END},
1250 {"!=", NOTEQUAL, BINOP_END},
1251 {"<=", LEQ, BINOP_END},
1252 {">=", GEQ, BINOP_END}
1255 /* Read one token, getting characters through lexptr. */
1266 static char *tempbuf;
1267 static int tempbufsize;
1272 /* See if it is a special token of length 3. */
1273 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1274 if (STREQN (tokstart, tokentab3[i].operator, 3))
1277 yylval.opcode = tokentab3[i].opcode;
1278 return tokentab3[i].token;
1281 /* See if it is a special token of length 2. */
1282 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1283 if (STREQN (tokstart, tokentab2[i].operator, 2))
1286 yylval.opcode = tokentab2[i].opcode;
1287 return tokentab2[i].token;
1290 switch (tokchr = *tokstart)
1302 /* We either have a character constant ('0' or '\177' for example)
1303 or we have a quoted symbol reference ('foo(int,int)' in C++
1308 c = parse_escape (&lexptr);
1310 error ("Empty character constant.");
1312 yylval.typed_val_int.val = c;
1313 yylval.typed_val_int.type = builtin_type_char;
1318 namelen = skip_quoted (tokstart,
1319 get_gdb_completer_word_break_characters())
1323 lexptr = tokstart + namelen;
1324 if (lexptr[-1] != '\'')
1325 error ("Unmatched single quote.");
1330 error ("Invalid character constant.");
1340 if (paren_depth == 0)
1347 if (comma_terminates && paren_depth == 0)
1353 /* Might be a floating point number. */
1354 if (lexptr[1] < '0' || lexptr[1] > '9')
1355 goto symbol; /* Nope, must be a symbol. */
1356 /* FALL THRU into number case. */
1369 /* It's a number. */
1370 int got_dot = 0, got_e = 0, toktype = FLOAT;
1371 /* initialize toktype to anything other than ERROR. */
1372 register char *p = tokstart;
1373 int hex = input_radix > 10;
1374 int local_radix = input_radix;
1375 if (tokchr == '0' && (p[1] == 'x' || p[1] == 'X'))
1381 else if (tokchr == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1390 /* This test includes !hex because 'e' is a valid hex digit
1391 and thus does not indicate a floating point number when
1392 the radix is hex. */
1394 if (!hex && (*p == 'e' || *p == 'E'))
1396 toktype = ERROR; /* only one 'e' in a float */
1399 /* This test does not include !hex, because a '.' always indicates
1400 a decimal floating point number regardless of the radix. */
1403 toktype = ERROR; /* only one '.' in a float */
1406 else if (got_e && (p[-1] == 'e' || p[-1] == 'E') &&
1407 (*p == '-' || *p == '+'))
1408 /* This is the sign of the exponent, not the end of the
1411 /* Always take decimal digits; parse_number handles radix error */
1412 else if (*p >= '0' && *p <= '9')
1414 /* We will take letters only if hex is true, and only
1415 up to what the input radix would permit. FSF was content
1416 to rely on parse_number to validate; but it leaks. */
1417 else if (*p >= 'a' && *p <= 'z') {
1418 if (!hex || *p >= ('a' + local_radix - 10))
1421 else if (*p >= 'A' && *p <= 'Z') {
1422 if (!hex || *p >= ('A' + local_radix - 10))
1427 if (toktype != ERROR)
1428 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1429 if (toktype == ERROR)
1431 char *err_copy = (char *) alloca (p - tokstart + 1);
1433 memcpy (err_copy, tokstart, p - tokstart);
1434 err_copy[p - tokstart] = 0;
1435 error ("Invalid number \"%s\".", err_copy);
1452 case '@': /* moved out below */
1468 if (strncmp(tokstart, "@selector", 9) == 0)
1470 tokptr = strchr(tokstart, '(');
1473 error ("Missing '(' in @selector(...)");
1476 tokptr++; /* skip the '(' */
1478 /* Grow the static temp buffer if necessary, including allocating
1479 the first one on demand. */
1480 if (tempbufindex + 1 >= tempbufsize)
1482 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1484 tempbuf[tempbufindex++] = *tokptr++;
1485 } while ((*tokptr != ')') && (*tokptr != '\0'));
1486 if (*tokptr++ != ')')
1488 error ("Missing ')' in @selector(...)");
1490 tempbuf[tempbufindex] = '\0';
1491 yylval.sval.ptr = tempbuf;
1492 yylval.sval.length = tempbufindex;
1496 if (tokstart[1] != '"')
1501 /* ObjC NextStep NSString constant: fall thru and parse like STRING */
1506 /* Build the gdb internal form of the input string in tempbuf,
1507 translating any standard C escape forms seen. Note that the
1508 buffer is null byte terminated *only* for the convenience of
1509 debugging gdb itself and printing the buffer contents when
1510 the buffer contains no embedded nulls. Gdb does not depend
1511 upon the buffer being null byte terminated, it uses the length
1512 string instead. This allows gdb to handle C strings (as well
1513 as strings in other languages) with embedded null bytes */
1515 tokptr = ++tokstart;
1519 /* Grow the static temp buffer if necessary, including allocating
1520 the first one on demand. */
1521 if (tempbufindex + 1 >= tempbufsize)
1523 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1529 /* Do nothing, loop will terminate. */
1533 c = parse_escape (&tokptr);
1538 tempbuf[tempbufindex++] = c;
1541 tempbuf[tempbufindex++] = *tokptr++;
1544 } while ((*tokptr != '"') && (*tokptr != '\0'));
1545 if (*tokptr++ != '"')
1547 error ("Unterminated string in expression.");
1549 tempbuf[tempbufindex] = '\0'; /* See note above */
1550 yylval.sval.ptr = tempbuf;
1551 yylval.sval.length = tempbufindex;
1553 return (tokchr == '@' ? NSSTRING : STRING);
1556 if (!(tokchr == '_' || tokchr == '$' ||
1557 (tokchr >= 'a' && tokchr <= 'z') || (tokchr >= 'A' && tokchr <= 'Z')))
1558 /* We must have come across a bad character (e.g. ';'). */
1559 error ("Invalid character '%c' in expression.", c);
1561 /* It's a name. See how long it is. */
1563 for (c = tokstart[namelen];
1564 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1565 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1570 while (tokstart[++i] && tokstart[i] != '>');
1571 if (tokstart[i] == '>')
1574 c = tokstart[++namelen];
1577 /* The token "if" terminates the expression and is NOT
1578 removed from the input stream. */
1579 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1588 /* Catch specific keywords. Should be done with a data structure. */
1592 if (STREQN (tokstart, "unsigned", 8))
1594 if (current_language->la_language == language_cplus
1595 && STREQN (tokstart, "template", 8))
1597 if (STREQN (tokstart, "volatile", 8))
1598 return VOLATILE_KEYWORD;
1601 if (STREQN (tokstart, "struct", 6))
1603 if (STREQN (tokstart, "signed", 6))
1604 return SIGNED_KEYWORD;
1605 if (STREQN (tokstart, "sizeof", 6))
1607 if (STREQN (tokstart, "double", 6))
1608 return DOUBLE_KEYWORD;
1611 if ((current_language->la_language == language_cplus)
1612 && STREQN (tokstart, "class", 5))
1614 if (STREQN (tokstart, "union", 5))
1616 if (STREQN (tokstart, "short", 5))
1618 if (STREQN (tokstart, "const", 5))
1619 return CONST_KEYWORD;
1622 if (STREQN (tokstart, "enum", 4))
1624 if (STREQN (tokstart, "long", 4))
1626 if (current_language->la_language == language_cplus
1627 && STREQN (tokstart, "this", 4))
1629 static const char this_name[] =
1630 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1632 if (lookup_symbol (this_name, expression_context_block,
1633 VAR_NAMESPACE, (int *) NULL,
1634 (struct symtab **) NULL))
1639 if (STREQN (tokstart, "int", 3))
1646 yylval.sval.ptr = tokstart;
1647 yylval.sval.length = namelen;
1649 if (*tokstart == '$')
1651 write_dollar_variable (yylval.sval);
1655 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1656 functions or symtabs. If this is not so, then ...
1657 Use token-type TYPENAME for symbols that happen to be defined
1658 currently as names of types; NAME for other symbols.
1659 The caller is not constrained to care about the distinction. */
1661 char *tmp = copy_name (yylval.sval);
1663 int is_a_field_of_this = 0, *need_this;
1666 if (current_language->la_language == language_cplus ||
1667 current_language->la_language == language_objc)
1668 need_this = &is_a_field_of_this;
1670 need_this = (int *) NULL;
1672 sym = lookup_symbol (tmp, expression_context_block,
1675 (struct symtab **) NULL);
1676 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1677 no psymtabs (coff, xcoff, or some future change to blow away the
1678 psymtabs once symbols are read). */
1679 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1680 lookup_symtab (tmp))
1682 yylval.ssym.sym = sym;
1683 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1686 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1689 /* Despite the following flaw, we need to keep this code enabled.
1690 Because we can get called from check_stub_method, if we don't
1691 handle nested types then it screws many operations in any
1692 program which uses nested types. */
1693 /* In "A::x", if x is a member function of A and there happens
1694 to be a type (nested or not, since the stabs don't make that
1695 distinction) named x, then this code incorrectly thinks we
1696 are dealing with nested types rather than a member function. */
1700 struct symbol *best_sym;
1702 /* Look ahead to detect nested types. This probably should be
1703 done in the grammar, but trying seemed to introduce a lot
1704 of shift/reduce and reduce/reduce conflicts. It's possible
1705 that it could be done, though. Or perhaps a non-grammar, but
1706 less ad hoc, approach would work well. */
1708 /* Since we do not currently have any way of distinguishing
1709 a nested type from a non-nested one (the stabs don't tell
1710 us whether a type is nested), we just ignore the
1717 /* Skip whitespace. */
1718 while (*p == ' ' || *p == '\t' || *p == '\n')
1720 if (*p == ':' && p[1] == ':')
1722 /* Skip the `::'. */
1724 /* Skip whitespace. */
1725 while (*p == ' ' || *p == '\t' || *p == '\n')
1728 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1729 || (*p >= 'a' && *p <= 'z')
1730 || (*p >= 'A' && *p <= 'Z'))
1734 struct symbol *cur_sym;
1735 /* As big as the whole rest of the expression, which is
1736 at least big enough. */
1737 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1741 memcpy (tmp1, tmp, strlen (tmp));
1742 tmp1 += strlen (tmp);
1743 memcpy (tmp1, "::", 2);
1745 memcpy (tmp1, namestart, p - namestart);
1746 tmp1[p - namestart] = '\0';
1747 cur_sym = lookup_symbol (ncopy, expression_context_block,
1748 VAR_NAMESPACE, (int *) NULL,
1749 (struct symtab **) NULL);
1752 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1770 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1772 yylval.tsym.type = SYMBOL_TYPE (sym);
1776 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1779 if (!sym) /* see if it's an ObjC classname */
1781 CORE_ADDR Class = lookup_objc_class(tmp);
1784 extern struct symbol *lookup_struct_typedef();
1785 yylval.class.class = Class;
1786 if ((sym = lookup_struct_typedef (tmp, expression_context_block, 1)))
1787 yylval.class.type = SYMBOL_TYPE (sym);
1792 /* Input names that aren't symbols but ARE valid hex numbers,
1793 when the input radix permits them, can be names or numbers
1794 depending on the parse. Note we support radixes > 16 here. */
1796 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1797 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1799 YYSTYPE newlval; /* Its value is ignored. */
1800 hextype = parse_number (tokstart, namelen, 0, &newlval);
1803 yylval.ssym.sym = sym;
1804 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1809 /* Any other kind of symbol */
1810 yylval.ssym.sym = sym;
1811 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1820 if (*lexptr == '\0')
1821 error("A %s near end of expression.", (msg ? msg : "error"));
1823 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);