1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2013 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. */
39 #include "gdb_string.h"
41 #include "expression.h"
43 #include "parser-defs.h"
46 #include "bfd.h" /* Required by objfiles.h. */
47 #include "symfile.h" /* Required by objfiles.h. */
48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
51 #include "cp-support.h"
53 #include "gdb_assert.h"
54 #include "macroscope.h"
55 #include "objc-lang.h"
56 #include "typeprint.h"
59 #define parse_type builtin_type (parse_gdbarch)
61 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
62 as well as gratuitiously global symbol names, so we can have multiple
63 yacc generated parsers in gdb. Note that these are only the variables
64 produced by yacc. If other parser generators (bison, byacc, etc) produce
65 additional global names that conflict at link time, then those parser
66 generators need to be fixed instead of adding those names to this list. */
68 #define yymaxdepth c_maxdepth
69 #define yyparse c_parse_internal
71 #define yyerror c_error
74 #define yydebug c_debug
83 #define yyerrflag c_errflag
84 #define yynerrs c_nerrs
89 #define yystate c_state
95 #define yyreds c_reds /* With YYDEBUG defined */
96 #define yytoks c_toks /* With YYDEBUG defined */
97 #define yyname c_name /* With YYDEBUG defined */
98 #define yyrule c_rule /* With YYDEBUG defined */
100 #define yylen c_yylen
101 #define yydefred c_yydefred
102 #define yydgoto c_yydgoto
103 #define yysindex c_yysindex
104 #define yyrindex c_yyrindex
105 #define yygindex c_yygindex
106 #define yytable c_yytable
107 #define yycheck c_yycheck
109 #define yysslim c_yysslim
110 #define yyssp c_yyssp
111 #define yystacksize c_yystacksize
113 #define yyvsp c_yyvsp
116 #define YYDEBUG 1 /* Default to yydebug support */
119 #define YYFPRINTF parser_fprintf
123 static int yylex (void);
125 void yyerror (char *);
129 /* Although the yacc "value" of an expression is not used,
130 since the result is stored in the structure being created,
131 other node types do have values. */
147 } typed_val_decfloat;
150 struct typed_stoken tsval;
152 struct symtoken ssym;
155 enum exp_opcode opcode;
157 struct stoken_vector svec;
158 VEC (type_ptr) *tvec;
160 struct type_stack *type_stack;
162 struct objc_class_str class;
166 /* YYSTYPE gets defined by %union */
167 static int parse_number (const char *, int, int, YYSTYPE *);
168 static struct stoken operator_stoken (const char *);
169 static void check_parameter_typelist (VEC (type_ptr) *);
170 static void write_destructor_name (struct stoken);
172 static void c_print_token (FILE *file, int type, YYSTYPE value);
173 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
176 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
178 %type <tval> type typebase
179 %type <tvec> nonempty_typelist func_mod parameter_typelist
180 /* %type <bval> block */
182 /* Fancy type parsing. */
184 %type <lval> array_mod
185 %type <tval> conversion_type_id
187 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
189 %token <typed_val_int> INT
190 %token <typed_val_float> FLOAT
191 %token <typed_val_decfloat> DECFLOAT
193 /* Both NAME and TYPENAME tokens represent symbols in the input,
194 and both convey their data as strings.
195 But a TYPENAME is a string that happens to be defined as a typedef
196 or builtin type name (such as int or char)
197 and a NAME is any other symbol.
198 Contexts where this distinction is not important can use the
199 nonterminal "name", which matches either NAME or TYPENAME. */
201 %token <tsval> STRING
202 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
203 %token SELECTOR /* ObjC "@selector" pseudo-operator */
205 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
206 %token <ssym> UNKNOWN_CPP_NAME
207 %token <voidval> COMPLETE
208 %token <tsym> TYPENAME
209 %token <class> CLASSNAME /* ObjC Class name */
211 %type <svec> string_exp
212 %type <ssym> name_not_typename
213 %type <tsym> typename
215 /* This is like a '[' token, but is only generated when parsing
216 Objective C. This lets us reuse the same parser without
217 erroneously parsing ObjC-specific expressions in C. */
220 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
221 but which would parse as a valid number in the current input radix.
222 E.g. "c" when input_radix==16. Depending on the parse, it will be
223 turned into a name or into a number. */
225 %token <ssym> NAME_OR_INT
228 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
232 %type <sval> operator
233 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
239 /* Special type cases, put in to allow the parser to distinguish different
241 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
243 %token <sval> VARIABLE
245 %token <opcode> ASSIGN_MODIFY
254 %right '=' ASSIGN_MODIFY
262 %left '<' '>' LEQ GEQ
267 %right UNARY INCREMENT DECREMENT
268 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
269 %token <ssym> BLOCKNAME
270 %token <bval> FILENAME
284 { write_exp_elt_opcode(OP_TYPE);
285 write_exp_elt_type($1);
286 write_exp_elt_opcode(OP_TYPE);}
289 write_exp_elt_opcode (OP_TYPEOF);
291 | TYPEOF '(' type ')'
293 write_exp_elt_opcode (OP_TYPE);
294 write_exp_elt_type ($3);
295 write_exp_elt_opcode (OP_TYPE);
297 | DECLTYPE '(' exp ')'
299 write_exp_elt_opcode (OP_DECLTYPE);
303 /* Expressions, including the comma operator. */
306 { write_exp_elt_opcode (BINOP_COMMA); }
309 /* Expressions, not including the comma operator. */
310 exp : '*' exp %prec UNARY
311 { write_exp_elt_opcode (UNOP_IND); }
314 exp : '&' exp %prec UNARY
315 { write_exp_elt_opcode (UNOP_ADDR); }
318 exp : '-' exp %prec UNARY
319 { write_exp_elt_opcode (UNOP_NEG); }
322 exp : '+' exp %prec UNARY
323 { write_exp_elt_opcode (UNOP_PLUS); }
326 exp : '!' exp %prec UNARY
327 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
330 exp : '~' exp %prec UNARY
331 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
334 exp : INCREMENT exp %prec UNARY
335 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
338 exp : DECREMENT exp %prec UNARY
339 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
342 exp : exp INCREMENT %prec UNARY
343 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
346 exp : exp DECREMENT %prec UNARY
347 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
350 exp : TYPEID '(' exp ')' %prec UNARY
351 { write_exp_elt_opcode (OP_TYPEID); }
354 exp : TYPEID '(' type_exp ')' %prec UNARY
355 { write_exp_elt_opcode (OP_TYPEID); }
358 exp : SIZEOF exp %prec UNARY
359 { write_exp_elt_opcode (UNOP_SIZEOF); }
363 { write_exp_elt_opcode (STRUCTOP_PTR);
364 write_exp_string ($3);
365 write_exp_elt_opcode (STRUCTOP_PTR); }
368 exp : exp ARROW name COMPLETE
369 { mark_struct_expression ();
370 write_exp_elt_opcode (STRUCTOP_PTR);
371 write_exp_string ($3);
372 write_exp_elt_opcode (STRUCTOP_PTR); }
375 exp : exp ARROW COMPLETE
377 mark_struct_expression ();
378 write_exp_elt_opcode (STRUCTOP_PTR);
381 write_exp_string (s);
382 write_exp_elt_opcode (STRUCTOP_PTR); }
385 exp : exp ARROW '~' name
386 { write_exp_elt_opcode (STRUCTOP_PTR);
387 write_destructor_name ($4);
388 write_exp_elt_opcode (STRUCTOP_PTR); }
391 exp : exp ARROW '~' name COMPLETE
392 { mark_struct_expression ();
393 write_exp_elt_opcode (STRUCTOP_PTR);
394 write_destructor_name ($4);
395 write_exp_elt_opcode (STRUCTOP_PTR); }
398 exp : exp ARROW qualified_name
399 { /* exp->type::name becomes exp->*(&type::name) */
400 /* Note: this doesn't work if name is a
401 static member! FIXME */
402 write_exp_elt_opcode (UNOP_ADDR);
403 write_exp_elt_opcode (STRUCTOP_MPTR); }
406 exp : exp ARROW_STAR exp
407 { write_exp_elt_opcode (STRUCTOP_MPTR); }
411 { write_exp_elt_opcode (STRUCTOP_STRUCT);
412 write_exp_string ($3);
413 write_exp_elt_opcode (STRUCTOP_STRUCT); }
416 exp : exp '.' name COMPLETE
417 { mark_struct_expression ();
418 write_exp_elt_opcode (STRUCTOP_STRUCT);
419 write_exp_string ($3);
420 write_exp_elt_opcode (STRUCTOP_STRUCT); }
423 exp : exp '.' COMPLETE
425 mark_struct_expression ();
426 write_exp_elt_opcode (STRUCTOP_STRUCT);
429 write_exp_string (s);
430 write_exp_elt_opcode (STRUCTOP_STRUCT); }
433 exp : exp '.' '~' name
434 { write_exp_elt_opcode (STRUCTOP_STRUCT);
435 write_destructor_name ($4);
436 write_exp_elt_opcode (STRUCTOP_STRUCT); }
439 exp : exp '.' '~' name COMPLETE
440 { mark_struct_expression ();
441 write_exp_elt_opcode (STRUCTOP_STRUCT);
442 write_destructor_name ($4);
443 write_exp_elt_opcode (STRUCTOP_STRUCT); }
446 exp : exp '.' qualified_name
447 { /* exp.type::name becomes exp.*(&type::name) */
448 /* Note: this doesn't work if name is a
449 static member! FIXME */
450 write_exp_elt_opcode (UNOP_ADDR);
451 write_exp_elt_opcode (STRUCTOP_MEMBER); }
454 exp : exp DOT_STAR exp
455 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
458 exp : exp '[' exp1 ']'
459 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
462 exp : exp OBJC_LBRAC exp1 ']'
463 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
467 * The rules below parse ObjC message calls of the form:
468 * '[' target selector {':' argument}* ']'
471 exp : OBJC_LBRAC TYPENAME
475 class = lookup_objc_class (parse_gdbarch,
476 copy_name ($2.stoken));
478 error (_("%s is not an ObjC Class"),
479 copy_name ($2.stoken));
480 write_exp_elt_opcode (OP_LONG);
481 write_exp_elt_type (parse_type->builtin_int);
482 write_exp_elt_longcst ((LONGEST) class);
483 write_exp_elt_opcode (OP_LONG);
487 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
489 write_exp_elt_opcode (OP_OBJC_MSGCALL);
493 exp : OBJC_LBRAC CLASSNAME
495 write_exp_elt_opcode (OP_LONG);
496 write_exp_elt_type (parse_type->builtin_int);
497 write_exp_elt_longcst ((LONGEST) $2.class);
498 write_exp_elt_opcode (OP_LONG);
502 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
504 write_exp_elt_opcode (OP_OBJC_MSGCALL);
511 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
513 write_exp_elt_opcode (OP_OBJC_MSGCALL);
518 { add_msglist(&$1, 0); }
526 msgarg : name ':' exp
527 { add_msglist(&$1, 1); }
528 | ':' exp /* Unnamed arg. */
529 { add_msglist(0, 1); }
530 | ',' exp /* Variable number of args. */
531 { add_msglist(0, 0); }
535 /* This is to save the value of arglist_len
536 being accumulated by an outer function call. */
537 { start_arglist (); }
538 arglist ')' %prec ARROW
539 { write_exp_elt_opcode (OP_FUNCALL);
540 write_exp_elt_longcst ((LONGEST) end_arglist ());
541 write_exp_elt_opcode (OP_FUNCALL); }
544 exp : UNKNOWN_CPP_NAME '('
546 /* This could potentially be a an argument defined
547 lookup function (Koenig). */
548 write_exp_elt_opcode (OP_ADL_FUNC);
549 write_exp_elt_block (expression_context_block);
550 write_exp_elt_sym (NULL); /* Placeholder. */
551 write_exp_string ($1.stoken);
552 write_exp_elt_opcode (OP_ADL_FUNC);
554 /* This is to save the value of arglist_len
555 being accumulated by an outer function call. */
559 arglist ')' %prec ARROW
561 write_exp_elt_opcode (OP_FUNCALL);
562 write_exp_elt_longcst ((LONGEST) end_arglist ());
563 write_exp_elt_opcode (OP_FUNCALL);
568 { start_arglist (); }
578 arglist : arglist ',' exp %prec ABOVE_COMMA
582 exp : exp '(' parameter_typelist ')' const_or_volatile
584 VEC (type_ptr) *type_list = $3;
585 struct type *type_elt;
586 LONGEST len = VEC_length (type_ptr, type_list);
588 write_exp_elt_opcode (TYPE_INSTANCE);
589 write_exp_elt_longcst (len);
591 VEC_iterate (type_ptr, type_list, i, type_elt);
593 write_exp_elt_type (type_elt);
594 write_exp_elt_longcst(len);
595 write_exp_elt_opcode (TYPE_INSTANCE);
596 VEC_free (type_ptr, type_list);
601 { $$ = end_arglist () - 1; }
603 exp : lcurly arglist rcurly %prec ARROW
604 { write_exp_elt_opcode (OP_ARRAY);
605 write_exp_elt_longcst ((LONGEST) 0);
606 write_exp_elt_longcst ((LONGEST) $3);
607 write_exp_elt_opcode (OP_ARRAY); }
610 exp : lcurly type_exp rcurly exp %prec UNARY
611 { write_exp_elt_opcode (UNOP_MEMVAL_TYPE); }
614 exp : '(' type_exp ')' exp %prec UNARY
615 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
622 /* Binary operators in order of decreasing precedence. */
625 { write_exp_elt_opcode (BINOP_REPEAT); }
629 { write_exp_elt_opcode (BINOP_MUL); }
633 { write_exp_elt_opcode (BINOP_DIV); }
637 { write_exp_elt_opcode (BINOP_REM); }
641 { write_exp_elt_opcode (BINOP_ADD); }
645 { write_exp_elt_opcode (BINOP_SUB); }
649 { write_exp_elt_opcode (BINOP_LSH); }
653 { write_exp_elt_opcode (BINOP_RSH); }
657 { write_exp_elt_opcode (BINOP_EQUAL); }
660 exp : exp NOTEQUAL exp
661 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
665 { write_exp_elt_opcode (BINOP_LEQ); }
669 { write_exp_elt_opcode (BINOP_GEQ); }
673 { write_exp_elt_opcode (BINOP_LESS); }
677 { write_exp_elt_opcode (BINOP_GTR); }
681 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
685 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
689 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
693 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
697 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
700 exp : exp '?' exp ':' exp %prec '?'
701 { write_exp_elt_opcode (TERNOP_COND); }
705 { write_exp_elt_opcode (BINOP_ASSIGN); }
708 exp : exp ASSIGN_MODIFY exp
709 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
710 write_exp_elt_opcode ($2);
711 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
715 { write_exp_elt_opcode (OP_LONG);
716 write_exp_elt_type ($1.type);
717 write_exp_elt_longcst ((LONGEST)($1.val));
718 write_exp_elt_opcode (OP_LONG); }
723 struct stoken_vector vec;
726 write_exp_string_vector ($1.type, &vec);
732 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
733 write_exp_elt_opcode (OP_LONG);
734 write_exp_elt_type (val.typed_val_int.type);
735 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
736 write_exp_elt_opcode (OP_LONG);
742 { write_exp_elt_opcode (OP_DOUBLE);
743 write_exp_elt_type ($1.type);
744 write_exp_elt_dblcst ($1.dval);
745 write_exp_elt_opcode (OP_DOUBLE); }
749 { write_exp_elt_opcode (OP_DECFLOAT);
750 write_exp_elt_type ($1.type);
751 write_exp_elt_decfloatcst ($1.val);
752 write_exp_elt_opcode (OP_DECFLOAT); }
760 write_dollar_variable ($1);
764 exp : SELECTOR '(' name ')'
766 write_exp_elt_opcode (OP_OBJC_SELECTOR);
767 write_exp_string ($3);
768 write_exp_elt_opcode (OP_OBJC_SELECTOR); }
771 exp : SIZEOF '(' type ')' %prec UNARY
772 { write_exp_elt_opcode (OP_LONG);
773 write_exp_elt_type (lookup_signed_typename
774 (parse_language, parse_gdbarch,
777 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
778 write_exp_elt_opcode (OP_LONG); }
781 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
782 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
785 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
786 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
789 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
790 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
793 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
794 { /* We could do more error checking here, but
795 it doesn't seem worthwhile. */
796 write_exp_elt_opcode (UNOP_CAST_TYPE); }
802 /* We copy the string here, and not in the
803 lexer, to guarantee that we do not leak a
804 string. Note that we follow the
805 NUL-termination convention of the
807 struct typed_stoken *vec = XNEW (struct typed_stoken);
812 vec->length = $1.length;
813 vec->ptr = malloc ($1.length + 1);
814 memcpy (vec->ptr, $1.ptr, $1.length + 1);
819 /* Note that we NUL-terminate here, but just
823 $$.tokens = realloc ($$.tokens,
824 $$.len * sizeof (struct typed_stoken));
826 p = malloc ($2.length + 1);
827 memcpy (p, $2.ptr, $2.length + 1);
829 $$.tokens[$$.len - 1].type = $2.type;
830 $$.tokens[$$.len - 1].length = $2.length;
831 $$.tokens[$$.len - 1].ptr = p;
838 enum c_string_type type = C_STRING;
840 for (i = 0; i < $1.len; ++i)
842 switch ($1.tokens[i].type)
850 && type != $1.tokens[i].type)
851 error (_("Undefined string concatenation."));
852 type = $1.tokens[i].type;
856 internal_error (__FILE__, __LINE__,
857 "unrecognized type in string concatenation");
861 write_exp_string_vector (type, &$1);
862 for (i = 0; i < $1.len; ++i)
863 free ($1.tokens[i].ptr);
868 exp : NSSTRING /* ObjC NextStep NSString constant
869 * of the form '@' '"' string '"'.
871 { write_exp_elt_opcode (OP_OBJC_NSSTRING);
872 write_exp_string ($1);
873 write_exp_elt_opcode (OP_OBJC_NSSTRING); }
878 { write_exp_elt_opcode (OP_LONG);
879 write_exp_elt_type (parse_type->builtin_bool);
880 write_exp_elt_longcst ((LONGEST) 1);
881 write_exp_elt_opcode (OP_LONG); }
885 { write_exp_elt_opcode (OP_LONG);
886 write_exp_elt_type (parse_type->builtin_bool);
887 write_exp_elt_longcst ((LONGEST) 0);
888 write_exp_elt_opcode (OP_LONG); }
896 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
898 error (_("No file or function \"%s\"."),
899 copy_name ($1.stoken));
907 block : block COLONCOLON name
909 = lookup_symbol (copy_name ($3), $1,
911 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
912 error (_("No function \"%s\" in specified context."),
914 $$ = SYMBOL_BLOCK_VALUE (tem); }
917 variable: name_not_typename ENTRY
918 { struct symbol *sym = $1.sym;
920 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
921 || !symbol_read_needs_frame (sym))
922 error (_("@entry can be used only for function "
923 "parameters, not for \"%s\""),
924 copy_name ($1.stoken));
926 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
927 write_exp_elt_sym (sym);
928 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
932 variable: block COLONCOLON name
933 { struct symbol *sym;
934 sym = lookup_symbol (copy_name ($3), $1,
937 error (_("No symbol \"%s\" in specified context."),
939 if (symbol_read_needs_frame (sym))
941 if (innermost_block == 0
942 || contained_in (block_found,
944 innermost_block = block_found;
947 write_exp_elt_opcode (OP_VAR_VALUE);
948 /* block_found is set by lookup_symbol. */
949 write_exp_elt_block (block_found);
950 write_exp_elt_sym (sym);
951 write_exp_elt_opcode (OP_VAR_VALUE); }
954 qualified_name: TYPENAME COLONCOLON name
956 struct type *type = $1.type;
957 CHECK_TYPEDEF (type);
958 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
959 && TYPE_CODE (type) != TYPE_CODE_UNION
960 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
961 error (_("`%s' is not defined as an aggregate type."),
962 TYPE_SAFE_NAME (type));
964 write_exp_elt_opcode (OP_SCOPE);
965 write_exp_elt_type (type);
966 write_exp_string ($3);
967 write_exp_elt_opcode (OP_SCOPE);
969 | TYPENAME COLONCOLON '~' name
971 struct type *type = $1.type;
972 struct stoken tmp_token;
975 CHECK_TYPEDEF (type);
976 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
977 && TYPE_CODE (type) != TYPE_CODE_UNION
978 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
979 error (_("`%s' is not defined as an aggregate type."),
980 TYPE_SAFE_NAME (type));
981 buf = alloca ($4.length + 2);
983 tmp_token.length = $4.length + 1;
985 memcpy (buf+1, $4.ptr, $4.length);
986 buf[tmp_token.length] = 0;
988 /* Check for valid destructor name. */
989 destructor_name_p (tmp_token.ptr, $1.type);
990 write_exp_elt_opcode (OP_SCOPE);
991 write_exp_elt_type (type);
992 write_exp_string (tmp_token);
993 write_exp_elt_opcode (OP_SCOPE);
995 | TYPENAME COLONCOLON name COLONCOLON name
997 char *copy = copy_name ($3);
998 error (_("No type \"%s\" within class "
999 "or namespace \"%s\"."),
1000 copy, TYPE_SAFE_NAME ($1.type));
1004 variable: qualified_name
1005 | COLONCOLON name_not_typename
1007 char *name = copy_name ($2.stoken);
1009 struct bound_minimal_symbol msymbol;
1012 lookup_symbol (name, (const struct block *) NULL,
1016 write_exp_elt_opcode (OP_VAR_VALUE);
1017 write_exp_elt_block (NULL);
1018 write_exp_elt_sym (sym);
1019 write_exp_elt_opcode (OP_VAR_VALUE);
1023 msymbol = lookup_bound_minimal_symbol (name);
1024 if (msymbol.minsym != NULL)
1025 write_exp_msymbol (msymbol);
1026 else if (!have_full_symbols () && !have_partial_symbols ())
1027 error (_("No symbol table is loaded. Use the \"file\" command."));
1029 error (_("No symbol \"%s\" in current context."), name);
1033 variable: name_not_typename
1034 { struct symbol *sym = $1.sym;
1038 if (symbol_read_needs_frame (sym))
1040 if (innermost_block == 0
1041 || contained_in (block_found,
1043 innermost_block = block_found;
1046 write_exp_elt_opcode (OP_VAR_VALUE);
1047 /* We want to use the selected frame, not
1048 another more inner frame which happens to
1049 be in the same block. */
1050 write_exp_elt_block (NULL);
1051 write_exp_elt_sym (sym);
1052 write_exp_elt_opcode (OP_VAR_VALUE);
1054 else if ($1.is_a_field_of_this)
1056 /* C++: it hangs off of `this'. Must
1057 not inadvertently convert from a method call
1059 if (innermost_block == 0
1060 || contained_in (block_found,
1062 innermost_block = block_found;
1063 write_exp_elt_opcode (OP_THIS);
1064 write_exp_elt_opcode (OP_THIS);
1065 write_exp_elt_opcode (STRUCTOP_PTR);
1066 write_exp_string ($1.stoken);
1067 write_exp_elt_opcode (STRUCTOP_PTR);
1071 struct bound_minimal_symbol msymbol;
1072 char *arg = copy_name ($1.stoken);
1075 lookup_bound_minimal_symbol (arg);
1076 if (msymbol.minsym != NULL)
1077 write_exp_msymbol (msymbol);
1078 else if (!have_full_symbols () && !have_partial_symbols ())
1079 error (_("No symbol table is loaded. Use the \"file\" command."));
1081 error (_("No symbol \"%s\" in current context."),
1082 copy_name ($1.stoken));
1087 space_identifier : '@' NAME
1088 { insert_type_address_space (copy_name ($2.stoken)); }
1091 const_or_volatile: const_or_volatile_noopt
1095 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1098 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1099 | const_or_volatile_noopt
1102 const_or_volatile_or_space_identifier:
1103 const_or_volatile_or_space_identifier_noopt
1109 { insert_type (tp_pointer); }
1110 const_or_volatile_or_space_identifier
1112 { insert_type (tp_pointer); }
1113 const_or_volatile_or_space_identifier
1115 { insert_type (tp_reference); }
1117 { insert_type (tp_reference); }
1120 ptr_operator_ts: ptr_operator
1122 $$ = get_type_stack ();
1123 /* This cleanup is eventually run by
1125 make_cleanup (type_stack_cleanup, $$);
1129 abs_decl: ptr_operator_ts direct_abs_decl
1130 { $$ = append_type_stack ($2, $1); }
1135 direct_abs_decl: '(' abs_decl ')'
1137 | direct_abs_decl array_mod
1139 push_type_stack ($1);
1141 push_type (tp_array);
1142 $$ = get_type_stack ();
1147 push_type (tp_array);
1148 $$ = get_type_stack ();
1151 | direct_abs_decl func_mod
1153 push_type_stack ($1);
1155 $$ = get_type_stack ();
1160 $$ = get_type_stack ();
1170 | OBJC_LBRAC INT ']'
1176 | '(' parameter_typelist ')'
1180 /* We used to try to recognize pointer to member types here, but
1181 that didn't work (shift/reduce conflicts meant that these rules never
1182 got executed). The problem is that
1183 int (foo::bar::baz::bizzle)
1184 is a function type but
1185 int (foo::bar::baz::bizzle::*)
1186 is a pointer to member type. Stroustrup loses again! */
1191 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1195 { $$ = lookup_signed_typename (parse_language,
1199 { $$ = lookup_signed_typename (parse_language,
1203 { $$ = lookup_signed_typename (parse_language,
1207 { $$ = lookup_signed_typename (parse_language,
1210 | LONG SIGNED_KEYWORD INT_KEYWORD
1211 { $$ = lookup_signed_typename (parse_language,
1214 | LONG SIGNED_KEYWORD
1215 { $$ = lookup_signed_typename (parse_language,
1218 | SIGNED_KEYWORD LONG INT_KEYWORD
1219 { $$ = lookup_signed_typename (parse_language,
1222 | UNSIGNED LONG INT_KEYWORD
1223 { $$ = lookup_unsigned_typename (parse_language,
1226 | LONG UNSIGNED INT_KEYWORD
1227 { $$ = lookup_unsigned_typename (parse_language,
1231 { $$ = lookup_unsigned_typename (parse_language,
1235 { $$ = lookup_signed_typename (parse_language,
1238 | LONG LONG INT_KEYWORD
1239 { $$ = lookup_signed_typename (parse_language,
1242 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1243 { $$ = lookup_signed_typename (parse_language,
1246 | LONG LONG SIGNED_KEYWORD
1247 { $$ = lookup_signed_typename (parse_language,
1250 | SIGNED_KEYWORD LONG LONG
1251 { $$ = lookup_signed_typename (parse_language,
1254 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1255 { $$ = lookup_signed_typename (parse_language,
1258 | UNSIGNED LONG LONG
1259 { $$ = lookup_unsigned_typename (parse_language,
1262 | UNSIGNED LONG LONG INT_KEYWORD
1263 { $$ = lookup_unsigned_typename (parse_language,
1266 | LONG LONG UNSIGNED
1267 { $$ = lookup_unsigned_typename (parse_language,
1270 | LONG LONG UNSIGNED INT_KEYWORD
1271 { $$ = lookup_unsigned_typename (parse_language,
1275 { $$ = lookup_signed_typename (parse_language,
1278 | SHORT SIGNED_KEYWORD INT_KEYWORD
1279 { $$ = lookup_signed_typename (parse_language,
1282 | SHORT SIGNED_KEYWORD
1283 { $$ = lookup_signed_typename (parse_language,
1286 | UNSIGNED SHORT INT_KEYWORD
1287 { $$ = lookup_unsigned_typename (parse_language,
1291 { $$ = lookup_unsigned_typename (parse_language,
1294 | SHORT UNSIGNED INT_KEYWORD
1295 { $$ = lookup_unsigned_typename (parse_language,
1299 { $$ = lookup_typename (parse_language, parse_gdbarch,
1300 "double", (struct block *) NULL,
1302 | LONG DOUBLE_KEYWORD
1303 { $$ = lookup_typename (parse_language, parse_gdbarch,
1305 (struct block *) NULL, 0); }
1307 { $$ = lookup_struct (copy_name ($2),
1308 expression_context_block); }
1311 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1314 | STRUCT name COMPLETE
1316 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1321 { $$ = lookup_struct (copy_name ($2),
1322 expression_context_block); }
1325 mark_completion_tag (TYPE_CODE_CLASS, "", 0);
1328 | CLASS name COMPLETE
1330 mark_completion_tag (TYPE_CODE_CLASS, $2.ptr,
1335 { $$ = lookup_union (copy_name ($2),
1336 expression_context_block); }
1339 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1342 | UNION name COMPLETE
1344 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1349 { $$ = lookup_enum (copy_name ($2),
1350 expression_context_block); }
1353 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1356 | ENUM name COMPLETE
1358 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1363 { $$ = lookup_unsigned_typename (parse_language,
1365 TYPE_NAME($2.type)); }
1367 { $$ = lookup_unsigned_typename (parse_language,
1370 | SIGNED_KEYWORD typename
1371 { $$ = lookup_signed_typename (parse_language,
1373 TYPE_NAME($2.type)); }
1375 { $$ = lookup_signed_typename (parse_language,
1378 /* It appears that this rule for templates is never
1379 reduced; template recognition happens by lookahead
1380 in the token processing code in yylex. */
1381 | TEMPLATE name '<' type '>'
1382 { $$ = lookup_template_type(copy_name($2), $4,
1383 expression_context_block);
1385 | const_or_volatile_or_space_identifier_noopt typebase
1386 { $$ = follow_types ($2); }
1387 | typebase const_or_volatile_or_space_identifier_noopt
1388 { $$ = follow_types ($1); }
1394 $$.stoken.ptr = "int";
1395 $$.stoken.length = 3;
1396 $$.type = lookup_signed_typename (parse_language,
1402 $$.stoken.ptr = "long";
1403 $$.stoken.length = 4;
1404 $$.type = lookup_signed_typename (parse_language,
1410 $$.stoken.ptr = "short";
1411 $$.stoken.length = 5;
1412 $$.type = lookup_signed_typename (parse_language,
1420 { check_parameter_typelist ($1); }
1421 | nonempty_typelist ',' DOTDOTDOT
1423 VEC_safe_push (type_ptr, $1, NULL);
1424 check_parameter_typelist ($1);
1432 VEC (type_ptr) *typelist = NULL;
1433 VEC_safe_push (type_ptr, typelist, $1);
1436 | nonempty_typelist ',' type
1438 VEC_safe_push (type_ptr, $1, $3);
1446 push_type_stack ($2);
1447 $$ = follow_types ($1);
1451 conversion_type_id: typebase conversion_declarator
1452 { $$ = follow_types ($1); }
1455 conversion_declarator: /* Nothing. */
1456 | ptr_operator conversion_declarator
1459 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1460 | VOLATILE_KEYWORD CONST_KEYWORD
1463 const_or_volatile_noopt: const_and_volatile
1464 { insert_type (tp_const);
1465 insert_type (tp_volatile);
1468 { insert_type (tp_const); }
1470 { insert_type (tp_volatile); }
1473 operator: OPERATOR NEW
1474 { $$ = operator_stoken (" new"); }
1476 { $$ = operator_stoken (" delete"); }
1477 | OPERATOR NEW '[' ']'
1478 { $$ = operator_stoken (" new[]"); }
1479 | OPERATOR DELETE '[' ']'
1480 { $$ = operator_stoken (" delete[]"); }
1481 | OPERATOR NEW OBJC_LBRAC ']'
1482 { $$ = operator_stoken (" new[]"); }
1483 | OPERATOR DELETE OBJC_LBRAC ']'
1484 { $$ = operator_stoken (" delete[]"); }
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 ("<"); }
1510 { $$ = operator_stoken (">"); }
1511 | OPERATOR ASSIGN_MODIFY
1512 { const char *op = "unknown";
1536 case BINOP_BITWISE_IOR:
1539 case BINOP_BITWISE_AND:
1542 case BINOP_BITWISE_XOR:
1549 $$ = operator_stoken (op);
1552 { $$ = operator_stoken ("<<"); }
1554 { $$ = operator_stoken (">>"); }
1556 { $$ = operator_stoken ("=="); }
1558 { $$ = operator_stoken ("!="); }
1560 { $$ = operator_stoken ("<="); }
1562 { $$ = operator_stoken (">="); }
1564 { $$ = operator_stoken ("&&"); }
1566 { $$ = operator_stoken ("||"); }
1567 | OPERATOR INCREMENT
1568 { $$ = operator_stoken ("++"); }
1569 | OPERATOR DECREMENT
1570 { $$ = operator_stoken ("--"); }
1572 { $$ = operator_stoken (","); }
1573 | OPERATOR ARROW_STAR
1574 { $$ = operator_stoken ("->*"); }
1576 { $$ = operator_stoken ("->"); }
1578 { $$ = operator_stoken ("()"); }
1580 { $$ = operator_stoken ("[]"); }
1581 | OPERATOR OBJC_LBRAC ']'
1582 { $$ = operator_stoken ("[]"); }
1583 | OPERATOR conversion_type_id
1586 struct ui_file *buf = mem_fileopen ();
1588 c_print_type ($2, NULL, buf, -1, 0,
1589 &type_print_raw_options);
1590 name = ui_file_xstrdup (buf, &length);
1591 ui_file_delete (buf);
1592 $$ = operator_stoken (name);
1599 name : NAME { $$ = $1.stoken; }
1600 | BLOCKNAME { $$ = $1.stoken; }
1601 | TYPENAME { $$ = $1.stoken; }
1602 | NAME_OR_INT { $$ = $1.stoken; }
1603 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1604 | operator { $$ = $1; }
1607 name_not_typename : NAME
1609 /* These would be useful if name_not_typename was useful, but it is just
1610 a fake for "variable", so these cause reduce/reduce conflicts because
1611 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1612 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1613 context where only a name could occur, this might be useful.
1618 struct field_of_this_result is_a_field_of_this;
1621 $$.sym = lookup_symbol ($1.ptr,
1622 expression_context_block,
1624 &is_a_field_of_this);
1625 $$.is_a_field_of_this
1626 = is_a_field_of_this.type != NULL;
1633 /* Like write_exp_string, but prepends a '~'. */
1636 write_destructor_name (struct stoken token)
1638 char *copy = alloca (token.length + 1);
1641 memcpy (©[1], token.ptr, token.length);
1646 write_exp_string (token);
1649 /* Returns a stoken of the operator name given by OP (which does not
1650 include the string "operator"). */
1651 static struct stoken
1652 operator_stoken (const char *op)
1654 static const char *operator_string = "operator";
1655 struct stoken st = { NULL, 0 };
1658 st.length = strlen (operator_string) + strlen (op);
1659 buf = malloc (st.length + 1);
1660 strcpy (buf, operator_string);
1664 /* The toplevel (c_parse) will free the memory allocated here. */
1665 make_cleanup (free, buf);
1669 /* Validate a parameter typelist. */
1672 check_parameter_typelist (VEC (type_ptr) *params)
1677 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1679 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1683 if (VEC_length (type_ptr, params) == 1)
1688 VEC_free (type_ptr, params);
1689 error (_("parameter types following 'void'"));
1693 VEC_free (type_ptr, params);
1694 error (_("'void' invalid as parameter type"));
1700 /* Take care of parsing a number (anything that starts with a digit).
1701 Set yylval and return the token type; update lexptr.
1702 LEN is the number of characters in it. */
1704 /*** Needs some error checking for the float case ***/
1707 parse_number (const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1709 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1710 here, and we do kind of silly things like cast to unsigned. */
1717 int base = input_radix;
1720 /* Number of "L" suffixes encountered. */
1723 /* We have found a "L" or "U" suffix. */
1724 int found_suffix = 0;
1727 struct type *signed_type;
1728 struct type *unsigned_type;
1732 memcpy (p, buf, len);
1736 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1737 point. Return DECFLOAT. */
1739 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1742 putithere->typed_val_decfloat.type
1743 = parse_type->builtin_decfloat;
1744 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1745 gdbarch_byte_order (parse_gdbarch), p);
1750 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1753 putithere->typed_val_decfloat.type
1754 = parse_type->builtin_decdouble;
1755 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1756 gdbarch_byte_order (parse_gdbarch), p);
1761 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1764 putithere->typed_val_decfloat.type
1765 = parse_type->builtin_declong;
1766 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1767 gdbarch_byte_order (parse_gdbarch), p);
1772 if (! parse_c_float (parse_gdbarch, p, len,
1773 &putithere->typed_val_float.dval,
1774 &putithere->typed_val_float.type))
1779 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1823 if (c >= 'A' && c <= 'Z')
1825 if (c != 'l' && c != 'u')
1827 if (c >= '0' && c <= '9')
1835 if (base > 10 && c >= 'a' && c <= 'f')
1839 n += i = c - 'a' + 10;
1852 return ERROR; /* Char not a digit */
1855 return ERROR; /* Invalid digit in this base */
1857 /* Portably test for overflow (only works for nonzero values, so make
1858 a second check for zero). FIXME: Can't we just make n and prevn
1859 unsigned and avoid this? */
1860 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1861 unsigned_p = 1; /* Try something unsigned */
1863 /* Portably test for unsigned overflow.
1864 FIXME: This check is wrong; for example it doesn't find overflow
1865 on 0x123456789 when LONGEST is 32 bits. */
1866 if (c != 'l' && c != 'u' && n != 0)
1868 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1869 error (_("Numeric constant too large."));
1874 /* An integer constant is an int, a long, or a long long. An L
1875 suffix forces it to be long; an LL suffix forces it to be long
1876 long. If not forced to a larger size, it gets the first type of
1877 the above that it fits in. To figure out whether it fits, we
1878 shift it right and see whether anything remains. Note that we
1879 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1880 operation, because many compilers will warn about such a shift
1881 (which always produces a zero result). Sometimes gdbarch_int_bit
1882 or gdbarch_long_bit will be that big, sometimes not. To deal with
1883 the case where it is we just always shift the value more than
1884 once, with fewer bits each time. */
1886 un = (ULONGEST)n >> 2;
1888 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1890 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1892 /* A large decimal (not hex or octal) constant (between INT_MAX
1893 and UINT_MAX) is a long or unsigned long, according to ANSI,
1894 never an unsigned int, but this code treats it as unsigned
1895 int. This probably should be fixed. GCC gives a warning on
1898 unsigned_type = parse_type->builtin_unsigned_int;
1899 signed_type = parse_type->builtin_int;
1901 else if (long_p <= 1
1902 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1904 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1905 unsigned_type = parse_type->builtin_unsigned_long;
1906 signed_type = parse_type->builtin_long;
1911 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1912 < gdbarch_long_long_bit (parse_gdbarch))
1913 /* A long long does not fit in a LONGEST. */
1914 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1916 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1917 high_bit = (ULONGEST) 1 << shift;
1918 unsigned_type = parse_type->builtin_unsigned_long_long;
1919 signed_type = parse_type->builtin_long_long;
1922 putithere->typed_val_int.val = n;
1924 /* If the high bit of the worked out type is set then this number
1925 has to be unsigned. */
1927 if (unsigned_p || (n & high_bit))
1929 putithere->typed_val_int.type = unsigned_type;
1933 putithere->typed_val_int.type = signed_type;
1939 /* Temporary obstack used for holding strings. */
1940 static struct obstack tempbuf;
1941 static int tempbuf_init;
1943 /* Parse a C escape sequence. The initial backslash of the sequence
1944 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1945 last character of the sequence. If OUTPUT is not NULL, the
1946 translated form of the escape sequence will be written there. If
1947 OUTPUT is NULL, no output is written and the call will only affect
1948 *PTR. If an escape sequence is expressed in target bytes, then the
1949 entire sequence will simply be copied to OUTPUT. Return 1 if any
1950 character was emitted, 0 otherwise. */
1953 c_parse_escape (const char **ptr, struct obstack *output)
1955 const char *tokptr = *ptr;
1958 /* Some escape sequences undergo character set conversion. Those we
1962 /* Hex escapes do not undergo character set conversion, so keep
1963 the escape sequence for later. */
1966 obstack_grow_str (output, "\\x");
1968 if (!isxdigit (*tokptr))
1969 error (_("\\x escape without a following hex digit"));
1970 while (isxdigit (*tokptr))
1973 obstack_1grow (output, *tokptr);
1978 /* Octal escapes do not undergo character set conversion, so
1979 keep the escape sequence for later. */
1991 obstack_grow_str (output, "\\");
1993 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1997 obstack_1grow (output, *tokptr);
2003 /* We handle UCNs later. We could handle them here, but that
2004 would mean a spurious error in the case where the UCN could
2005 be converted to the target charset but not the host
2011 int i, len = c == 'U' ? 8 : 4;
2014 obstack_1grow (output, '\\');
2015 obstack_1grow (output, *tokptr);
2018 if (!isxdigit (*tokptr))
2019 error (_("\\%c escape without a following hex digit"), c);
2020 for (i = 0; i < len && isxdigit (*tokptr); ++i)
2023 obstack_1grow (output, *tokptr);
2029 /* We must pass backslash through so that it does not
2030 cause quoting during the second expansion. */
2033 obstack_grow_str (output, "\\\\");
2037 /* Escapes which undergo conversion. */
2040 obstack_1grow (output, '\a');
2045 obstack_1grow (output, '\b');
2050 obstack_1grow (output, '\f');
2055 obstack_1grow (output, '\n');
2060 obstack_1grow (output, '\r');
2065 obstack_1grow (output, '\t');
2070 obstack_1grow (output, '\v');
2074 /* GCC extension. */
2077 obstack_1grow (output, HOST_ESCAPE_CHAR);
2081 /* Backslash-newline expands to nothing at all. */
2087 /* A few escapes just expand to the character itself. */
2091 /* GCC extensions. */
2096 /* Unrecognized escapes turn into the character itself. */
2099 obstack_1grow (output, *tokptr);
2107 /* Parse a string or character literal from TOKPTR. The string or
2108 character may be wide or unicode. *OUTPTR is set to just after the
2109 end of the literal in the input string. The resulting token is
2110 stored in VALUE. This returns a token value, either STRING or
2111 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2112 number of host characters in the literal. */
2114 parse_string_or_char (const char *tokptr, const char **outptr,
2115 struct typed_stoken *value, int *host_chars)
2118 enum c_string_type type;
2121 /* Build the gdb internal form of the input string in tempbuf. Note
2122 that the buffer is null byte terminated *only* for the
2123 convenience of debugging gdb itself and printing the buffer
2124 contents when the buffer contains no embedded nulls. Gdb does
2125 not depend upon the buffer being null byte terminated, it uses
2126 the length string instead. This allows gdb to handle C strings
2127 (as well as strings in other languages) with embedded null
2133 obstack_free (&tempbuf, NULL);
2134 obstack_init (&tempbuf);
2136 /* Record the string type. */
2139 type = C_WIDE_STRING;
2142 else if (*tokptr == 'u')
2147 else if (*tokptr == 'U')
2152 else if (*tokptr == '@')
2154 /* An Objective C string. */
2162 /* Skip the quote. */
2176 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2178 else if (c == quote)
2182 obstack_1grow (&tempbuf, c);
2184 /* FIXME: this does the wrong thing with multi-byte host
2185 characters. We could use mbrlen here, but that would
2186 make "set host-charset" a bit less useful. */
2191 if (*tokptr != quote)
2194 error (_("Unterminated string in expression."));
2196 error (_("Unmatched single quote."));
2201 value->ptr = obstack_base (&tempbuf);
2202 value->length = obstack_object_size (&tempbuf);
2206 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2209 /* This is used to associate some attributes with a token. */
2213 /* If this bit is set, the token is C++-only. */
2217 /* If this bit is set, the token is conditional: if there is a
2218 symbol of the same name, then the token is a symbol; otherwise,
2219 the token is a keyword. */
2228 enum exp_opcode opcode;
2229 enum token_flags flags;
2232 static const struct token tokentab3[] =
2234 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2235 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2236 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2237 {"...", DOTDOTDOT, BINOP_END, 0}
2240 static const struct token tokentab2[] =
2242 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2243 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2244 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2245 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2246 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2247 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2248 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2249 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2250 {"++", INCREMENT, BINOP_END, 0},
2251 {"--", DECREMENT, BINOP_END, 0},
2252 {"->", ARROW, BINOP_END, 0},
2253 {"&&", ANDAND, BINOP_END, 0},
2254 {"||", OROR, BINOP_END, 0},
2255 /* "::" is *not* only C++: gdb overrides its meaning in several
2256 different ways, e.g., 'filename'::func, function::variable. */
2257 {"::", COLONCOLON, BINOP_END, 0},
2258 {"<<", LSH, BINOP_END, 0},
2259 {">>", RSH, BINOP_END, 0},
2260 {"==", EQUAL, BINOP_END, 0},
2261 {"!=", NOTEQUAL, BINOP_END, 0},
2262 {"<=", LEQ, BINOP_END, 0},
2263 {">=", GEQ, BINOP_END, 0},
2264 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2267 /* Identifier-like tokens. */
2268 static const struct token ident_tokens[] =
2270 {"unsigned", UNSIGNED, OP_NULL, 0},
2271 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2272 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2273 {"struct", STRUCT, OP_NULL, 0},
2274 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2275 {"sizeof", SIZEOF, OP_NULL, 0},
2276 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2277 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2278 {"class", CLASS, OP_NULL, FLAG_CXX},
2279 {"union", UNION, OP_NULL, 0},
2280 {"short", SHORT, OP_NULL, 0},
2281 {"const", CONST_KEYWORD, OP_NULL, 0},
2282 {"enum", ENUM, OP_NULL, 0},
2283 {"long", LONG, OP_NULL, 0},
2284 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2285 {"int", INT_KEYWORD, OP_NULL, 0},
2286 {"new", NEW, OP_NULL, FLAG_CXX},
2287 {"delete", DELETE, OP_NULL, FLAG_CXX},
2288 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2290 {"and", ANDAND, BINOP_END, FLAG_CXX},
2291 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2292 {"bitand", '&', OP_NULL, FLAG_CXX},
2293 {"bitor", '|', OP_NULL, FLAG_CXX},
2294 {"compl", '~', OP_NULL, FLAG_CXX},
2295 {"not", '!', OP_NULL, FLAG_CXX},
2296 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2297 {"or", OROR, BINOP_END, FLAG_CXX},
2298 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2299 {"xor", '^', OP_NULL, FLAG_CXX},
2300 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2302 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2303 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2304 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2305 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2307 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2308 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2309 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2310 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2311 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2313 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2316 /* When we find that lexptr (the global var defined in parse.c) is
2317 pointing at a macro invocation, we expand the invocation, and call
2318 scan_macro_expansion to save the old lexptr here and point lexptr
2319 into the expanded text. When we reach the end of that, we call
2320 end_macro_expansion to pop back to the value we saved here. The
2321 macro expansion code promises to return only fully-expanded text,
2322 so we don't need to "push" more than one level.
2324 This is disgusting, of course. It would be cleaner to do all macro
2325 expansion beforehand, and then hand that to lexptr. But we don't
2326 really know where the expression ends. Remember, in a command like
2328 (gdb) break *ADDRESS if CONDITION
2330 we evaluate ADDRESS in the scope of the current frame, but we
2331 evaluate CONDITION in the scope of the breakpoint's location. So
2332 it's simply wrong to try to macro-expand the whole thing at once. */
2333 static const char *macro_original_text;
2335 /* We save all intermediate macro expansions on this obstack for the
2336 duration of a single parse. The expansion text may sometimes have
2337 to live past the end of the expansion, due to yacc lookahead.
2338 Rather than try to be clever about saving the data for a single
2339 token, we simply keep it all and delete it after parsing has
2341 static struct obstack expansion_obstack;
2344 scan_macro_expansion (char *expansion)
2348 /* We'd better not be trying to push the stack twice. */
2349 gdb_assert (! macro_original_text);
2351 /* Copy to the obstack, and then free the intermediate
2353 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2356 /* Save the old lexptr value, so we can return to it when we're done
2357 parsing the expanded text. */
2358 macro_original_text = lexptr;
2364 scanning_macro_expansion (void)
2366 return macro_original_text != 0;
2371 finished_macro_expansion (void)
2373 /* There'd better be something to pop back to. */
2374 gdb_assert (macro_original_text);
2376 /* Pop back to the original text. */
2377 lexptr = macro_original_text;
2378 macro_original_text = 0;
2383 scan_macro_cleanup (void *dummy)
2385 if (macro_original_text)
2386 finished_macro_expansion ();
2388 obstack_free (&expansion_obstack, NULL);
2391 /* Return true iff the token represents a C++ cast operator. */
2394 is_cast_operator (const char *token, int len)
2396 return (! strncmp (token, "dynamic_cast", len)
2397 || ! strncmp (token, "static_cast", len)
2398 || ! strncmp (token, "reinterpret_cast", len)
2399 || ! strncmp (token, "const_cast", len));
2402 /* The scope used for macro expansion. */
2403 static struct macro_scope *expression_macro_scope;
2405 /* This is set if a NAME token appeared at the very end of the input
2406 string, with no whitespace separating the name from the EOF. This
2407 is used only when parsing to do field name completion. */
2408 static int saw_name_at_eof;
2410 /* This is set if the previously-returned token was a structure
2411 operator -- either '.' or ARROW. This is used only when parsing to
2412 do field name completion. */
2413 static int last_was_structop;
2415 /* Read one token, getting characters through lexptr. */
2418 lex_one_token (void)
2423 const char *tokstart;
2424 int saw_structop = last_was_structop;
2427 last_was_structop = 0;
2431 /* Check if this is a macro invocation that we need to expand. */
2432 if (! scanning_macro_expansion ())
2434 char *expanded = macro_expand_next (&lexptr,
2435 standard_macro_lookup,
2436 expression_macro_scope);
2439 scan_macro_expansion (expanded);
2442 prev_lexptr = lexptr;
2445 /* See if it is a special token of length 3. */
2446 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2447 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2449 if ((tokentab3[i].flags & FLAG_CXX) != 0
2450 && parse_language->la_language != language_cplus)
2454 yylval.opcode = tokentab3[i].opcode;
2455 return tokentab3[i].token;
2458 /* See if it is a special token of length 2. */
2459 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2460 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2462 if ((tokentab2[i].flags & FLAG_CXX) != 0
2463 && parse_language->la_language != language_cplus)
2467 yylval.opcode = tokentab2[i].opcode;
2468 if (parse_completion && tokentab2[i].token == ARROW)
2469 last_was_structop = 1;
2470 return tokentab2[i].token;
2473 switch (c = *tokstart)
2476 /* If we were just scanning the result of a macro expansion,
2477 then we need to resume scanning the original text.
2478 If we're parsing for field name completion, and the previous
2479 token allows such completion, return a COMPLETE token.
2480 Otherwise, we were already scanning the original text, and
2481 we're really done. */
2482 if (scanning_macro_expansion ())
2484 finished_macro_expansion ();
2487 else if (saw_name_at_eof)
2489 saw_name_at_eof = 0;
2492 else if (saw_structop)
2507 if (parse_language->la_language == language_objc && c == '[')
2513 if (paren_depth == 0)
2520 if (comma_terminates
2522 && ! scanning_macro_expansion ())
2528 /* Might be a floating point number. */
2529 if (lexptr[1] < '0' || lexptr[1] > '9')
2531 if (parse_completion)
2532 last_was_structop = 1;
2533 goto symbol; /* Nope, must be a symbol. */
2535 /* FALL THRU into number case. */
2548 /* It's a number. */
2549 int got_dot = 0, got_e = 0, toktype;
2550 const char *p = tokstart;
2551 int hex = input_radix > 10;
2553 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2558 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2566 /* This test includes !hex because 'e' is a valid hex digit
2567 and thus does not indicate a floating point number when
2568 the radix is hex. */
2569 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2570 got_dot = got_e = 1;
2571 /* This test does not include !hex, because a '.' always indicates
2572 a decimal floating point number regardless of the radix. */
2573 else if (!got_dot && *p == '.')
2575 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2576 && (*p == '-' || *p == '+'))
2577 /* This is the sign of the exponent, not the end of the
2580 /* We will take any letters or digits. parse_number will
2581 complain if past the radix, or if L or U are not final. */
2582 else if ((*p < '0' || *p > '9')
2583 && ((*p < 'a' || *p > 'z')
2584 && (*p < 'A' || *p > 'Z')))
2587 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2588 if (toktype == ERROR)
2590 char *err_copy = (char *) alloca (p - tokstart + 1);
2592 memcpy (err_copy, tokstart, p - tokstart);
2593 err_copy[p - tokstart] = 0;
2594 error (_("Invalid number \"%s\"."), err_copy);
2602 const char *p = &tokstart[1];
2603 size_t len = strlen ("entry");
2605 if (parse_language->la_language == language_objc)
2607 size_t len = strlen ("selector");
2609 if (strncmp (p, "selector", len) == 0
2610 && (p[len] == '\0' || isspace (p[len])))
2619 while (isspace (*p))
2621 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2653 if (tokstart[1] != '"' && tokstart[1] != '\'')
2662 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2667 error (_("Empty character constant."));
2668 else if (host_len > 2 && c == '\'')
2671 namelen = lexptr - tokstart - 1;
2674 else if (host_len > 1)
2675 error (_("Invalid character constant."));
2681 if (!(c == '_' || c == '$'
2682 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2683 /* We must have come across a bad character (e.g. ';'). */
2684 error (_("Invalid character '%c' in expression."), c);
2686 /* It's a name. See how long it is. */
2688 for (c = tokstart[namelen];
2689 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2690 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2692 /* Template parameter lists are part of the name.
2693 FIXME: This mishandles `print $a<4&&$a>3'. */
2697 if (! is_cast_operator (tokstart, namelen))
2699 /* Scan ahead to get rest of the template specification. Note
2700 that we look ahead only when the '<' adjoins non-whitespace
2701 characters; for comparison expressions, e.g. "a < b > c",
2702 there must be spaces before the '<', etc. */
2704 const char *p = find_template_name_end (tokstart + namelen);
2707 namelen = p - tokstart;
2711 c = tokstart[++namelen];
2714 /* The token "if" terminates the expression and is NOT removed from
2715 the input stream. It doesn't count if it appears in the
2716 expansion of a macro. */
2718 && tokstart[0] == 'i'
2719 && tokstart[1] == 'f'
2720 && ! scanning_macro_expansion ())
2725 /* For the same reason (breakpoint conditions), "thread N"
2726 terminates the expression. "thread" could be an identifier, but
2727 an identifier is never followed by a number without intervening
2728 punctuation. "task" is similar. Handle abbreviations of these,
2729 similarly to breakpoint.c:find_condition_and_thread. */
2731 && (strncmp (tokstart, "thread", namelen) == 0
2732 || strncmp (tokstart, "task", namelen) == 0)
2733 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2734 && ! scanning_macro_expansion ())
2736 const char *p = tokstart + namelen + 1;
2738 while (*p == ' ' || *p == '\t')
2740 if (*p >= '0' && *p <= '9')
2748 yylval.sval.ptr = tokstart;
2749 yylval.sval.length = namelen;
2751 /* Catch specific keywords. */
2752 copy = copy_name (yylval.sval);
2753 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2754 if (strcmp (copy, ident_tokens[i].operator) == 0)
2756 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2757 && parse_language->la_language != language_cplus)
2760 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2762 struct field_of_this_result is_a_field_of_this;
2764 if (lookup_symbol (copy, expression_context_block,
2766 (parse_language->la_language == language_cplus
2767 ? &is_a_field_of_this
2771 /* The keyword is shadowed. */
2776 /* It is ok to always set this, even though we don't always
2777 strictly need to. */
2778 yylval.opcode = ident_tokens[i].opcode;
2779 return ident_tokens[i].token;
2782 if (*tokstart == '$')
2785 if (parse_completion && *lexptr == '\0')
2786 saw_name_at_eof = 1;
2788 yylval.ssym.stoken = yylval.sval;
2789 yylval.ssym.sym = NULL;
2790 yylval.ssym.is_a_field_of_this = 0;
2794 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2801 DEF_VEC_O (token_and_value);
2803 /* A FIFO of tokens that have been read but not yet returned to the
2805 static VEC (token_and_value) *token_fifo;
2807 /* Non-zero if the lexer should return tokens from the FIFO. */
2810 /* Temporary storage for c_lex; this holds symbol names as they are
2812 static struct obstack name_obstack;
2814 /* Classify a NAME token. The contents of the token are in `yylval'.
2815 Updates yylval and returns the new token type. BLOCK is the block
2816 in which lookups start; this can be NULL to mean the global
2819 classify_name (const struct block *block)
2823 struct field_of_this_result is_a_field_of_this;
2825 copy = copy_name (yylval.sval);
2827 /* Initialize this in case we *don't* use it in this call; that way
2828 we can refer to it unconditionally below. */
2829 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2831 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2832 parse_language->la_name_of_this
2833 ? &is_a_field_of_this : NULL);
2835 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2837 yylval.ssym.sym = sym;
2838 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2843 /* See if it's a file name. */
2844 struct symtab *symtab;
2846 symtab = lookup_symtab (copy);
2849 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2853 /* If we found a field of 'this', we might have erroneously
2854 found a constructor where we wanted a type name. Handle this
2855 case by noticing that we found a constructor and then look up
2856 the type tag instead. */
2857 if (is_a_field_of_this.type != NULL
2858 && is_a_field_of_this.fn_field != NULL
2859 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2862 struct field_of_this_result inner_is_a_field_of_this;
2864 sym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2865 &inner_is_a_field_of_this);
2868 yylval.tsym.type = SYMBOL_TYPE (sym);
2874 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2876 yylval.tsym.type = SYMBOL_TYPE (sym);
2881 = language_lookup_primitive_type_by_name (parse_language,
2882 parse_gdbarch, copy);
2883 if (yylval.tsym.type != NULL)
2886 /* See if it's an ObjC classname. */
2887 if (parse_language->la_language == language_objc && !sym)
2889 CORE_ADDR Class = lookup_objc_class (parse_gdbarch, copy);
2892 yylval.class.class = Class;
2893 sym = lookup_struct_typedef (copy, expression_context_block, 1);
2895 yylval.class.type = SYMBOL_TYPE (sym);
2900 /* Input names that aren't symbols but ARE valid hex numbers, when
2901 the input radix permits them, can be names or numbers depending
2902 on the parse. Note we support radixes > 16 here. */
2904 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2905 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2907 YYSTYPE newlval; /* Its value is ignored. */
2908 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2911 yylval.ssym.sym = sym;
2912 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2917 /* Any other kind of symbol */
2918 yylval.ssym.sym = sym;
2919 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2922 && parse_language->la_language == language_cplus
2923 && is_a_field_of_this.type == NULL
2924 && !lookup_minimal_symbol (copy, NULL, NULL))
2925 return UNKNOWN_CPP_NAME;
2930 /* Like classify_name, but used by the inner loop of the lexer, when a
2931 name might have already been seen. CONTEXT is the context type, or
2932 NULL if this is the first component of a name. */
2935 classify_inner_name (const struct block *block, struct type *context)
2940 if (context == NULL)
2941 return classify_name (block);
2943 type = check_typedef (context);
2944 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2945 && TYPE_CODE (type) != TYPE_CODE_UNION
2946 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2949 copy = copy_name (yylval.ssym.stoken);
2950 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block);
2951 if (yylval.ssym.sym == NULL)
2954 switch (SYMBOL_CLASS (yylval.ssym.sym))
2961 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2967 internal_error (__FILE__, __LINE__, _("not reached"));
2970 /* The outer level of a two-level lexer. This calls the inner lexer
2971 to return tokens. It then either returns these tokens, or
2972 aggregates them into a larger token. This lets us work around a
2973 problem in our parsing approach, where the parser could not
2974 distinguish between qualified names and qualified types at the
2977 This approach is still not ideal, because it mishandles template
2978 types. See the comment in lex_one_token for an example. However,
2979 this is still an improvement over the earlier approach, and will
2980 suffice until we move to better parsing technology. */
2984 token_and_value current;
2985 int first_was_coloncolon, last_was_coloncolon;
2986 struct type *context_type = NULL;
2987 int last_to_examine, next_to_examine, checkpoint;
2988 const struct block *search_block;
2990 if (popping && !VEC_empty (token_and_value, token_fifo))
2994 /* Read the first token and decide what to do. Most of the
2995 subsequent code is C++-only; but also depends on seeing a "::" or
2997 current.token = lex_one_token ();
2998 if (current.token == NAME)
2999 current.token = classify_name (expression_context_block);
3000 if (parse_language->la_language != language_cplus
3001 || (current.token != TYPENAME && current.token != COLONCOLON
3002 && current.token != FILENAME))
3003 return current.token;
3005 /* Read any sequence of alternating "::" and name-like tokens into
3007 current.value = yylval;
3008 VEC_safe_push (token_and_value, token_fifo, ¤t);
3009 last_was_coloncolon = current.token == COLONCOLON;
3012 current.token = lex_one_token ();
3013 current.value = yylval;
3014 VEC_safe_push (token_and_value, token_fifo, ¤t);
3016 if ((last_was_coloncolon && current.token != NAME)
3017 || (!last_was_coloncolon && current.token != COLONCOLON))
3019 last_was_coloncolon = !last_was_coloncolon;
3023 /* We always read one extra token, so compute the number of tokens
3024 to examine accordingly. */
3025 last_to_examine = VEC_length (token_and_value, token_fifo) - 2;
3026 next_to_examine = 0;
3028 current = *VEC_index (token_and_value, token_fifo, next_to_examine);
3031 obstack_free (&name_obstack, obstack_base (&name_obstack));
3033 if (current.token == FILENAME)
3034 search_block = current.value.bval;
3035 else if (current.token == COLONCOLON)
3036 search_block = NULL;
3039 gdb_assert (current.token == TYPENAME);
3040 search_block = expression_context_block;
3041 obstack_grow (&name_obstack, current.value.sval.ptr,
3042 current.value.sval.length);
3043 context_type = current.value.tsym.type;
3047 first_was_coloncolon = current.token == COLONCOLON;
3048 last_was_coloncolon = first_was_coloncolon;
3050 while (next_to_examine <= last_to_examine)
3052 token_and_value *next;
3054 next = VEC_index (token_and_value, token_fifo, next_to_examine);
3057 if (next->token == NAME && last_was_coloncolon)
3061 yylval = next->value;
3062 classification = classify_inner_name (search_block, context_type);
3063 /* We keep going until we either run out of names, or until
3064 we have a qualified name which is not a type. */
3065 if (classification != TYPENAME && classification != NAME)
3068 /* Accept up to this token. */
3069 checkpoint = next_to_examine;
3071 /* Update the partial name we are constructing. */
3072 if (context_type != NULL)
3074 /* We don't want to put a leading "::" into the name. */
3075 obstack_grow_str (&name_obstack, "::");
3077 obstack_grow (&name_obstack, next->value.sval.ptr,
3078 next->value.sval.length);
3080 yylval.sval.ptr = obstack_base (&name_obstack);
3081 yylval.sval.length = obstack_object_size (&name_obstack);
3082 current.value = yylval;
3083 current.token = classification;
3085 last_was_coloncolon = 0;
3087 if (classification == NAME)
3090 context_type = yylval.tsym.type;
3092 else if (next->token == COLONCOLON && !last_was_coloncolon)
3093 last_was_coloncolon = 1;
3096 /* We've reached the end of the name. */
3101 /* If we have a replacement token, install it as the first token in
3102 the FIFO, and delete the other constituent tokens. */
3105 current.value.sval.ptr = obstack_copy0 (&expansion_obstack,
3106 current.value.sval.ptr,
3107 current.value.sval.length);
3109 VEC_replace (token_and_value, token_fifo, 0, ¤t);
3111 VEC_block_remove (token_and_value, token_fifo, 1, checkpoint - 1);
3115 current = *VEC_index (token_and_value, token_fifo, 0);
3116 VEC_ordered_remove (token_and_value, token_fifo, 0);
3117 yylval = current.value;
3118 return current.token;
3125 struct cleanup *back_to = make_cleanup (free_current_contents,
3126 &expression_macro_scope);
3128 /* Set up the scope for macro expansion. */
3129 expression_macro_scope = NULL;
3131 if (expression_context_block)
3132 expression_macro_scope
3133 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
3135 expression_macro_scope = default_macro_scope ();
3136 if (! expression_macro_scope)
3137 expression_macro_scope = user_macro_scope ();
3139 /* Initialize macro expansion code. */
3140 obstack_init (&expansion_obstack);
3141 gdb_assert (! macro_original_text);
3142 make_cleanup (scan_macro_cleanup, 0);
3144 make_cleanup_restore_integer (&yydebug);
3145 yydebug = parser_debug;
3147 /* Initialize some state used by the lexer. */
3148 last_was_structop = 0;
3149 saw_name_at_eof = 0;
3151 VEC_free (token_and_value, token_fifo);
3153 obstack_init (&name_obstack);
3154 make_cleanup_obstack_free (&name_obstack);
3156 result = yyparse ();
3157 do_cleanups (back_to);
3161 /* This is called via the YYPRINT macro when parser debugging is
3162 enabled. It prints a token's value. */
3165 c_print_token (FILE *file, int type, YYSTYPE value)
3170 fprintf (file, "typed_val_int<%s, %s>",
3171 TYPE_SAFE_NAME (value.typed_val_int.type),
3172 pulongest (value.typed_val_int.val));
3178 char *copy = alloca (value.tsval.length + 1);
3180 memcpy (copy, value.tsval.ptr, value.tsval.length);
3181 copy[value.tsval.length] = '\0';
3183 fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3189 fprintf (file, "sval<%s>", copy_name (value.sval));
3193 fprintf (file, "tsym<type=%s, name=%s>",
3194 TYPE_SAFE_NAME (value.tsym.type),
3195 copy_name (value.tsym.stoken));
3199 case UNKNOWN_CPP_NAME:
3202 fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3203 copy_name (value.ssym.stoken),
3204 (value.ssym.sym == NULL
3205 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym)),
3206 value.ssym.is_a_field_of_this);
3210 fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3219 lexptr = prev_lexptr;
3221 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);