1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2017 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 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,
20 and return the result as a struct expression pointer.
21 That structure contains arithmetic operations in reverse polish,
22 with constants represented by operations that are followed by special data.
23 See expression.h for the details of the format.
24 What is important here is that it can be built up sequentially
25 during the process of parsing; the lower levels of the tree always
26 come first in the result.
28 Note that malloc's and realloc's in this file are transformed to
29 xmalloc and xrealloc respectively by the same sed command in the
30 makefile that remaps any other malloc/realloc inserted by the parser
31 generator. Doing this with #defines and trying to control the interaction
32 with include files (<malloc.h> and <stdlib.h> for example) just became
33 too messy, particularly when such includes can be inserted at random
34 times by the parser generator. */
40 #include "expression.h"
42 #include "parser-defs.h"
45 #include "bfd.h" /* Required by objfiles.h. */
46 #include "symfile.h" /* Required by objfiles.h. */
47 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
50 #include "cp-support.h"
52 #include "macroscope.h"
53 #include "objc-lang.h"
54 #include "typeprint.h"
57 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
61 #define GDB_YY_REMAP_PREFIX c_
64 /* The state of the parser, used internally when we are parsing the
67 static struct parser_state *pstate = NULL;
71 static int yylex (void);
73 void yyerror (const char *);
75 static int type_aggregate_p (struct type *);
79 /* Although the yacc "value" of an expression is not used,
80 since the result is stored in the structure being created,
81 other node types do have values. */
100 struct typed_stoken tsval;
102 struct symtoken ssym;
104 const struct block *bval;
105 enum exp_opcode opcode;
107 struct stoken_vector svec;
108 VEC (type_ptr) *tvec;
110 struct type_stack *type_stack;
112 struct objc_class_str theclass;
116 /* YYSTYPE gets defined by %union */
117 static int parse_number (struct parser_state *par_state,
118 const char *, int, int, YYSTYPE *);
119 static struct stoken operator_stoken (const char *);
120 static void check_parameter_typelist (VEC (type_ptr) *);
121 static void write_destructor_name (struct parser_state *par_state,
125 static void c_print_token (FILE *file, int type, YYSTYPE value);
126 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
130 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
132 %type <tval> type typebase
133 %type <tvec> nonempty_typelist func_mod parameter_typelist
134 /* %type <bval> block */
136 /* Fancy type parsing. */
138 %type <lval> array_mod
139 %type <tval> conversion_type_id
141 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
143 %token <typed_val_int> INT
144 %token <typed_val_float> FLOAT
145 %token <typed_val_decfloat> DECFLOAT
147 /* Both NAME and TYPENAME tokens represent symbols in the input,
148 and both convey their data as strings.
149 But a TYPENAME is a string that happens to be defined as a typedef
150 or builtin type name (such as int or char)
151 and a NAME is any other symbol.
152 Contexts where this distinction is not important can use the
153 nonterminal "name", which matches either NAME or TYPENAME. */
155 %token <tsval> STRING
156 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
157 %token SELECTOR /* ObjC "@selector" pseudo-operator */
159 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
160 %token <ssym> UNKNOWN_CPP_NAME
161 %token <voidval> COMPLETE
162 %token <tsym> TYPENAME
163 %token <theclass> CLASSNAME /* ObjC Class name */
165 %type <svec> string_exp
166 %type <ssym> name_not_typename
167 %type <tsym> type_name
169 /* This is like a '[' token, but is only generated when parsing
170 Objective C. This lets us reuse the same parser without
171 erroneously parsing ObjC-specific expressions in C. */
174 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
175 but which would parse as a valid number in the current input radix.
176 E.g. "c" when input_radix==16. Depending on the parse, it will be
177 turned into a name or into a number. */
179 %token <ssym> NAME_OR_INT
182 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
187 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
193 /* Special type cases, put in to allow the parser to distinguish different
195 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
197 %token <sval> VARIABLE
199 %token <opcode> ASSIGN_MODIFY
208 %right '=' ASSIGN_MODIFY
216 %left '<' '>' LEQ GEQ
221 %right UNARY INCREMENT DECREMENT
222 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
223 %token <ssym> BLOCKNAME
224 %token <bval> FILENAME
238 { write_exp_elt_opcode(pstate, OP_TYPE);
239 write_exp_elt_type(pstate, $1);
240 write_exp_elt_opcode(pstate, OP_TYPE);}
243 write_exp_elt_opcode (pstate, OP_TYPEOF);
245 | TYPEOF '(' type ')'
247 write_exp_elt_opcode (pstate, OP_TYPE);
248 write_exp_elt_type (pstate, $3);
249 write_exp_elt_opcode (pstate, OP_TYPE);
251 | DECLTYPE '(' exp ')'
253 write_exp_elt_opcode (pstate, OP_DECLTYPE);
257 /* Expressions, including the comma operator. */
260 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
263 /* Expressions, not including the comma operator. */
264 exp : '*' exp %prec UNARY
265 { write_exp_elt_opcode (pstate, UNOP_IND); }
268 exp : '&' exp %prec UNARY
269 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
272 exp : '-' exp %prec UNARY
273 { write_exp_elt_opcode (pstate, UNOP_NEG); }
276 exp : '+' exp %prec UNARY
277 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
280 exp : '!' exp %prec UNARY
281 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
284 exp : '~' exp %prec UNARY
285 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
288 exp : INCREMENT exp %prec UNARY
289 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
292 exp : DECREMENT exp %prec UNARY
293 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
296 exp : exp INCREMENT %prec UNARY
297 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
300 exp : exp DECREMENT %prec UNARY
301 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
304 exp : TYPEID '(' exp ')' %prec UNARY
305 { write_exp_elt_opcode (pstate, OP_TYPEID); }
308 exp : TYPEID '(' type_exp ')' %prec UNARY
309 { write_exp_elt_opcode (pstate, OP_TYPEID); }
312 exp : SIZEOF exp %prec UNARY
313 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
317 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
318 write_exp_string (pstate, $3);
319 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
322 exp : exp ARROW name COMPLETE
323 { mark_struct_expression (pstate);
324 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
325 write_exp_string (pstate, $3);
326 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
329 exp : exp ARROW COMPLETE
331 mark_struct_expression (pstate);
332 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
335 write_exp_string (pstate, s);
336 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
339 exp : exp ARROW '~' name
340 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
341 write_destructor_name (pstate, $4);
342 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
345 exp : exp ARROW '~' name COMPLETE
346 { mark_struct_expression (pstate);
347 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
348 write_destructor_name (pstate, $4);
349 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
352 exp : exp ARROW qualified_name
353 { /* exp->type::name becomes exp->*(&type::name) */
354 /* Note: this doesn't work if name is a
355 static member! FIXME */
356 write_exp_elt_opcode (pstate, UNOP_ADDR);
357 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
360 exp : exp ARROW_STAR exp
361 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
365 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
366 write_exp_string (pstate, $3);
367 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
370 exp : exp '.' name COMPLETE
371 { mark_struct_expression (pstate);
372 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
373 write_exp_string (pstate, $3);
374 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
377 exp : exp '.' COMPLETE
379 mark_struct_expression (pstate);
380 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
383 write_exp_string (pstate, s);
384 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
387 exp : exp '.' '~' name
388 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
389 write_destructor_name (pstate, $4);
390 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
393 exp : exp '.' '~' name COMPLETE
394 { mark_struct_expression (pstate);
395 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
396 write_destructor_name (pstate, $4);
397 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
400 exp : exp '.' qualified_name
401 { /* exp.type::name becomes exp.*(&type::name) */
402 /* Note: this doesn't work if name is a
403 static member! FIXME */
404 write_exp_elt_opcode (pstate, UNOP_ADDR);
405 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
408 exp : exp DOT_STAR exp
409 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
412 exp : exp '[' exp1 ']'
413 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
416 exp : exp OBJC_LBRAC exp1 ']'
417 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
421 * The rules below parse ObjC message calls of the form:
422 * '[' target selector {':' argument}* ']'
425 exp : OBJC_LBRAC TYPENAME
429 theclass = lookup_objc_class (parse_gdbarch (pstate),
430 copy_name ($2.stoken));
432 error (_("%s is not an ObjC Class"),
433 copy_name ($2.stoken));
434 write_exp_elt_opcode (pstate, OP_LONG);
435 write_exp_elt_type (pstate,
436 parse_type (pstate)->builtin_int);
437 write_exp_elt_longcst (pstate, (LONGEST) theclass);
438 write_exp_elt_opcode (pstate, OP_LONG);
442 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
443 end_msglist (pstate);
444 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
448 exp : OBJC_LBRAC CLASSNAME
450 write_exp_elt_opcode (pstate, OP_LONG);
451 write_exp_elt_type (pstate,
452 parse_type (pstate)->builtin_int);
453 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
454 write_exp_elt_opcode (pstate, OP_LONG);
458 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
459 end_msglist (pstate);
460 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
467 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
468 end_msglist (pstate);
469 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
474 { add_msglist(&$1, 0); }
482 msgarg : name ':' exp
483 { add_msglist(&$1, 1); }
484 | ':' exp /* Unnamed arg. */
485 { add_msglist(0, 1); }
486 | ',' exp /* Variable number of args. */
487 { add_msglist(0, 0); }
491 /* This is to save the value of arglist_len
492 being accumulated by an outer function call. */
493 { start_arglist (); }
494 arglist ')' %prec ARROW
495 { write_exp_elt_opcode (pstate, OP_FUNCALL);
496 write_exp_elt_longcst (pstate,
497 (LONGEST) end_arglist ());
498 write_exp_elt_opcode (pstate, OP_FUNCALL); }
501 exp : UNKNOWN_CPP_NAME '('
503 /* This could potentially be a an argument defined
504 lookup function (Koenig). */
505 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
506 write_exp_elt_block (pstate,
507 expression_context_block);
508 write_exp_elt_sym (pstate,
509 NULL); /* Placeholder. */
510 write_exp_string (pstate, $1.stoken);
511 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
513 /* This is to save the value of arglist_len
514 being accumulated by an outer function call. */
518 arglist ')' %prec ARROW
520 write_exp_elt_opcode (pstate, OP_FUNCALL);
521 write_exp_elt_longcst (pstate,
522 (LONGEST) end_arglist ());
523 write_exp_elt_opcode (pstate, OP_FUNCALL);
528 { start_arglist (); }
538 arglist : arglist ',' exp %prec ABOVE_COMMA
542 exp : exp '(' parameter_typelist ')' const_or_volatile
544 VEC (type_ptr) *type_list = $3;
545 struct type *type_elt;
546 LONGEST len = VEC_length (type_ptr, type_list);
548 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
549 write_exp_elt_longcst (pstate, len);
551 VEC_iterate (type_ptr, type_list, i, type_elt);
553 write_exp_elt_type (pstate, type_elt);
554 write_exp_elt_longcst(pstate, len);
555 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
556 VEC_free (type_ptr, type_list);
561 { $$ = end_arglist () - 1; }
563 exp : lcurly arglist rcurly %prec ARROW
564 { write_exp_elt_opcode (pstate, OP_ARRAY);
565 write_exp_elt_longcst (pstate, (LONGEST) 0);
566 write_exp_elt_longcst (pstate, (LONGEST) $3);
567 write_exp_elt_opcode (pstate, OP_ARRAY); }
570 exp : lcurly type_exp rcurly exp %prec UNARY
571 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
574 exp : '(' type_exp ')' exp %prec UNARY
575 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
582 /* Binary operators in order of decreasing precedence. */
585 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
589 { write_exp_elt_opcode (pstate, BINOP_MUL); }
593 { write_exp_elt_opcode (pstate, BINOP_DIV); }
597 { write_exp_elt_opcode (pstate, BINOP_REM); }
601 { write_exp_elt_opcode (pstate, BINOP_ADD); }
605 { write_exp_elt_opcode (pstate, BINOP_SUB); }
609 { write_exp_elt_opcode (pstate, BINOP_LSH); }
613 { write_exp_elt_opcode (pstate, BINOP_RSH); }
617 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
620 exp : exp NOTEQUAL exp
621 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
625 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
629 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
633 { write_exp_elt_opcode (pstate, BINOP_LESS); }
637 { write_exp_elt_opcode (pstate, BINOP_GTR); }
641 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
645 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
649 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
653 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
657 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
660 exp : exp '?' exp ':' exp %prec '?'
661 { write_exp_elt_opcode (pstate, TERNOP_COND); }
665 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
668 exp : exp ASSIGN_MODIFY exp
669 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
670 write_exp_elt_opcode (pstate, $2);
671 write_exp_elt_opcode (pstate,
672 BINOP_ASSIGN_MODIFY); }
676 { write_exp_elt_opcode (pstate, OP_LONG);
677 write_exp_elt_type (pstate, $1.type);
678 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
679 write_exp_elt_opcode (pstate, OP_LONG); }
684 struct stoken_vector vec;
687 write_exp_string_vector (pstate, $1.type, &vec);
693 parse_number (pstate, $1.stoken.ptr,
694 $1.stoken.length, 0, &val);
695 write_exp_elt_opcode (pstate, OP_LONG);
696 write_exp_elt_type (pstate, val.typed_val_int.type);
697 write_exp_elt_longcst (pstate,
698 (LONGEST) val.typed_val_int.val);
699 write_exp_elt_opcode (pstate, OP_LONG);
705 { write_exp_elt_opcode (pstate, OP_DOUBLE);
706 write_exp_elt_type (pstate, $1.type);
707 write_exp_elt_dblcst (pstate, $1.dval);
708 write_exp_elt_opcode (pstate, OP_DOUBLE); }
712 { write_exp_elt_opcode (pstate, OP_DECFLOAT);
713 write_exp_elt_type (pstate, $1.type);
714 write_exp_elt_decfloatcst (pstate, $1.val);
715 write_exp_elt_opcode (pstate, OP_DECFLOAT); }
723 write_dollar_variable (pstate, $1);
727 exp : SELECTOR '(' name ')'
729 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
730 write_exp_string (pstate, $3);
731 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
734 exp : SIZEOF '(' type ')' %prec UNARY
735 { struct type *type = $3;
736 write_exp_elt_opcode (pstate, OP_LONG);
737 write_exp_elt_type (pstate, lookup_signed_typename
738 (parse_language (pstate),
739 parse_gdbarch (pstate),
741 type = check_typedef (type);
743 /* $5.3.3/2 of the C++ Standard (n3290 draft)
744 says of sizeof: "When applied to a reference
745 or a reference type, the result is the size of
746 the referenced type." */
747 if (TYPE_IS_REFERENCE (type))
748 type = check_typedef (TYPE_TARGET_TYPE (type));
749 write_exp_elt_longcst (pstate,
750 (LONGEST) TYPE_LENGTH (type));
751 write_exp_elt_opcode (pstate, OP_LONG); }
754 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
755 { write_exp_elt_opcode (pstate,
756 UNOP_REINTERPRET_CAST); }
759 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
760 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
763 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
764 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
767 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
768 { /* We could do more error checking here, but
769 it doesn't seem worthwhile. */
770 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
776 /* We copy the string here, and not in the
777 lexer, to guarantee that we do not leak a
778 string. Note that we follow the
779 NUL-termination convention of the
781 struct typed_stoken *vec = XNEW (struct typed_stoken);
786 vec->length = $1.length;
787 vec->ptr = (char *) malloc ($1.length + 1);
788 memcpy (vec->ptr, $1.ptr, $1.length + 1);
793 /* Note that we NUL-terminate here, but just
797 $$.tokens = XRESIZEVEC (struct typed_stoken,
800 p = (char *) malloc ($2.length + 1);
801 memcpy (p, $2.ptr, $2.length + 1);
803 $$.tokens[$$.len - 1].type = $2.type;
804 $$.tokens[$$.len - 1].length = $2.length;
805 $$.tokens[$$.len - 1].ptr = p;
812 c_string_type type = C_STRING;
814 for (i = 0; i < $1.len; ++i)
816 switch ($1.tokens[i].type)
824 && type != $1.tokens[i].type)
825 error (_("Undefined string concatenation."));
826 type = (enum c_string_type_values) $1.tokens[i].type;
830 internal_error (__FILE__, __LINE__,
831 "unrecognized type in string concatenation");
835 write_exp_string_vector (pstate, type, &$1);
836 for (i = 0; i < $1.len; ++i)
837 free ($1.tokens[i].ptr);
842 exp : NSSTRING /* ObjC NextStep NSString constant
843 * of the form '@' '"' string '"'.
845 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
846 write_exp_string (pstate, $1);
847 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
852 { write_exp_elt_opcode (pstate, OP_LONG);
853 write_exp_elt_type (pstate,
854 parse_type (pstate)->builtin_bool);
855 write_exp_elt_longcst (pstate, (LONGEST) 1);
856 write_exp_elt_opcode (pstate, OP_LONG); }
860 { write_exp_elt_opcode (pstate, OP_LONG);
861 write_exp_elt_type (pstate,
862 parse_type (pstate)->builtin_bool);
863 write_exp_elt_longcst (pstate, (LONGEST) 0);
864 write_exp_elt_opcode (pstate, OP_LONG); }
872 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
874 error (_("No file or function \"%s\"."),
875 copy_name ($1.stoken));
883 block : block COLONCOLON name
885 = lookup_symbol (copy_name ($3), $1,
886 VAR_DOMAIN, NULL).symbol;
888 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
889 error (_("No function \"%s\" in specified context."),
891 $$ = SYMBOL_BLOCK_VALUE (tem); }
894 variable: name_not_typename ENTRY
895 { struct symbol *sym = $1.sym.symbol;
897 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
898 || !symbol_read_needs_frame (sym))
899 error (_("@entry can be used only for function "
900 "parameters, not for \"%s\""),
901 copy_name ($1.stoken));
903 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
904 write_exp_elt_sym (pstate, sym);
905 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
909 variable: block COLONCOLON name
910 { struct block_symbol sym
911 = lookup_symbol (copy_name ($3), $1,
915 error (_("No symbol \"%s\" in specified context."),
917 if (symbol_read_needs_frame (sym.symbol))
919 if (innermost_block == 0
920 || contained_in (sym.block,
922 innermost_block = sym.block;
925 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
926 write_exp_elt_block (pstate, sym.block);
927 write_exp_elt_sym (pstate, sym.symbol);
928 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
931 qualified_name: TYPENAME COLONCOLON name
933 struct type *type = $1.type;
934 type = check_typedef (type);
935 if (!type_aggregate_p (type))
936 error (_("`%s' is not defined as an aggregate type."),
937 TYPE_SAFE_NAME (type));
939 write_exp_elt_opcode (pstate, OP_SCOPE);
940 write_exp_elt_type (pstate, type);
941 write_exp_string (pstate, $3);
942 write_exp_elt_opcode (pstate, OP_SCOPE);
944 | TYPENAME COLONCOLON '~' name
946 struct type *type = $1.type;
947 struct stoken tmp_token;
950 type = check_typedef (type);
951 if (!type_aggregate_p (type))
952 error (_("`%s' is not defined as an aggregate type."),
953 TYPE_SAFE_NAME (type));
954 buf = (char *) alloca ($4.length + 2);
956 tmp_token.length = $4.length + 1;
958 memcpy (buf+1, $4.ptr, $4.length);
959 buf[tmp_token.length] = 0;
961 /* Check for valid destructor name. */
962 destructor_name_p (tmp_token.ptr, $1.type);
963 write_exp_elt_opcode (pstate, OP_SCOPE);
964 write_exp_elt_type (pstate, type);
965 write_exp_string (pstate, tmp_token);
966 write_exp_elt_opcode (pstate, OP_SCOPE);
968 | TYPENAME COLONCOLON name COLONCOLON name
970 char *copy = copy_name ($3);
971 error (_("No type \"%s\" within class "
972 "or namespace \"%s\"."),
973 copy, TYPE_SAFE_NAME ($1.type));
977 variable: qualified_name
978 | COLONCOLON name_not_typename
980 char *name = copy_name ($2.stoken);
982 struct bound_minimal_symbol msymbol;
985 = lookup_symbol (name, (const struct block *) NULL,
986 VAR_DOMAIN, NULL).symbol;
989 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
990 write_exp_elt_block (pstate, NULL);
991 write_exp_elt_sym (pstate, sym);
992 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
996 msymbol = lookup_bound_minimal_symbol (name);
997 if (msymbol.minsym != NULL)
998 write_exp_msymbol (pstate, msymbol);
999 else if (!have_full_symbols () && !have_partial_symbols ())
1000 error (_("No symbol table is loaded. Use the \"file\" command."));
1002 error (_("No symbol \"%s\" in current context."), name);
1006 variable: name_not_typename
1007 { struct block_symbol sym = $1.sym;
1011 if (symbol_read_needs_frame (sym.symbol))
1013 if (innermost_block == 0
1014 || contained_in (sym.block,
1016 innermost_block = sym.block;
1019 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1020 write_exp_elt_block (pstate, sym.block);
1021 write_exp_elt_sym (pstate, sym.symbol);
1022 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1024 else if ($1.is_a_field_of_this)
1026 /* C++: it hangs off of `this'. Must
1027 not inadvertently convert from a method call
1029 if (innermost_block == 0
1030 || contained_in (sym.block,
1032 innermost_block = sym.block;
1033 write_exp_elt_opcode (pstate, OP_THIS);
1034 write_exp_elt_opcode (pstate, OP_THIS);
1035 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1036 write_exp_string (pstate, $1.stoken);
1037 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1041 char *arg = copy_name ($1.stoken);
1043 bound_minimal_symbol msymbol
1044 = lookup_bound_minimal_symbol (arg);
1045 if (msymbol.minsym == NULL)
1047 if (!have_full_symbols () && !have_partial_symbols ())
1048 error (_("No symbol table is loaded. Use the \"file\" command."));
1050 error (_("No symbol \"%s\" in current context."),
1051 copy_name ($1.stoken));
1054 /* This minsym might be an alias for
1055 another function. See if we can find
1056 the debug symbol for the target, and
1057 if so, use it instead, since it has
1058 return type / prototype info. This
1059 is important for example for "p
1060 *__errno_location()". */
1061 symbol *alias_target
1062 = find_function_alias_target (msymbol);
1063 if (alias_target != NULL)
1065 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1067 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1068 write_exp_elt_sym (pstate, alias_target);
1069 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1072 write_exp_msymbol (pstate, msymbol);
1077 space_identifier : '@' NAME
1078 { insert_type_address_space (pstate, copy_name ($2.stoken)); }
1081 const_or_volatile: const_or_volatile_noopt
1085 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1088 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1089 | const_or_volatile_noopt
1092 const_or_volatile_or_space_identifier:
1093 const_or_volatile_or_space_identifier_noopt
1099 { insert_type (tp_pointer); }
1100 const_or_volatile_or_space_identifier
1102 { insert_type (tp_pointer); }
1103 const_or_volatile_or_space_identifier
1105 { insert_type (tp_reference); }
1107 { insert_type (tp_reference); }
1109 { insert_type (tp_rvalue_reference); }
1110 | ANDAND ptr_operator
1111 { insert_type (tp_rvalue_reference); }
1114 ptr_operator_ts: ptr_operator
1116 $$ = get_type_stack ();
1117 /* This cleanup is eventually run by
1119 make_cleanup (type_stack_cleanup, $$);
1123 abs_decl: ptr_operator_ts direct_abs_decl
1124 { $$ = append_type_stack ($2, $1); }
1129 direct_abs_decl: '(' abs_decl ')'
1131 | direct_abs_decl array_mod
1133 push_type_stack ($1);
1135 push_type (tp_array);
1136 $$ = get_type_stack ();
1141 push_type (tp_array);
1142 $$ = get_type_stack ();
1145 | direct_abs_decl func_mod
1147 push_type_stack ($1);
1149 $$ = get_type_stack ();
1154 $$ = get_type_stack ();
1164 | OBJC_LBRAC INT ']'
1170 | '(' parameter_typelist ')'
1174 /* We used to try to recognize pointer to member types here, but
1175 that didn't work (shift/reduce conflicts meant that these rules never
1176 got executed). The problem is that
1177 int (foo::bar::baz::bizzle)
1178 is a function type but
1179 int (foo::bar::baz::bizzle::*)
1180 is a pointer to member type. Stroustrup loses again! */
1185 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1189 { $$ = lookup_signed_typename (parse_language (pstate),
1190 parse_gdbarch (pstate),
1193 { $$ = lookup_signed_typename (parse_language (pstate),
1194 parse_gdbarch (pstate),
1197 { $$ = lookup_signed_typename (parse_language (pstate),
1198 parse_gdbarch (pstate),
1201 { $$ = lookup_signed_typename (parse_language (pstate),
1202 parse_gdbarch (pstate),
1204 | LONG SIGNED_KEYWORD INT_KEYWORD
1205 { $$ = lookup_signed_typename (parse_language (pstate),
1206 parse_gdbarch (pstate),
1208 | LONG SIGNED_KEYWORD
1209 { $$ = lookup_signed_typename (parse_language (pstate),
1210 parse_gdbarch (pstate),
1212 | SIGNED_KEYWORD LONG INT_KEYWORD
1213 { $$ = lookup_signed_typename (parse_language (pstate),
1214 parse_gdbarch (pstate),
1216 | UNSIGNED LONG INT_KEYWORD
1217 { $$ = lookup_unsigned_typename (parse_language (pstate),
1218 parse_gdbarch (pstate),
1220 | LONG UNSIGNED INT_KEYWORD
1221 { $$ = lookup_unsigned_typename (parse_language (pstate),
1222 parse_gdbarch (pstate),
1225 { $$ = lookup_unsigned_typename (parse_language (pstate),
1226 parse_gdbarch (pstate),
1229 { $$ = lookup_signed_typename (parse_language (pstate),
1230 parse_gdbarch (pstate),
1232 | LONG LONG INT_KEYWORD
1233 { $$ = lookup_signed_typename (parse_language (pstate),
1234 parse_gdbarch (pstate),
1236 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1237 { $$ = lookup_signed_typename (parse_language (pstate),
1238 parse_gdbarch (pstate),
1240 | LONG LONG SIGNED_KEYWORD
1241 { $$ = lookup_signed_typename (parse_language (pstate),
1242 parse_gdbarch (pstate),
1244 | SIGNED_KEYWORD LONG LONG
1245 { $$ = lookup_signed_typename (parse_language (pstate),
1246 parse_gdbarch (pstate),
1248 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1249 { $$ = lookup_signed_typename (parse_language (pstate),
1250 parse_gdbarch (pstate),
1252 | UNSIGNED LONG LONG
1253 { $$ = lookup_unsigned_typename (parse_language (pstate),
1254 parse_gdbarch (pstate),
1256 | UNSIGNED LONG LONG INT_KEYWORD
1257 { $$ = lookup_unsigned_typename (parse_language (pstate),
1258 parse_gdbarch (pstate),
1260 | LONG LONG UNSIGNED
1261 { $$ = lookup_unsigned_typename (parse_language (pstate),
1262 parse_gdbarch (pstate),
1264 | LONG LONG UNSIGNED INT_KEYWORD
1265 { $$ = lookup_unsigned_typename (parse_language (pstate),
1266 parse_gdbarch (pstate),
1269 { $$ = lookup_signed_typename (parse_language (pstate),
1270 parse_gdbarch (pstate),
1272 | SHORT SIGNED_KEYWORD INT_KEYWORD
1273 { $$ = lookup_signed_typename (parse_language (pstate),
1274 parse_gdbarch (pstate),
1276 | SHORT SIGNED_KEYWORD
1277 { $$ = lookup_signed_typename (parse_language (pstate),
1278 parse_gdbarch (pstate),
1280 | UNSIGNED SHORT INT_KEYWORD
1281 { $$ = lookup_unsigned_typename (parse_language (pstate),
1282 parse_gdbarch (pstate),
1285 { $$ = lookup_unsigned_typename (parse_language (pstate),
1286 parse_gdbarch (pstate),
1288 | SHORT UNSIGNED INT_KEYWORD
1289 { $$ = lookup_unsigned_typename (parse_language (pstate),
1290 parse_gdbarch (pstate),
1293 { $$ = lookup_typename (parse_language (pstate),
1294 parse_gdbarch (pstate),
1296 (struct block *) NULL,
1298 | LONG DOUBLE_KEYWORD
1299 { $$ = lookup_typename (parse_language (pstate),
1300 parse_gdbarch (pstate),
1302 (struct block *) NULL,
1305 { $$ = lookup_struct (copy_name ($2),
1306 expression_context_block); }
1309 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1312 | STRUCT name COMPLETE
1314 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1319 { $$ = lookup_struct (copy_name ($2),
1320 expression_context_block); }
1323 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1326 | CLASS name COMPLETE
1328 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1333 { $$ = lookup_union (copy_name ($2),
1334 expression_context_block); }
1337 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1340 | UNION name COMPLETE
1342 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1347 { $$ = lookup_enum (copy_name ($2),
1348 expression_context_block); }
1351 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1354 | ENUM name COMPLETE
1356 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1360 | UNSIGNED type_name
1361 { $$ = lookup_unsigned_typename (parse_language (pstate),
1362 parse_gdbarch (pstate),
1363 TYPE_NAME($2.type)); }
1365 { $$ = lookup_unsigned_typename (parse_language (pstate),
1366 parse_gdbarch (pstate),
1368 | SIGNED_KEYWORD type_name
1369 { $$ = lookup_signed_typename (parse_language (pstate),
1370 parse_gdbarch (pstate),
1371 TYPE_NAME($2.type)); }
1373 { $$ = lookup_signed_typename (parse_language (pstate),
1374 parse_gdbarch (pstate),
1376 /* It appears that this rule for templates is never
1377 reduced; template recognition happens by lookahead
1378 in the token processing code in yylex. */
1379 | TEMPLATE name '<' type '>'
1380 { $$ = lookup_template_type(copy_name($2), $4,
1381 expression_context_block);
1383 | const_or_volatile_or_space_identifier_noopt typebase
1384 { $$ = follow_types ($2); }
1385 | typebase const_or_volatile_or_space_identifier_noopt
1386 { $$ = follow_types ($1); }
1392 $$.stoken.ptr = "int";
1393 $$.stoken.length = 3;
1394 $$.type = lookup_signed_typename (parse_language (pstate),
1395 parse_gdbarch (pstate),
1400 $$.stoken.ptr = "long";
1401 $$.stoken.length = 4;
1402 $$.type = lookup_signed_typename (parse_language (pstate),
1403 parse_gdbarch (pstate),
1408 $$.stoken.ptr = "short";
1409 $$.stoken.length = 5;
1410 $$.type = lookup_signed_typename (parse_language (pstate),
1411 parse_gdbarch (pstate),
1418 { check_parameter_typelist ($1); }
1419 | nonempty_typelist ',' DOTDOTDOT
1421 VEC_safe_push (type_ptr, $1, NULL);
1422 check_parameter_typelist ($1);
1430 VEC (type_ptr) *typelist = NULL;
1431 VEC_safe_push (type_ptr, typelist, $1);
1434 | nonempty_typelist ',' type
1436 VEC_safe_push (type_ptr, $1, $3);
1444 push_type_stack ($2);
1445 $$ = follow_types ($1);
1449 conversion_type_id: typebase conversion_declarator
1450 { $$ = follow_types ($1); }
1453 conversion_declarator: /* Nothing. */
1454 | ptr_operator conversion_declarator
1457 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1458 | VOLATILE_KEYWORD CONST_KEYWORD
1461 const_or_volatile_noopt: const_and_volatile
1462 { insert_type (tp_const);
1463 insert_type (tp_volatile);
1466 { insert_type (tp_const); }
1468 { insert_type (tp_volatile); }
1472 { $$ = operator_stoken (" new"); }
1474 { $$ = operator_stoken (" delete"); }
1475 | OPERATOR NEW '[' ']'
1476 { $$ = operator_stoken (" new[]"); }
1477 | OPERATOR DELETE '[' ']'
1478 { $$ = operator_stoken (" delete[]"); }
1479 | OPERATOR NEW OBJC_LBRAC ']'
1480 { $$ = operator_stoken (" new[]"); }
1481 | OPERATOR DELETE OBJC_LBRAC ']'
1482 { $$ = operator_stoken (" delete[]"); }
1484 { $$ = operator_stoken ("+"); }
1486 { $$ = operator_stoken ("-"); }
1488 { $$ = operator_stoken ("*"); }
1490 { $$ = operator_stoken ("/"); }
1492 { $$ = operator_stoken ("%"); }
1494 { $$ = operator_stoken ("^"); }
1496 { $$ = operator_stoken ("&"); }
1498 { $$ = operator_stoken ("|"); }
1500 { $$ = operator_stoken ("~"); }
1502 { $$ = operator_stoken ("!"); }
1504 { $$ = operator_stoken ("="); }
1506 { $$ = operator_stoken ("<"); }
1508 { $$ = operator_stoken (">"); }
1509 | OPERATOR ASSIGN_MODIFY
1510 { const char *op = "unknown";
1534 case BINOP_BITWISE_IOR:
1537 case BINOP_BITWISE_AND:
1540 case BINOP_BITWISE_XOR:
1547 $$ = operator_stoken (op);
1550 { $$ = operator_stoken ("<<"); }
1552 { $$ = operator_stoken (">>"); }
1554 { $$ = operator_stoken ("=="); }
1556 { $$ = operator_stoken ("!="); }
1558 { $$ = operator_stoken ("<="); }
1560 { $$ = operator_stoken (">="); }
1562 { $$ = operator_stoken ("&&"); }
1564 { $$ = operator_stoken ("||"); }
1565 | OPERATOR INCREMENT
1566 { $$ = operator_stoken ("++"); }
1567 | OPERATOR DECREMENT
1568 { $$ = operator_stoken ("--"); }
1570 { $$ = operator_stoken (","); }
1571 | OPERATOR ARROW_STAR
1572 { $$ = operator_stoken ("->*"); }
1574 { $$ = operator_stoken ("->"); }
1576 { $$ = operator_stoken ("()"); }
1578 { $$ = operator_stoken ("[]"); }
1579 | OPERATOR OBJC_LBRAC ']'
1580 { $$ = operator_stoken ("[]"); }
1581 | OPERATOR conversion_type_id
1584 c_print_type ($2, NULL, &buf, -1, 0,
1585 &type_print_raw_options);
1586 $$ = operator_stoken (buf.c_str ());
1592 name : NAME { $$ = $1.stoken; }
1593 | BLOCKNAME { $$ = $1.stoken; }
1594 | TYPENAME { $$ = $1.stoken; }
1595 | NAME_OR_INT { $$ = $1.stoken; }
1596 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1600 name_not_typename : NAME
1602 /* These would be useful if name_not_typename was useful, but it is just
1603 a fake for "variable", so these cause reduce/reduce conflicts because
1604 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1605 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1606 context where only a name could occur, this might be useful.
1611 struct field_of_this_result is_a_field_of_this;
1614 $$.sym = lookup_symbol ($1.ptr,
1615 expression_context_block,
1617 &is_a_field_of_this);
1618 $$.is_a_field_of_this
1619 = is_a_field_of_this.type != NULL;
1626 /* Like write_exp_string, but prepends a '~'. */
1629 write_destructor_name (struct parser_state *par_state, struct stoken token)
1631 char *copy = (char *) alloca (token.length + 1);
1634 memcpy (©[1], token.ptr, token.length);
1639 write_exp_string (par_state, token);
1642 /* Returns a stoken of the operator name given by OP (which does not
1643 include the string "operator"). */
1645 static struct stoken
1646 operator_stoken (const char *op)
1648 struct stoken st = { NULL, 0 };
1651 st.length = CP_OPERATOR_LEN + strlen (op);
1652 buf = (char *) malloc (st.length + 1);
1653 strcpy (buf, CP_OPERATOR_STR);
1657 /* The toplevel (c_parse) will free the memory allocated here. */
1658 make_cleanup (free, buf);
1662 /* Return true if the type is aggregate-like. */
1665 type_aggregate_p (struct type *type)
1667 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1668 || TYPE_CODE (type) == TYPE_CODE_UNION
1669 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1670 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1671 && TYPE_DECLARED_CLASS (type)));
1674 /* Validate a parameter typelist. */
1677 check_parameter_typelist (VEC (type_ptr) *params)
1682 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1684 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1688 if (VEC_length (type_ptr, params) == 1)
1693 VEC_free (type_ptr, params);
1694 error (_("parameter types following 'void'"));
1698 VEC_free (type_ptr, params);
1699 error (_("'void' invalid as parameter type"));
1705 /* Take care of parsing a number (anything that starts with a digit).
1706 Set yylval and return the token type; update lexptr.
1707 LEN is the number of characters in it. */
1709 /*** Needs some error checking for the float case ***/
1712 parse_number (struct parser_state *par_state,
1713 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1715 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1716 here, and we do kind of silly things like cast to unsigned. */
1723 int base = input_radix;
1726 /* Number of "L" suffixes encountered. */
1729 /* We have found a "L" or "U" suffix. */
1730 int found_suffix = 0;
1733 struct type *signed_type;
1734 struct type *unsigned_type;
1737 p = (char *) alloca (len);
1738 memcpy (p, buf, len);
1742 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1743 point. Return DECFLOAT. */
1745 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1748 putithere->typed_val_decfloat.type
1749 = parse_type (par_state)->builtin_decfloat;
1750 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1751 gdbarch_byte_order (parse_gdbarch (par_state)),
1757 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1760 putithere->typed_val_decfloat.type
1761 = parse_type (par_state)->builtin_decdouble;
1762 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1763 gdbarch_byte_order (parse_gdbarch (par_state)),
1769 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1772 putithere->typed_val_decfloat.type
1773 = parse_type (par_state)->builtin_declong;
1774 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1775 gdbarch_byte_order (parse_gdbarch (par_state)),
1781 if (! parse_c_float (parse_gdbarch (par_state), p, len,
1782 &putithere->typed_val_float.dval,
1783 &putithere->typed_val_float.type))
1788 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1789 if (p[0] == '0' && len > 1)
1832 if (c >= 'A' && c <= 'Z')
1834 if (c != 'l' && c != 'u')
1836 if (c >= '0' && c <= '9')
1844 if (base > 10 && c >= 'a' && c <= 'f')
1848 n += i = c - 'a' + 10;
1861 return ERROR; /* Char not a digit */
1864 return ERROR; /* Invalid digit in this base */
1866 /* Portably test for overflow (only works for nonzero values, so make
1867 a second check for zero). FIXME: Can't we just make n and prevn
1868 unsigned and avoid this? */
1869 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1870 unsigned_p = 1; /* Try something unsigned */
1872 /* Portably test for unsigned overflow.
1873 FIXME: This check is wrong; for example it doesn't find overflow
1874 on 0x123456789 when LONGEST is 32 bits. */
1875 if (c != 'l' && c != 'u' && n != 0)
1877 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1878 error (_("Numeric constant too large."));
1883 /* An integer constant is an int, a long, or a long long. An L
1884 suffix forces it to be long; an LL suffix forces it to be long
1885 long. If not forced to a larger size, it gets the first type of
1886 the above that it fits in. To figure out whether it fits, we
1887 shift it right and see whether anything remains. Note that we
1888 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1889 operation, because many compilers will warn about such a shift
1890 (which always produces a zero result). Sometimes gdbarch_int_bit
1891 or gdbarch_long_bit will be that big, sometimes not. To deal with
1892 the case where it is we just always shift the value more than
1893 once, with fewer bits each time. */
1895 un = (ULONGEST)n >> 2;
1897 && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
1900 = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
1902 /* A large decimal (not hex or octal) constant (between INT_MAX
1903 and UINT_MAX) is a long or unsigned long, according to ANSI,
1904 never an unsigned int, but this code treats it as unsigned
1905 int. This probably should be fixed. GCC gives a warning on
1908 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
1909 signed_type = parse_type (par_state)->builtin_int;
1911 else if (long_p <= 1
1912 && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
1915 = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
1916 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
1917 signed_type = parse_type (par_state)->builtin_long;
1922 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1923 < gdbarch_long_long_bit (parse_gdbarch (par_state)))
1924 /* A long long does not fit in a LONGEST. */
1925 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1927 shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
1928 high_bit = (ULONGEST) 1 << shift;
1929 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
1930 signed_type = parse_type (par_state)->builtin_long_long;
1933 putithere->typed_val_int.val = n;
1935 /* If the high bit of the worked out type is set then this number
1936 has to be unsigned. */
1938 if (unsigned_p || (n & high_bit))
1940 putithere->typed_val_int.type = unsigned_type;
1944 putithere->typed_val_int.type = signed_type;
1950 /* Temporary obstack used for holding strings. */
1951 static struct obstack tempbuf;
1952 static int tempbuf_init;
1954 /* Parse a C escape sequence. The initial backslash of the sequence
1955 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1956 last character of the sequence. If OUTPUT is not NULL, the
1957 translated form of the escape sequence will be written there. If
1958 OUTPUT is NULL, no output is written and the call will only affect
1959 *PTR. If an escape sequence is expressed in target bytes, then the
1960 entire sequence will simply be copied to OUTPUT. Return 1 if any
1961 character was emitted, 0 otherwise. */
1964 c_parse_escape (const char **ptr, struct obstack *output)
1966 const char *tokptr = *ptr;
1969 /* Some escape sequences undergo character set conversion. Those we
1973 /* Hex escapes do not undergo character set conversion, so keep
1974 the escape sequence for later. */
1977 obstack_grow_str (output, "\\x");
1979 if (!isxdigit (*tokptr))
1980 error (_("\\x escape without a following hex digit"));
1981 while (isxdigit (*tokptr))
1984 obstack_1grow (output, *tokptr);
1989 /* Octal escapes do not undergo character set conversion, so
1990 keep the escape sequence for later. */
2002 obstack_grow_str (output, "\\");
2004 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
2008 obstack_1grow (output, *tokptr);
2014 /* We handle UCNs later. We could handle them here, but that
2015 would mean a spurious error in the case where the UCN could
2016 be converted to the target charset but not the host
2022 int i, len = c == 'U' ? 8 : 4;
2025 obstack_1grow (output, '\\');
2026 obstack_1grow (output, *tokptr);
2029 if (!isxdigit (*tokptr))
2030 error (_("\\%c escape without a following hex digit"), c);
2031 for (i = 0; i < len && isxdigit (*tokptr); ++i)
2034 obstack_1grow (output, *tokptr);
2040 /* We must pass backslash through so that it does not
2041 cause quoting during the second expansion. */
2044 obstack_grow_str (output, "\\\\");
2048 /* Escapes which undergo conversion. */
2051 obstack_1grow (output, '\a');
2056 obstack_1grow (output, '\b');
2061 obstack_1grow (output, '\f');
2066 obstack_1grow (output, '\n');
2071 obstack_1grow (output, '\r');
2076 obstack_1grow (output, '\t');
2081 obstack_1grow (output, '\v');
2085 /* GCC extension. */
2088 obstack_1grow (output, HOST_ESCAPE_CHAR);
2092 /* Backslash-newline expands to nothing at all. */
2098 /* A few escapes just expand to the character itself. */
2102 /* GCC extensions. */
2107 /* Unrecognized escapes turn into the character itself. */
2110 obstack_1grow (output, *tokptr);
2118 /* Parse a string or character literal from TOKPTR. The string or
2119 character may be wide or unicode. *OUTPTR is set to just after the
2120 end of the literal in the input string. The resulting token is
2121 stored in VALUE. This returns a token value, either STRING or
2122 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2123 number of host characters in the literal. */
2126 parse_string_or_char (const char *tokptr, const char **outptr,
2127 struct typed_stoken *value, int *host_chars)
2133 /* Build the gdb internal form of the input string in tempbuf. Note
2134 that the buffer is null byte terminated *only* for the
2135 convenience of debugging gdb itself and printing the buffer
2136 contents when the buffer contains no embedded nulls. Gdb does
2137 not depend upon the buffer being null byte terminated, it uses
2138 the length string instead. This allows gdb to handle C strings
2139 (as well as strings in other languages) with embedded null
2145 obstack_free (&tempbuf, NULL);
2146 obstack_init (&tempbuf);
2148 /* Record the string type. */
2151 type = C_WIDE_STRING;
2154 else if (*tokptr == 'u')
2159 else if (*tokptr == 'U')
2164 else if (*tokptr == '@')
2166 /* An Objective C string. */
2174 /* Skip the quote. */
2188 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2190 else if (c == quote)
2194 obstack_1grow (&tempbuf, c);
2196 /* FIXME: this does the wrong thing with multi-byte host
2197 characters. We could use mbrlen here, but that would
2198 make "set host-charset" a bit less useful. */
2203 if (*tokptr != quote)
2206 error (_("Unterminated string in expression."));
2208 error (_("Unmatched single quote."));
2213 value->ptr = (char *) obstack_base (&tempbuf);
2214 value->length = obstack_object_size (&tempbuf);
2218 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2221 /* This is used to associate some attributes with a token. */
2225 /* If this bit is set, the token is C++-only. */
2229 /* If this bit is set, the token is conditional: if there is a
2230 symbol of the same name, then the token is a symbol; otherwise,
2231 the token is a keyword. */
2235 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2241 enum exp_opcode opcode;
2245 static const struct token tokentab3[] =
2247 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2248 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2249 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2250 {"...", DOTDOTDOT, BINOP_END, 0}
2253 static const struct token tokentab2[] =
2255 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2256 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2257 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2258 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2259 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2260 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2261 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2262 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2263 {"++", INCREMENT, BINOP_END, 0},
2264 {"--", DECREMENT, BINOP_END, 0},
2265 {"->", ARROW, BINOP_END, 0},
2266 {"&&", ANDAND, BINOP_END, 0},
2267 {"||", OROR, BINOP_END, 0},
2268 /* "::" is *not* only C++: gdb overrides its meaning in several
2269 different ways, e.g., 'filename'::func, function::variable. */
2270 {"::", COLONCOLON, BINOP_END, 0},
2271 {"<<", LSH, BINOP_END, 0},
2272 {">>", RSH, BINOP_END, 0},
2273 {"==", EQUAL, BINOP_END, 0},
2274 {"!=", NOTEQUAL, BINOP_END, 0},
2275 {"<=", LEQ, BINOP_END, 0},
2276 {">=", GEQ, BINOP_END, 0},
2277 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2280 /* Identifier-like tokens. */
2281 static const struct token ident_tokens[] =
2283 {"unsigned", UNSIGNED, OP_NULL, 0},
2284 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2285 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2286 {"struct", STRUCT, OP_NULL, 0},
2287 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2288 {"sizeof", SIZEOF, OP_NULL, 0},
2289 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2290 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2291 {"class", CLASS, OP_NULL, FLAG_CXX},
2292 {"union", UNION, OP_NULL, 0},
2293 {"short", SHORT, OP_NULL, 0},
2294 {"const", CONST_KEYWORD, OP_NULL, 0},
2295 {"enum", ENUM, OP_NULL, 0},
2296 {"long", LONG, OP_NULL, 0},
2297 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2298 {"int", INT_KEYWORD, OP_NULL, 0},
2299 {"new", NEW, OP_NULL, FLAG_CXX},
2300 {"delete", DELETE, OP_NULL, FLAG_CXX},
2301 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2303 {"and", ANDAND, BINOP_END, FLAG_CXX},
2304 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2305 {"bitand", '&', OP_NULL, FLAG_CXX},
2306 {"bitor", '|', OP_NULL, FLAG_CXX},
2307 {"compl", '~', OP_NULL, FLAG_CXX},
2308 {"not", '!', OP_NULL, FLAG_CXX},
2309 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2310 {"or", OROR, BINOP_END, FLAG_CXX},
2311 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2312 {"xor", '^', OP_NULL, FLAG_CXX},
2313 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2315 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2316 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2317 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2318 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2320 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2321 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2322 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2323 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2324 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2326 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2329 /* When we find that lexptr (the global var defined in parse.c) is
2330 pointing at a macro invocation, we expand the invocation, and call
2331 scan_macro_expansion to save the old lexptr here and point lexptr
2332 into the expanded text. When we reach the end of that, we call
2333 end_macro_expansion to pop back to the value we saved here. The
2334 macro expansion code promises to return only fully-expanded text,
2335 so we don't need to "push" more than one level.
2337 This is disgusting, of course. It would be cleaner to do all macro
2338 expansion beforehand, and then hand that to lexptr. But we don't
2339 really know where the expression ends. Remember, in a command like
2341 (gdb) break *ADDRESS if CONDITION
2343 we evaluate ADDRESS in the scope of the current frame, but we
2344 evaluate CONDITION in the scope of the breakpoint's location. So
2345 it's simply wrong to try to macro-expand the whole thing at once. */
2346 static const char *macro_original_text;
2348 /* We save all intermediate macro expansions on this obstack for the
2349 duration of a single parse. The expansion text may sometimes have
2350 to live past the end of the expansion, due to yacc lookahead.
2351 Rather than try to be clever about saving the data for a single
2352 token, we simply keep it all and delete it after parsing has
2354 static struct obstack expansion_obstack;
2357 scan_macro_expansion (char *expansion)
2361 /* We'd better not be trying to push the stack twice. */
2362 gdb_assert (! macro_original_text);
2364 /* Copy to the obstack, and then free the intermediate
2366 copy = (char *) obstack_copy0 (&expansion_obstack, expansion,
2367 strlen (expansion));
2370 /* Save the old lexptr value, so we can return to it when we're done
2371 parsing the expanded text. */
2372 macro_original_text = lexptr;
2377 scanning_macro_expansion (void)
2379 return macro_original_text != 0;
2383 finished_macro_expansion (void)
2385 /* There'd better be something to pop back to. */
2386 gdb_assert (macro_original_text);
2388 /* Pop back to the original text. */
2389 lexptr = macro_original_text;
2390 macro_original_text = 0;
2394 scan_macro_cleanup (void *dummy)
2396 if (macro_original_text)
2397 finished_macro_expansion ();
2399 obstack_free (&expansion_obstack, NULL);
2402 /* Return true iff the token represents a C++ cast operator. */
2405 is_cast_operator (const char *token, int len)
2407 return (! strncmp (token, "dynamic_cast", len)
2408 || ! strncmp (token, "static_cast", len)
2409 || ! strncmp (token, "reinterpret_cast", len)
2410 || ! strncmp (token, "const_cast", len));
2413 /* The scope used for macro expansion. */
2414 static struct macro_scope *expression_macro_scope;
2416 /* This is set if a NAME token appeared at the very end of the input
2417 string, with no whitespace separating the name from the EOF. This
2418 is used only when parsing to do field name completion. */
2419 static int saw_name_at_eof;
2421 /* This is set if the previously-returned token was a structure
2422 operator -- either '.' or ARROW. This is used only when parsing to
2423 do field name completion. */
2424 static int last_was_structop;
2426 /* Read one token, getting characters through lexptr. */
2429 lex_one_token (struct parser_state *par_state, int *is_quoted_name)
2434 const char *tokstart;
2435 int saw_structop = last_was_structop;
2438 last_was_structop = 0;
2439 *is_quoted_name = 0;
2443 /* Check if this is a macro invocation that we need to expand. */
2444 if (! scanning_macro_expansion ())
2446 char *expanded = macro_expand_next (&lexptr,
2447 standard_macro_lookup,
2448 expression_macro_scope);
2451 scan_macro_expansion (expanded);
2454 prev_lexptr = lexptr;
2457 /* See if it is a special token of length 3. */
2458 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2459 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2461 if ((tokentab3[i].flags & FLAG_CXX) != 0
2462 && parse_language (par_state)->la_language != language_cplus)
2466 yylval.opcode = tokentab3[i].opcode;
2467 return tokentab3[i].token;
2470 /* See if it is a special token of length 2. */
2471 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2472 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2474 if ((tokentab2[i].flags & FLAG_CXX) != 0
2475 && parse_language (par_state)->la_language != language_cplus)
2479 yylval.opcode = tokentab2[i].opcode;
2480 if (parse_completion && tokentab2[i].token == ARROW)
2481 last_was_structop = 1;
2482 return tokentab2[i].token;
2485 switch (c = *tokstart)
2488 /* If we were just scanning the result of a macro expansion,
2489 then we need to resume scanning the original text.
2490 If we're parsing for field name completion, and the previous
2491 token allows such completion, return a COMPLETE token.
2492 Otherwise, we were already scanning the original text, and
2493 we're really done. */
2494 if (scanning_macro_expansion ())
2496 finished_macro_expansion ();
2499 else if (saw_name_at_eof)
2501 saw_name_at_eof = 0;
2504 else if (saw_structop)
2519 if (parse_language (par_state)->la_language == language_objc
2526 if (paren_depth == 0)
2533 if (comma_terminates
2535 && ! scanning_macro_expansion ())
2541 /* Might be a floating point number. */
2542 if (lexptr[1] < '0' || lexptr[1] > '9')
2544 if (parse_completion)
2545 last_was_structop = 1;
2546 goto symbol; /* Nope, must be a symbol. */
2548 /* FALL THRU into number case. */
2561 /* It's a number. */
2562 int got_dot = 0, got_e = 0, toktype;
2563 const char *p = tokstart;
2564 int hex = input_radix > 10;
2566 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2571 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2579 /* This test includes !hex because 'e' is a valid hex digit
2580 and thus does not indicate a floating point number when
2581 the radix is hex. */
2582 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2583 got_dot = got_e = 1;
2584 /* This test does not include !hex, because a '.' always indicates
2585 a decimal floating point number regardless of the radix. */
2586 else if (!got_dot && *p == '.')
2588 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2589 && (*p == '-' || *p == '+'))
2590 /* This is the sign of the exponent, not the end of the
2593 /* We will take any letters or digits. parse_number will
2594 complain if past the radix, or if L or U are not final. */
2595 else if ((*p < '0' || *p > '9')
2596 && ((*p < 'a' || *p > 'z')
2597 && (*p < 'A' || *p > 'Z')))
2600 toktype = parse_number (par_state, tokstart, p - tokstart,
2601 got_dot|got_e, &yylval);
2602 if (toktype == ERROR)
2604 char *err_copy = (char *) alloca (p - tokstart + 1);
2606 memcpy (err_copy, tokstart, p - tokstart);
2607 err_copy[p - tokstart] = 0;
2608 error (_("Invalid number \"%s\"."), err_copy);
2616 const char *p = &tokstart[1];
2617 size_t len = strlen ("entry");
2619 if (parse_language (par_state)->la_language == language_objc)
2621 size_t len = strlen ("selector");
2623 if (strncmp (p, "selector", len) == 0
2624 && (p[len] == '\0' || isspace (p[len])))
2633 while (isspace (*p))
2635 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2667 if (tokstart[1] != '"' && tokstart[1] != '\'')
2676 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2681 error (_("Empty character constant."));
2682 else if (host_len > 2 && c == '\'')
2685 namelen = lexptr - tokstart - 1;
2686 *is_quoted_name = 1;
2690 else if (host_len > 1)
2691 error (_("Invalid character constant."));
2697 if (!(c == '_' || c == '$'
2698 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2699 /* We must have come across a bad character (e.g. ';'). */
2700 error (_("Invalid character '%c' in expression."), c);
2702 /* It's a name. See how long it is. */
2704 for (c = tokstart[namelen];
2705 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2706 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2708 /* Template parameter lists are part of the name.
2709 FIXME: This mishandles `print $a<4&&$a>3'. */
2713 if (! is_cast_operator (tokstart, namelen))
2715 /* Scan ahead to get rest of the template specification. Note
2716 that we look ahead only when the '<' adjoins non-whitespace
2717 characters; for comparison expressions, e.g. "a < b > c",
2718 there must be spaces before the '<', etc. */
2719 const char *p = find_template_name_end (tokstart + namelen);
2722 namelen = p - tokstart;
2726 c = tokstart[++namelen];
2729 /* The token "if" terminates the expression and is NOT removed from
2730 the input stream. It doesn't count if it appears in the
2731 expansion of a macro. */
2733 && tokstart[0] == 'i'
2734 && tokstart[1] == 'f'
2735 && ! scanning_macro_expansion ())
2740 /* For the same reason (breakpoint conditions), "thread N"
2741 terminates the expression. "thread" could be an identifier, but
2742 an identifier is never followed by a number without intervening
2743 punctuation. "task" is similar. Handle abbreviations of these,
2744 similarly to breakpoint.c:find_condition_and_thread. */
2746 && (strncmp (tokstart, "thread", namelen) == 0
2747 || strncmp (tokstart, "task", namelen) == 0)
2748 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2749 && ! scanning_macro_expansion ())
2751 const char *p = tokstart + namelen + 1;
2753 while (*p == ' ' || *p == '\t')
2755 if (*p >= '0' && *p <= '9')
2763 yylval.sval.ptr = tokstart;
2764 yylval.sval.length = namelen;
2766 /* Catch specific keywords. */
2767 copy = copy_name (yylval.sval);
2768 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2769 if (strcmp (copy, ident_tokens[i].oper) == 0)
2771 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2772 && parse_language (par_state)->la_language != language_cplus)
2775 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2777 struct field_of_this_result is_a_field_of_this;
2779 if (lookup_symbol (copy, expression_context_block,
2781 (parse_language (par_state)->la_language
2782 == language_cplus ? &is_a_field_of_this
2786 /* The keyword is shadowed. */
2791 /* It is ok to always set this, even though we don't always
2792 strictly need to. */
2793 yylval.opcode = ident_tokens[i].opcode;
2794 return ident_tokens[i].token;
2797 if (*tokstart == '$')
2800 if (parse_completion && *lexptr == '\0')
2801 saw_name_at_eof = 1;
2803 yylval.ssym.stoken = yylval.sval;
2804 yylval.ssym.sym.symbol = NULL;
2805 yylval.ssym.sym.block = NULL;
2806 yylval.ssym.is_a_field_of_this = 0;
2810 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2817 DEF_VEC_O (token_and_value);
2819 /* A FIFO of tokens that have been read but not yet returned to the
2821 static VEC (token_and_value) *token_fifo;
2823 /* Non-zero if the lexer should return tokens from the FIFO. */
2826 /* Temporary storage for c_lex; this holds symbol names as they are
2828 auto_obstack name_obstack;
2830 /* Classify a NAME token. The contents of the token are in `yylval'.
2831 Updates yylval and returns the new token type. BLOCK is the block
2832 in which lookups start; this can be NULL to mean the global scope.
2833 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2834 in single quotes. */
2837 classify_name (struct parser_state *par_state, const struct block *block,
2840 struct block_symbol bsym;
2842 struct field_of_this_result is_a_field_of_this;
2844 copy = copy_name (yylval.sval);
2846 /* Initialize this in case we *don't* use it in this call; that way
2847 we can refer to it unconditionally below. */
2848 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2850 bsym = lookup_symbol (copy, block, VAR_DOMAIN,
2851 parse_language (par_state)->la_name_of_this
2852 ? &is_a_field_of_this : NULL);
2854 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
2856 yylval.ssym.sym = bsym;
2857 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2860 else if (!bsym.symbol)
2862 /* If we found a field of 'this', we might have erroneously
2863 found a constructor where we wanted a type name. Handle this
2864 case by noticing that we found a constructor and then look up
2865 the type tag instead. */
2866 if (is_a_field_of_this.type != NULL
2867 && is_a_field_of_this.fn_field != NULL
2868 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2871 struct field_of_this_result inner_is_a_field_of_this;
2873 bsym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2874 &inner_is_a_field_of_this);
2875 if (bsym.symbol != NULL)
2877 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
2882 /* If we found a field, then we want to prefer it over a
2883 filename. However, if the name was quoted, then it is better
2884 to check for a filename or a block, since this is the only
2885 way the user has of requiring the extension to be used. */
2886 if (is_a_field_of_this.type == NULL || is_quoted_name)
2888 /* See if it's a file name. */
2889 struct symtab *symtab;
2891 symtab = lookup_symtab (copy);
2894 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
2901 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
2903 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
2907 /* See if it's an ObjC classname. */
2908 if (parse_language (par_state)->la_language == language_objc && !bsym.symbol)
2910 CORE_ADDR Class = lookup_objc_class (parse_gdbarch (par_state), copy);
2915 yylval.theclass.theclass = Class;
2916 sym = lookup_struct_typedef (copy, expression_context_block, 1);
2918 yylval.theclass.type = SYMBOL_TYPE (sym);
2923 /* Input names that aren't symbols but ARE valid hex numbers, when
2924 the input radix permits them, can be names or numbers depending
2925 on the parse. Note we support radixes > 16 here. */
2927 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2928 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2930 YYSTYPE newlval; /* Its value is ignored. */
2931 int hextype = parse_number (par_state, copy, yylval.sval.length,
2936 yylval.ssym.sym = bsym;
2937 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2942 /* Any other kind of symbol */
2943 yylval.ssym.sym = bsym;
2944 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2946 if (bsym.symbol == NULL
2947 && parse_language (par_state)->la_language == language_cplus
2948 && is_a_field_of_this.type == NULL
2949 && lookup_minimal_symbol (copy, NULL, NULL).minsym == NULL)
2950 return UNKNOWN_CPP_NAME;
2955 /* Like classify_name, but used by the inner loop of the lexer, when a
2956 name might have already been seen. CONTEXT is the context type, or
2957 NULL if this is the first component of a name. */
2960 classify_inner_name (struct parser_state *par_state,
2961 const struct block *block, struct type *context)
2966 if (context == NULL)
2967 return classify_name (par_state, block, 0);
2969 type = check_typedef (context);
2970 if (!type_aggregate_p (type))
2973 copy = copy_name (yylval.ssym.stoken);
2974 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
2975 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block, VAR_DOMAIN);
2977 /* If no symbol was found, search for a matching base class named
2978 COPY. This will allow users to enter qualified names of class members
2979 relative to the `this' pointer. */
2980 if (yylval.ssym.sym.symbol == NULL)
2982 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
2984 if (base_type != NULL)
2986 yylval.tsym.type = base_type;
2993 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
2997 /* cp_lookup_nested_symbol might have accidentally found a constructor
2998 named COPY when we really wanted a base class of the same name.
2999 Double-check this case by looking for a base class. */
3001 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
3003 if (base_type != NULL)
3005 yylval.tsym.type = base_type;
3012 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3018 internal_error (__FILE__, __LINE__, _("not reached"));
3021 /* The outer level of a two-level lexer. This calls the inner lexer
3022 to return tokens. It then either returns these tokens, or
3023 aggregates them into a larger token. This lets us work around a
3024 problem in our parsing approach, where the parser could not
3025 distinguish between qualified names and qualified types at the
3028 This approach is still not ideal, because it mishandles template
3029 types. See the comment in lex_one_token for an example. However,
3030 this is still an improvement over the earlier approach, and will
3031 suffice until we move to better parsing technology. */
3036 token_and_value current;
3037 int first_was_coloncolon, last_was_coloncolon;
3038 struct type *context_type = NULL;
3039 int last_to_examine, next_to_examine, checkpoint;
3040 const struct block *search_block;
3043 if (popping && !VEC_empty (token_and_value, token_fifo))
3047 /* Read the first token and decide what to do. Most of the
3048 subsequent code is C++-only; but also depends on seeing a "::" or
3050 current.token = lex_one_token (pstate, &is_quoted_name);
3051 if (current.token == NAME)
3052 current.token = classify_name (pstate, expression_context_block,
3054 if (parse_language (pstate)->la_language != language_cplus
3055 || (current.token != TYPENAME && current.token != COLONCOLON
3056 && current.token != FILENAME))
3057 return current.token;
3059 /* Read any sequence of alternating "::" and name-like tokens into
3061 current.value = yylval;
3062 VEC_safe_push (token_and_value, token_fifo, ¤t);
3063 last_was_coloncolon = current.token == COLONCOLON;
3068 /* We ignore quoted names other than the very first one.
3069 Subsequent ones do not have any special meaning. */
3070 current.token = lex_one_token (pstate, &ignore);
3071 current.value = yylval;
3072 VEC_safe_push (token_and_value, token_fifo, ¤t);
3074 if ((last_was_coloncolon && current.token != NAME)
3075 || (!last_was_coloncolon && current.token != COLONCOLON))
3077 last_was_coloncolon = !last_was_coloncolon;
3081 /* We always read one extra token, so compute the number of tokens
3082 to examine accordingly. */
3083 last_to_examine = VEC_length (token_and_value, token_fifo) - 2;
3084 next_to_examine = 0;
3086 current = *VEC_index (token_and_value, token_fifo, next_to_examine);
3089 name_obstack.clear ();
3091 if (current.token == FILENAME)
3092 search_block = current.value.bval;
3093 else if (current.token == COLONCOLON)
3094 search_block = NULL;
3097 gdb_assert (current.token == TYPENAME);
3098 search_block = expression_context_block;
3099 obstack_grow (&name_obstack, current.value.sval.ptr,
3100 current.value.sval.length);
3101 context_type = current.value.tsym.type;
3105 first_was_coloncolon = current.token == COLONCOLON;
3106 last_was_coloncolon = first_was_coloncolon;
3108 while (next_to_examine <= last_to_examine)
3110 token_and_value *next;
3112 next = VEC_index (token_and_value, token_fifo, next_to_examine);
3115 if (next->token == NAME && last_was_coloncolon)
3119 yylval = next->value;
3120 classification = classify_inner_name (pstate, search_block,
3122 /* We keep going until we either run out of names, or until
3123 we have a qualified name which is not a type. */
3124 if (classification != TYPENAME && classification != NAME)
3127 /* Accept up to this token. */
3128 checkpoint = next_to_examine;
3130 /* Update the partial name we are constructing. */
3131 if (context_type != NULL)
3133 /* We don't want to put a leading "::" into the name. */
3134 obstack_grow_str (&name_obstack, "::");
3136 obstack_grow (&name_obstack, next->value.sval.ptr,
3137 next->value.sval.length);
3139 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3140 yylval.sval.length = obstack_object_size (&name_obstack);
3141 current.value = yylval;
3142 current.token = classification;
3144 last_was_coloncolon = 0;
3146 if (classification == NAME)
3149 context_type = yylval.tsym.type;
3151 else if (next->token == COLONCOLON && !last_was_coloncolon)
3152 last_was_coloncolon = 1;
3155 /* We've reached the end of the name. */
3160 /* If we have a replacement token, install it as the first token in
3161 the FIFO, and delete the other constituent tokens. */
3164 current.value.sval.ptr
3165 = (const char *) obstack_copy0 (&expansion_obstack,
3166 current.value.sval.ptr,
3167 current.value.sval.length);
3169 VEC_replace (token_and_value, token_fifo, 0, ¤t);
3171 VEC_block_remove (token_and_value, token_fifo, 1, checkpoint - 1);
3175 current = *VEC_index (token_and_value, token_fifo, 0);
3176 VEC_ordered_remove (token_and_value, token_fifo, 0);
3177 yylval = current.value;
3178 return current.token;
3182 c_parse (struct parser_state *par_state)
3185 struct cleanup *back_to;
3187 /* Setting up the parser state. */
3188 gdb_assert (par_state != NULL);
3191 /* Note that parsing (within yyparse) freely installs cleanups
3192 assuming they'll be run here (below). */
3194 back_to = make_cleanup (free_current_contents, &expression_macro_scope);
3195 make_cleanup_clear_parser_state (&pstate);
3197 /* Set up the scope for macro expansion. */
3198 expression_macro_scope = NULL;
3200 if (expression_context_block)
3201 expression_macro_scope
3202 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
3204 expression_macro_scope = default_macro_scope ();
3205 if (! expression_macro_scope)
3206 expression_macro_scope = user_macro_scope ();
3208 /* Initialize macro expansion code. */
3209 obstack_init (&expansion_obstack);
3210 gdb_assert (! macro_original_text);
3211 make_cleanup (scan_macro_cleanup, 0);
3213 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3216 /* Initialize some state used by the lexer. */
3217 last_was_structop = 0;
3218 saw_name_at_eof = 0;
3220 VEC_free (token_and_value, token_fifo);
3222 name_obstack.clear ();
3224 result = yyparse ();
3225 do_cleanups (back_to);
3232 /* This is called via the YYPRINT macro when parser debugging is
3233 enabled. It prints a token's value. */
3236 c_print_token (FILE *file, int type, YYSTYPE value)
3241 parser_fprintf (file, "typed_val_int<%s, %s>",
3242 TYPE_SAFE_NAME (value.typed_val_int.type),
3243 pulongest (value.typed_val_int.val));
3249 char *copy = (char *) alloca (value.tsval.length + 1);
3251 memcpy (copy, value.tsval.ptr, value.tsval.length);
3252 copy[value.tsval.length] = '\0';
3254 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3260 parser_fprintf (file, "sval<%s>", copy_name (value.sval));
3264 parser_fprintf (file, "tsym<type=%s, name=%s>",
3265 TYPE_SAFE_NAME (value.tsym.type),
3266 copy_name (value.tsym.stoken));
3270 case UNKNOWN_CPP_NAME:
3273 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3274 copy_name (value.ssym.stoken),
3275 (value.ssym.sym.symbol == NULL
3276 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym.symbol)),
3277 value.ssym.is_a_field_of_this);
3281 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3289 yyerror (const char *msg)
3292 lexptr = prev_lexptr;
3294 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);