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 (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
238 /* Special type cases, put in to allow the parser to distinguish different
240 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
242 %token <sval> VARIABLE
244 %token <opcode> ASSIGN_MODIFY
253 %right '=' ASSIGN_MODIFY
261 %left '<' '>' LEQ GEQ
266 %right UNARY INCREMENT DECREMENT
267 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
268 %token <ssym> BLOCKNAME
269 %token <bval> FILENAME
283 { write_exp_elt_opcode(OP_TYPE);
284 write_exp_elt_type($1);
285 write_exp_elt_opcode(OP_TYPE);}
288 write_exp_elt_opcode (OP_TYPEOF);
290 | TYPEOF '(' type ')'
292 write_exp_elt_opcode (OP_TYPE);
293 write_exp_elt_type ($3);
294 write_exp_elt_opcode (OP_TYPE);
296 | DECLTYPE '(' exp ')'
298 write_exp_elt_opcode (OP_DECLTYPE);
302 /* Expressions, including the comma operator. */
305 { write_exp_elt_opcode (BINOP_COMMA); }
308 /* Expressions, not including the comma operator. */
309 exp : '*' exp %prec UNARY
310 { write_exp_elt_opcode (UNOP_IND); }
313 exp : '&' exp %prec UNARY
314 { write_exp_elt_opcode (UNOP_ADDR); }
317 exp : '-' exp %prec UNARY
318 { write_exp_elt_opcode (UNOP_NEG); }
321 exp : '+' exp %prec UNARY
322 { write_exp_elt_opcode (UNOP_PLUS); }
325 exp : '!' exp %prec UNARY
326 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
329 exp : '~' exp %prec UNARY
330 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
333 exp : INCREMENT exp %prec UNARY
334 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
337 exp : DECREMENT exp %prec UNARY
338 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
341 exp : exp INCREMENT %prec UNARY
342 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
345 exp : exp DECREMENT %prec UNARY
346 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
349 exp : SIZEOF exp %prec UNARY
350 { write_exp_elt_opcode (UNOP_SIZEOF); }
354 { write_exp_elt_opcode (STRUCTOP_PTR);
355 write_exp_string ($3);
356 write_exp_elt_opcode (STRUCTOP_PTR); }
359 exp : exp ARROW name COMPLETE
360 { mark_struct_expression ();
361 write_exp_elt_opcode (STRUCTOP_PTR);
362 write_exp_string ($3);
363 write_exp_elt_opcode (STRUCTOP_PTR); }
366 exp : exp ARROW COMPLETE
368 mark_struct_expression ();
369 write_exp_elt_opcode (STRUCTOP_PTR);
372 write_exp_string (s);
373 write_exp_elt_opcode (STRUCTOP_PTR); }
376 exp : exp ARROW '~' name
377 { write_exp_elt_opcode (STRUCTOP_PTR);
378 write_destructor_name ($4);
379 write_exp_elt_opcode (STRUCTOP_PTR); }
382 exp : exp ARROW '~' name COMPLETE
383 { mark_struct_expression ();
384 write_exp_elt_opcode (STRUCTOP_PTR);
385 write_destructor_name ($4);
386 write_exp_elt_opcode (STRUCTOP_PTR); }
389 exp : exp ARROW qualified_name
390 { /* exp->type::name becomes exp->*(&type::name) */
391 /* Note: this doesn't work if name is a
392 static member! FIXME */
393 write_exp_elt_opcode (UNOP_ADDR);
394 write_exp_elt_opcode (STRUCTOP_MPTR); }
397 exp : exp ARROW_STAR exp
398 { write_exp_elt_opcode (STRUCTOP_MPTR); }
402 { write_exp_elt_opcode (STRUCTOP_STRUCT);
403 write_exp_string ($3);
404 write_exp_elt_opcode (STRUCTOP_STRUCT); }
407 exp : exp '.' name COMPLETE
408 { mark_struct_expression ();
409 write_exp_elt_opcode (STRUCTOP_STRUCT);
410 write_exp_string ($3);
411 write_exp_elt_opcode (STRUCTOP_STRUCT); }
414 exp : exp '.' COMPLETE
416 mark_struct_expression ();
417 write_exp_elt_opcode (STRUCTOP_STRUCT);
420 write_exp_string (s);
421 write_exp_elt_opcode (STRUCTOP_STRUCT); }
424 exp : exp '.' '~' name
425 { write_exp_elt_opcode (STRUCTOP_STRUCT);
426 write_destructor_name ($4);
427 write_exp_elt_opcode (STRUCTOP_STRUCT); }
430 exp : exp '.' '~' name COMPLETE
431 { mark_struct_expression ();
432 write_exp_elt_opcode (STRUCTOP_STRUCT);
433 write_destructor_name ($4);
434 write_exp_elt_opcode (STRUCTOP_STRUCT); }
437 exp : exp '.' qualified_name
438 { /* exp.type::name becomes exp.*(&type::name) */
439 /* Note: this doesn't work if name is a
440 static member! FIXME */
441 write_exp_elt_opcode (UNOP_ADDR);
442 write_exp_elt_opcode (STRUCTOP_MEMBER); }
445 exp : exp DOT_STAR exp
446 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
449 exp : exp '[' exp1 ']'
450 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
453 exp : exp OBJC_LBRAC exp1 ']'
454 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
458 * The rules below parse ObjC message calls of the form:
459 * '[' target selector {':' argument}* ']'
462 exp : OBJC_LBRAC TYPENAME
466 class = lookup_objc_class (parse_gdbarch,
467 copy_name ($2.stoken));
469 error (_("%s is not an ObjC Class"),
470 copy_name ($2.stoken));
471 write_exp_elt_opcode (OP_LONG);
472 write_exp_elt_type (parse_type->builtin_int);
473 write_exp_elt_longcst ((LONGEST) class);
474 write_exp_elt_opcode (OP_LONG);
478 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
480 write_exp_elt_opcode (OP_OBJC_MSGCALL);
484 exp : OBJC_LBRAC CLASSNAME
486 write_exp_elt_opcode (OP_LONG);
487 write_exp_elt_type (parse_type->builtin_int);
488 write_exp_elt_longcst ((LONGEST) $2.class);
489 write_exp_elt_opcode (OP_LONG);
493 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
495 write_exp_elt_opcode (OP_OBJC_MSGCALL);
502 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
504 write_exp_elt_opcode (OP_OBJC_MSGCALL);
509 { add_msglist(&$1, 0); }
517 msgarg : name ':' exp
518 { add_msglist(&$1, 1); }
519 | ':' exp /* Unnamed arg. */
520 { add_msglist(0, 1); }
521 | ',' exp /* Variable number of args. */
522 { add_msglist(0, 0); }
526 /* This is to save the value of arglist_len
527 being accumulated by an outer function call. */
528 { start_arglist (); }
529 arglist ')' %prec ARROW
530 { write_exp_elt_opcode (OP_FUNCALL);
531 write_exp_elt_longcst ((LONGEST) end_arglist ());
532 write_exp_elt_opcode (OP_FUNCALL); }
535 exp : UNKNOWN_CPP_NAME '('
537 /* This could potentially be a an argument defined
538 lookup function (Koenig). */
539 write_exp_elt_opcode (OP_ADL_FUNC);
540 write_exp_elt_block (expression_context_block);
541 write_exp_elt_sym (NULL); /* Placeholder. */
542 write_exp_string ($1.stoken);
543 write_exp_elt_opcode (OP_ADL_FUNC);
545 /* This is to save the value of arglist_len
546 being accumulated by an outer function call. */
550 arglist ')' %prec ARROW
552 write_exp_elt_opcode (OP_FUNCALL);
553 write_exp_elt_longcst ((LONGEST) end_arglist ());
554 write_exp_elt_opcode (OP_FUNCALL);
559 { start_arglist (); }
569 arglist : arglist ',' exp %prec ABOVE_COMMA
573 exp : exp '(' parameter_typelist ')' const_or_volatile
575 VEC (type_ptr) *type_list = $3;
576 struct type *type_elt;
577 LONGEST len = VEC_length (type_ptr, type_list);
579 write_exp_elt_opcode (TYPE_INSTANCE);
580 write_exp_elt_longcst (len);
582 VEC_iterate (type_ptr, type_list, i, type_elt);
584 write_exp_elt_type (type_elt);
585 write_exp_elt_longcst(len);
586 write_exp_elt_opcode (TYPE_INSTANCE);
587 VEC_free (type_ptr, type_list);
592 { $$ = end_arglist () - 1; }
594 exp : lcurly arglist rcurly %prec ARROW
595 { write_exp_elt_opcode (OP_ARRAY);
596 write_exp_elt_longcst ((LONGEST) 0);
597 write_exp_elt_longcst ((LONGEST) $3);
598 write_exp_elt_opcode (OP_ARRAY); }
601 exp : lcurly type_exp rcurly exp %prec UNARY
602 { write_exp_elt_opcode (UNOP_MEMVAL_TYPE); }
605 exp : '(' type_exp ')' exp %prec UNARY
606 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
613 /* Binary operators in order of decreasing precedence. */
616 { write_exp_elt_opcode (BINOP_REPEAT); }
620 { write_exp_elt_opcode (BINOP_MUL); }
624 { write_exp_elt_opcode (BINOP_DIV); }
628 { write_exp_elt_opcode (BINOP_REM); }
632 { write_exp_elt_opcode (BINOP_ADD); }
636 { write_exp_elt_opcode (BINOP_SUB); }
640 { write_exp_elt_opcode (BINOP_LSH); }
644 { write_exp_elt_opcode (BINOP_RSH); }
648 { write_exp_elt_opcode (BINOP_EQUAL); }
651 exp : exp NOTEQUAL exp
652 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
656 { write_exp_elt_opcode (BINOP_LEQ); }
660 { write_exp_elt_opcode (BINOP_GEQ); }
664 { write_exp_elt_opcode (BINOP_LESS); }
668 { write_exp_elt_opcode (BINOP_GTR); }
672 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
676 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
680 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
684 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
688 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
691 exp : exp '?' exp ':' exp %prec '?'
692 { write_exp_elt_opcode (TERNOP_COND); }
696 { write_exp_elt_opcode (BINOP_ASSIGN); }
699 exp : exp ASSIGN_MODIFY exp
700 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
701 write_exp_elt_opcode ($2);
702 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
706 { write_exp_elt_opcode (OP_LONG);
707 write_exp_elt_type ($1.type);
708 write_exp_elt_longcst ((LONGEST)($1.val));
709 write_exp_elt_opcode (OP_LONG); }
714 struct stoken_vector vec;
717 write_exp_string_vector ($1.type, &vec);
723 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
724 write_exp_elt_opcode (OP_LONG);
725 write_exp_elt_type (val.typed_val_int.type);
726 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
727 write_exp_elt_opcode (OP_LONG);
733 { write_exp_elt_opcode (OP_DOUBLE);
734 write_exp_elt_type ($1.type);
735 write_exp_elt_dblcst ($1.dval);
736 write_exp_elt_opcode (OP_DOUBLE); }
740 { write_exp_elt_opcode (OP_DECFLOAT);
741 write_exp_elt_type ($1.type);
742 write_exp_elt_decfloatcst ($1.val);
743 write_exp_elt_opcode (OP_DECFLOAT); }
751 write_dollar_variable ($1);
755 exp : SELECTOR '(' name ')'
757 write_exp_elt_opcode (OP_OBJC_SELECTOR);
758 write_exp_string ($3);
759 write_exp_elt_opcode (OP_OBJC_SELECTOR); }
762 exp : SIZEOF '(' type ')' %prec UNARY
763 { write_exp_elt_opcode (OP_LONG);
764 write_exp_elt_type (lookup_signed_typename
765 (parse_language, parse_gdbarch,
768 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
769 write_exp_elt_opcode (OP_LONG); }
772 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
773 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
776 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
777 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
780 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
781 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
784 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
785 { /* We could do more error checking here, but
786 it doesn't seem worthwhile. */
787 write_exp_elt_opcode (UNOP_CAST_TYPE); }
793 /* We copy the string here, and not in the
794 lexer, to guarantee that we do not leak a
795 string. Note that we follow the
796 NUL-termination convention of the
798 struct typed_stoken *vec = XNEW (struct typed_stoken);
803 vec->length = $1.length;
804 vec->ptr = malloc ($1.length + 1);
805 memcpy (vec->ptr, $1.ptr, $1.length + 1);
810 /* Note that we NUL-terminate here, but just
814 $$.tokens = realloc ($$.tokens,
815 $$.len * sizeof (struct typed_stoken));
817 p = malloc ($2.length + 1);
818 memcpy (p, $2.ptr, $2.length + 1);
820 $$.tokens[$$.len - 1].type = $2.type;
821 $$.tokens[$$.len - 1].length = $2.length;
822 $$.tokens[$$.len - 1].ptr = p;
829 enum c_string_type type = C_STRING;
831 for (i = 0; i < $1.len; ++i)
833 switch ($1.tokens[i].type)
841 && type != $1.tokens[i].type)
842 error (_("Undefined string concatenation."));
843 type = $1.tokens[i].type;
847 internal_error (__FILE__, __LINE__,
848 "unrecognized type in string concatenation");
852 write_exp_string_vector (type, &$1);
853 for (i = 0; i < $1.len; ++i)
854 free ($1.tokens[i].ptr);
859 exp : NSSTRING /* ObjC NextStep NSString constant
860 * of the form '@' '"' string '"'.
862 { write_exp_elt_opcode (OP_OBJC_NSSTRING);
863 write_exp_string ($1);
864 write_exp_elt_opcode (OP_OBJC_NSSTRING); }
869 { write_exp_elt_opcode (OP_LONG);
870 write_exp_elt_type (parse_type->builtin_bool);
871 write_exp_elt_longcst ((LONGEST) 1);
872 write_exp_elt_opcode (OP_LONG); }
876 { write_exp_elt_opcode (OP_LONG);
877 write_exp_elt_type (parse_type->builtin_bool);
878 write_exp_elt_longcst ((LONGEST) 0);
879 write_exp_elt_opcode (OP_LONG); }
887 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
889 error (_("No file or function \"%s\"."),
890 copy_name ($1.stoken));
898 block : block COLONCOLON name
900 = lookup_symbol (copy_name ($3), $1,
902 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
903 error (_("No function \"%s\" in specified context."),
905 $$ = SYMBOL_BLOCK_VALUE (tem); }
908 variable: name_not_typename ENTRY
909 { struct symbol *sym = $1.sym;
911 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
912 || !symbol_read_needs_frame (sym))
913 error (_("@entry can be used only for function "
914 "parameters, not for \"%s\""),
915 copy_name ($1.stoken));
917 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
918 write_exp_elt_sym (sym);
919 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
923 variable: block COLONCOLON name
924 { struct symbol *sym;
925 sym = lookup_symbol (copy_name ($3), $1,
928 error (_("No symbol \"%s\" in specified context."),
930 if (symbol_read_needs_frame (sym))
932 if (innermost_block == 0
933 || contained_in (block_found,
935 innermost_block = block_found;
938 write_exp_elt_opcode (OP_VAR_VALUE);
939 /* block_found is set by lookup_symbol. */
940 write_exp_elt_block (block_found);
941 write_exp_elt_sym (sym);
942 write_exp_elt_opcode (OP_VAR_VALUE); }
945 qualified_name: TYPENAME COLONCOLON name
947 struct type *type = $1.type;
948 CHECK_TYPEDEF (type);
949 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
950 && TYPE_CODE (type) != TYPE_CODE_UNION
951 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
952 error (_("`%s' is not defined as an aggregate type."),
953 TYPE_SAFE_NAME (type));
955 write_exp_elt_opcode (OP_SCOPE);
956 write_exp_elt_type (type);
957 write_exp_string ($3);
958 write_exp_elt_opcode (OP_SCOPE);
960 | TYPENAME COLONCOLON '~' name
962 struct type *type = $1.type;
963 struct stoken tmp_token;
964 CHECK_TYPEDEF (type);
965 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
966 && TYPE_CODE (type) != TYPE_CODE_UNION
967 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
968 error (_("`%s' is not defined as an aggregate type."),
969 TYPE_SAFE_NAME (type));
971 tmp_token.ptr = (char*) alloca ($4.length + 2);
972 tmp_token.length = $4.length + 1;
973 tmp_token.ptr[0] = '~';
974 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
975 tmp_token.ptr[tmp_token.length] = 0;
977 /* Check for valid destructor name. */
978 destructor_name_p (tmp_token.ptr, $1.type);
979 write_exp_elt_opcode (OP_SCOPE);
980 write_exp_elt_type (type);
981 write_exp_string (tmp_token);
982 write_exp_elt_opcode (OP_SCOPE);
984 | TYPENAME COLONCOLON name COLONCOLON name
986 char *copy = copy_name ($3);
987 error (_("No type \"%s\" within class "
988 "or namespace \"%s\"."),
989 copy, TYPE_SAFE_NAME ($1.type));
993 variable: qualified_name
994 | COLONCOLON name_not_typename
996 char *name = copy_name ($2.stoken);
998 struct minimal_symbol *msymbol;
1001 lookup_symbol (name, (const struct block *) NULL,
1005 write_exp_elt_opcode (OP_VAR_VALUE);
1006 write_exp_elt_block (NULL);
1007 write_exp_elt_sym (sym);
1008 write_exp_elt_opcode (OP_VAR_VALUE);
1012 msymbol = lookup_minimal_symbol (name, NULL, NULL);
1013 if (msymbol != NULL)
1014 write_exp_msymbol (msymbol);
1015 else if (!have_full_symbols () && !have_partial_symbols ())
1016 error (_("No symbol table is loaded. Use the \"file\" command."));
1018 error (_("No symbol \"%s\" in current context."), name);
1022 variable: name_not_typename
1023 { struct symbol *sym = $1.sym;
1027 if (symbol_read_needs_frame (sym))
1029 if (innermost_block == 0
1030 || contained_in (block_found,
1032 innermost_block = block_found;
1035 write_exp_elt_opcode (OP_VAR_VALUE);
1036 /* We want to use the selected frame, not
1037 another more inner frame which happens to
1038 be in the same block. */
1039 write_exp_elt_block (NULL);
1040 write_exp_elt_sym (sym);
1041 write_exp_elt_opcode (OP_VAR_VALUE);
1043 else if ($1.is_a_field_of_this)
1045 /* C++: it hangs off of `this'. Must
1046 not inadvertently convert from a method call
1048 if (innermost_block == 0
1049 || contained_in (block_found,
1051 innermost_block = block_found;
1052 write_exp_elt_opcode (OP_THIS);
1053 write_exp_elt_opcode (OP_THIS);
1054 write_exp_elt_opcode (STRUCTOP_PTR);
1055 write_exp_string ($1.stoken);
1056 write_exp_elt_opcode (STRUCTOP_PTR);
1060 struct minimal_symbol *msymbol;
1061 char *arg = copy_name ($1.stoken);
1064 lookup_minimal_symbol (arg, NULL, NULL);
1065 if (msymbol != NULL)
1066 write_exp_msymbol (msymbol);
1067 else if (!have_full_symbols () && !have_partial_symbols ())
1068 error (_("No symbol table is loaded. Use the \"file\" command."));
1070 error (_("No symbol \"%s\" in current context."),
1071 copy_name ($1.stoken));
1076 space_identifier : '@' NAME
1077 { insert_type_address_space (copy_name ($2.stoken)); }
1080 const_or_volatile: const_or_volatile_noopt
1084 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1087 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1088 | const_or_volatile_noopt
1091 const_or_volatile_or_space_identifier:
1092 const_or_volatile_or_space_identifier_noopt
1098 { insert_type (tp_pointer); }
1099 const_or_volatile_or_space_identifier
1101 { insert_type (tp_pointer); }
1102 const_or_volatile_or_space_identifier
1104 { insert_type (tp_reference); }
1106 { insert_type (tp_reference); }
1109 ptr_operator_ts: ptr_operator
1111 $$ = get_type_stack ();
1112 /* This cleanup is eventually run by
1114 make_cleanup (type_stack_cleanup, $$);
1118 abs_decl: ptr_operator_ts direct_abs_decl
1119 { $$ = append_type_stack ($2, $1); }
1124 direct_abs_decl: '(' abs_decl ')'
1126 | direct_abs_decl array_mod
1128 push_type_stack ($1);
1130 push_type (tp_array);
1131 $$ = get_type_stack ();
1136 push_type (tp_array);
1137 $$ = get_type_stack ();
1140 | direct_abs_decl func_mod
1142 push_type_stack ($1);
1144 $$ = get_type_stack ();
1149 $$ = get_type_stack ();
1159 | OBJC_LBRAC INT ']'
1165 | '(' parameter_typelist ')'
1169 /* We used to try to recognize pointer to member types here, but
1170 that didn't work (shift/reduce conflicts meant that these rules never
1171 got executed). The problem is that
1172 int (foo::bar::baz::bizzle)
1173 is a function type but
1174 int (foo::bar::baz::bizzle::*)
1175 is a pointer to member type. Stroustrup loses again! */
1180 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1184 { $$ = lookup_signed_typename (parse_language,
1188 { $$ = lookup_signed_typename (parse_language,
1192 { $$ = lookup_signed_typename (parse_language,
1196 { $$ = lookup_signed_typename (parse_language,
1199 | LONG SIGNED_KEYWORD INT_KEYWORD
1200 { $$ = lookup_signed_typename (parse_language,
1203 | LONG SIGNED_KEYWORD
1204 { $$ = lookup_signed_typename (parse_language,
1207 | SIGNED_KEYWORD LONG INT_KEYWORD
1208 { $$ = lookup_signed_typename (parse_language,
1211 | UNSIGNED LONG INT_KEYWORD
1212 { $$ = lookup_unsigned_typename (parse_language,
1215 | LONG UNSIGNED INT_KEYWORD
1216 { $$ = lookup_unsigned_typename (parse_language,
1220 { $$ = lookup_unsigned_typename (parse_language,
1224 { $$ = lookup_signed_typename (parse_language,
1227 | LONG LONG INT_KEYWORD
1228 { $$ = lookup_signed_typename (parse_language,
1231 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1232 { $$ = lookup_signed_typename (parse_language,
1235 | LONG LONG SIGNED_KEYWORD
1236 { $$ = lookup_signed_typename (parse_language,
1239 | SIGNED_KEYWORD LONG LONG
1240 { $$ = lookup_signed_typename (parse_language,
1243 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1244 { $$ = lookup_signed_typename (parse_language,
1247 | UNSIGNED LONG LONG
1248 { $$ = lookup_unsigned_typename (parse_language,
1251 | UNSIGNED LONG LONG INT_KEYWORD
1252 { $$ = lookup_unsigned_typename (parse_language,
1255 | LONG LONG UNSIGNED
1256 { $$ = lookup_unsigned_typename (parse_language,
1259 | LONG LONG UNSIGNED INT_KEYWORD
1260 { $$ = lookup_unsigned_typename (parse_language,
1264 { $$ = lookup_signed_typename (parse_language,
1267 | SHORT SIGNED_KEYWORD INT_KEYWORD
1268 { $$ = lookup_signed_typename (parse_language,
1271 | SHORT SIGNED_KEYWORD
1272 { $$ = lookup_signed_typename (parse_language,
1275 | UNSIGNED SHORT INT_KEYWORD
1276 { $$ = lookup_unsigned_typename (parse_language,
1280 { $$ = lookup_unsigned_typename (parse_language,
1283 | SHORT UNSIGNED INT_KEYWORD
1284 { $$ = lookup_unsigned_typename (parse_language,
1288 { $$ = lookup_typename (parse_language, parse_gdbarch,
1289 "double", (struct block *) NULL,
1291 | LONG DOUBLE_KEYWORD
1292 { $$ = lookup_typename (parse_language, parse_gdbarch,
1294 (struct block *) NULL, 0); }
1296 { $$ = lookup_struct (copy_name ($2),
1297 expression_context_block); }
1300 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1303 | STRUCT name COMPLETE
1305 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1310 { $$ = lookup_struct (copy_name ($2),
1311 expression_context_block); }
1314 mark_completion_tag (TYPE_CODE_CLASS, "", 0);
1317 | CLASS name COMPLETE
1319 mark_completion_tag (TYPE_CODE_CLASS, $2.ptr,
1324 { $$ = lookup_union (copy_name ($2),
1325 expression_context_block); }
1328 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1331 | UNION name COMPLETE
1333 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1338 { $$ = lookup_enum (copy_name ($2),
1339 expression_context_block); }
1342 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1345 | ENUM name COMPLETE
1347 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1352 { $$ = lookup_unsigned_typename (parse_language,
1354 TYPE_NAME($2.type)); }
1356 { $$ = lookup_unsigned_typename (parse_language,
1359 | SIGNED_KEYWORD typename
1360 { $$ = lookup_signed_typename (parse_language,
1362 TYPE_NAME($2.type)); }
1364 { $$ = lookup_signed_typename (parse_language,
1367 /* It appears that this rule for templates is never
1368 reduced; template recognition happens by lookahead
1369 in the token processing code in yylex. */
1370 | TEMPLATE name '<' type '>'
1371 { $$ = lookup_template_type(copy_name($2), $4,
1372 expression_context_block);
1374 | const_or_volatile_or_space_identifier_noopt typebase
1375 { $$ = follow_types ($2); }
1376 | typebase const_or_volatile_or_space_identifier_noopt
1377 { $$ = follow_types ($1); }
1383 $$.stoken.ptr = "int";
1384 $$.stoken.length = 3;
1385 $$.type = lookup_signed_typename (parse_language,
1391 $$.stoken.ptr = "long";
1392 $$.stoken.length = 4;
1393 $$.type = lookup_signed_typename (parse_language,
1399 $$.stoken.ptr = "short";
1400 $$.stoken.length = 5;
1401 $$.type = lookup_signed_typename (parse_language,
1409 { check_parameter_typelist ($1); }
1410 | nonempty_typelist ',' DOTDOTDOT
1412 VEC_safe_push (type_ptr, $1, NULL);
1413 check_parameter_typelist ($1);
1421 VEC (type_ptr) *typelist = NULL;
1422 VEC_safe_push (type_ptr, typelist, $1);
1425 | nonempty_typelist ',' type
1427 VEC_safe_push (type_ptr, $1, $3);
1435 push_type_stack ($2);
1436 $$ = follow_types ($1);
1440 conversion_type_id: typebase conversion_declarator
1441 { $$ = follow_types ($1); }
1444 conversion_declarator: /* Nothing. */
1445 | ptr_operator conversion_declarator
1448 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1449 | VOLATILE_KEYWORD CONST_KEYWORD
1452 const_or_volatile_noopt: const_and_volatile
1453 { insert_type (tp_const);
1454 insert_type (tp_volatile);
1457 { insert_type (tp_const); }
1459 { insert_type (tp_volatile); }
1462 operator: OPERATOR NEW
1463 { $$ = operator_stoken (" new"); }
1465 { $$ = operator_stoken (" delete"); }
1466 | OPERATOR NEW '[' ']'
1467 { $$ = operator_stoken (" new[]"); }
1468 | OPERATOR DELETE '[' ']'
1469 { $$ = operator_stoken (" delete[]"); }
1470 | OPERATOR NEW OBJC_LBRAC ']'
1471 { $$ = operator_stoken (" new[]"); }
1472 | OPERATOR DELETE OBJC_LBRAC ']'
1473 { $$ = operator_stoken (" delete[]"); }
1475 { $$ = operator_stoken ("+"); }
1477 { $$ = operator_stoken ("-"); }
1479 { $$ = operator_stoken ("*"); }
1481 { $$ = operator_stoken ("/"); }
1483 { $$ = operator_stoken ("%"); }
1485 { $$ = operator_stoken ("^"); }
1487 { $$ = operator_stoken ("&"); }
1489 { $$ = operator_stoken ("|"); }
1491 { $$ = operator_stoken ("~"); }
1493 { $$ = operator_stoken ("!"); }
1495 { $$ = operator_stoken ("="); }
1497 { $$ = operator_stoken ("<"); }
1499 { $$ = operator_stoken (">"); }
1500 | OPERATOR ASSIGN_MODIFY
1501 { const char *op = "unknown";
1525 case BINOP_BITWISE_IOR:
1528 case BINOP_BITWISE_AND:
1531 case BINOP_BITWISE_XOR:
1538 $$ = operator_stoken (op);
1541 { $$ = operator_stoken ("<<"); }
1543 { $$ = operator_stoken (">>"); }
1545 { $$ = operator_stoken ("=="); }
1547 { $$ = operator_stoken ("!="); }
1549 { $$ = operator_stoken ("<="); }
1551 { $$ = operator_stoken (">="); }
1553 { $$ = operator_stoken ("&&"); }
1555 { $$ = operator_stoken ("||"); }
1556 | OPERATOR INCREMENT
1557 { $$ = operator_stoken ("++"); }
1558 | OPERATOR DECREMENT
1559 { $$ = operator_stoken ("--"); }
1561 { $$ = operator_stoken (","); }
1562 | OPERATOR ARROW_STAR
1563 { $$ = operator_stoken ("->*"); }
1565 { $$ = operator_stoken ("->"); }
1567 { $$ = operator_stoken ("()"); }
1569 { $$ = operator_stoken ("[]"); }
1570 | OPERATOR OBJC_LBRAC ']'
1571 { $$ = operator_stoken ("[]"); }
1572 | OPERATOR conversion_type_id
1575 struct ui_file *buf = mem_fileopen ();
1577 c_print_type ($2, NULL, buf, -1, 0,
1578 &type_print_raw_options);
1579 name = ui_file_xstrdup (buf, &length);
1580 ui_file_delete (buf);
1581 $$ = operator_stoken (name);
1588 name : NAME { $$ = $1.stoken; }
1589 | BLOCKNAME { $$ = $1.stoken; }
1590 | TYPENAME { $$ = $1.stoken; }
1591 | NAME_OR_INT { $$ = $1.stoken; }
1592 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1593 | operator { $$ = $1; }
1596 name_not_typename : NAME
1598 /* These would be useful if name_not_typename was useful, but it is just
1599 a fake for "variable", so these cause reduce/reduce conflicts because
1600 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1601 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1602 context where only a name could occur, this might be useful.
1607 struct field_of_this_result is_a_field_of_this;
1610 $$.sym = lookup_symbol ($1.ptr,
1611 expression_context_block,
1613 &is_a_field_of_this);
1614 $$.is_a_field_of_this
1615 = is_a_field_of_this.type != NULL;
1622 /* Like write_exp_string, but prepends a '~'. */
1625 write_destructor_name (struct stoken token)
1627 char *copy = alloca (token.length + 1);
1630 memcpy (©[1], token.ptr, token.length);
1635 write_exp_string (token);
1638 /* Returns a stoken of the operator name given by OP (which does not
1639 include the string "operator"). */
1640 static struct stoken
1641 operator_stoken (const char *op)
1643 static const char *operator_string = "operator";
1644 struct stoken st = { NULL, 0 };
1645 st.length = strlen (operator_string) + strlen (op);
1646 st.ptr = malloc (st.length + 1);
1647 strcpy (st.ptr, operator_string);
1648 strcat (st.ptr, op);
1650 /* The toplevel (c_parse) will free the memory allocated here. */
1651 make_cleanup (free, st.ptr);
1655 /* Validate a parameter typelist. */
1658 check_parameter_typelist (VEC (type_ptr) *params)
1663 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1665 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1669 if (VEC_length (type_ptr, params) == 1)
1674 VEC_free (type_ptr, params);
1675 error (_("parameter types following 'void'"));
1679 VEC_free (type_ptr, params);
1680 error (_("'void' invalid as parameter type"));
1686 /* Take care of parsing a number (anything that starts with a digit).
1687 Set yylval and return the token type; update lexptr.
1688 LEN is the number of characters in it. */
1690 /*** Needs some error checking for the float case ***/
1693 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1695 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1696 here, and we do kind of silly things like cast to unsigned. */
1703 int base = input_radix;
1706 /* Number of "L" suffixes encountered. */
1709 /* We have found a "L" or "U" suffix. */
1710 int found_suffix = 0;
1713 struct type *signed_type;
1714 struct type *unsigned_type;
1718 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1719 point. Return DECFLOAT. */
1721 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1724 putithere->typed_val_decfloat.type
1725 = parse_type->builtin_decfloat;
1726 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1727 gdbarch_byte_order (parse_gdbarch), p);
1732 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1735 putithere->typed_val_decfloat.type
1736 = parse_type->builtin_decdouble;
1737 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1738 gdbarch_byte_order (parse_gdbarch), p);
1743 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1746 putithere->typed_val_decfloat.type
1747 = parse_type->builtin_declong;
1748 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1749 gdbarch_byte_order (parse_gdbarch), p);
1754 if (! parse_c_float (parse_gdbarch, p, len,
1755 &putithere->typed_val_float.dval,
1756 &putithere->typed_val_float.type))
1761 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1805 if (c >= 'A' && c <= 'Z')
1807 if (c != 'l' && c != 'u')
1809 if (c >= '0' && c <= '9')
1817 if (base > 10 && c >= 'a' && c <= 'f')
1821 n += i = c - 'a' + 10;
1834 return ERROR; /* Char not a digit */
1837 return ERROR; /* Invalid digit in this base */
1839 /* Portably test for overflow (only works for nonzero values, so make
1840 a second check for zero). FIXME: Can't we just make n and prevn
1841 unsigned and avoid this? */
1842 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1843 unsigned_p = 1; /* Try something unsigned */
1845 /* Portably test for unsigned overflow.
1846 FIXME: This check is wrong; for example it doesn't find overflow
1847 on 0x123456789 when LONGEST is 32 bits. */
1848 if (c != 'l' && c != 'u' && n != 0)
1850 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1851 error (_("Numeric constant too large."));
1856 /* An integer constant is an int, a long, or a long long. An L
1857 suffix forces it to be long; an LL suffix forces it to be long
1858 long. If not forced to a larger size, it gets the first type of
1859 the above that it fits in. To figure out whether it fits, we
1860 shift it right and see whether anything remains. Note that we
1861 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1862 operation, because many compilers will warn about such a shift
1863 (which always produces a zero result). Sometimes gdbarch_int_bit
1864 or gdbarch_long_bit will be that big, sometimes not. To deal with
1865 the case where it is we just always shift the value more than
1866 once, with fewer bits each time. */
1868 un = (ULONGEST)n >> 2;
1870 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1872 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1874 /* A large decimal (not hex or octal) constant (between INT_MAX
1875 and UINT_MAX) is a long or unsigned long, according to ANSI,
1876 never an unsigned int, but this code treats it as unsigned
1877 int. This probably should be fixed. GCC gives a warning on
1880 unsigned_type = parse_type->builtin_unsigned_int;
1881 signed_type = parse_type->builtin_int;
1883 else if (long_p <= 1
1884 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1886 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1887 unsigned_type = parse_type->builtin_unsigned_long;
1888 signed_type = parse_type->builtin_long;
1893 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1894 < gdbarch_long_long_bit (parse_gdbarch))
1895 /* A long long does not fit in a LONGEST. */
1896 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1898 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1899 high_bit = (ULONGEST) 1 << shift;
1900 unsigned_type = parse_type->builtin_unsigned_long_long;
1901 signed_type = parse_type->builtin_long_long;
1904 putithere->typed_val_int.val = n;
1906 /* If the high bit of the worked out type is set then this number
1907 has to be unsigned. */
1909 if (unsigned_p || (n & high_bit))
1911 putithere->typed_val_int.type = unsigned_type;
1915 putithere->typed_val_int.type = signed_type;
1921 /* Temporary obstack used for holding strings. */
1922 static struct obstack tempbuf;
1923 static int tempbuf_init;
1925 /* Parse a C escape sequence. The initial backslash of the sequence
1926 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1927 last character of the sequence. If OUTPUT is not NULL, the
1928 translated form of the escape sequence will be written there. If
1929 OUTPUT is NULL, no output is written and the call will only affect
1930 *PTR. If an escape sequence is expressed in target bytes, then the
1931 entire sequence will simply be copied to OUTPUT. Return 1 if any
1932 character was emitted, 0 otherwise. */
1935 c_parse_escape (char **ptr, struct obstack *output)
1937 char *tokptr = *ptr;
1940 /* Some escape sequences undergo character set conversion. Those we
1944 /* Hex escapes do not undergo character set conversion, so keep
1945 the escape sequence for later. */
1948 obstack_grow_str (output, "\\x");
1950 if (!isxdigit (*tokptr))
1951 error (_("\\x escape without a following hex digit"));
1952 while (isxdigit (*tokptr))
1955 obstack_1grow (output, *tokptr);
1960 /* Octal escapes do not undergo character set conversion, so
1961 keep the escape sequence for later. */
1973 obstack_grow_str (output, "\\");
1975 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1979 obstack_1grow (output, *tokptr);
1985 /* We handle UCNs later. We could handle them here, but that
1986 would mean a spurious error in the case where the UCN could
1987 be converted to the target charset but not the host
1993 int i, len = c == 'U' ? 8 : 4;
1996 obstack_1grow (output, '\\');
1997 obstack_1grow (output, *tokptr);
2000 if (!isxdigit (*tokptr))
2001 error (_("\\%c escape without a following hex digit"), c);
2002 for (i = 0; i < len && isxdigit (*tokptr); ++i)
2005 obstack_1grow (output, *tokptr);
2011 /* We must pass backslash through so that it does not
2012 cause quoting during the second expansion. */
2015 obstack_grow_str (output, "\\\\");
2019 /* Escapes which undergo conversion. */
2022 obstack_1grow (output, '\a');
2027 obstack_1grow (output, '\b');
2032 obstack_1grow (output, '\f');
2037 obstack_1grow (output, '\n');
2042 obstack_1grow (output, '\r');
2047 obstack_1grow (output, '\t');
2052 obstack_1grow (output, '\v');
2056 /* GCC extension. */
2059 obstack_1grow (output, HOST_ESCAPE_CHAR);
2063 /* Backslash-newline expands to nothing at all. */
2069 /* A few escapes just expand to the character itself. */
2073 /* GCC extensions. */
2078 /* Unrecognized escapes turn into the character itself. */
2081 obstack_1grow (output, *tokptr);
2089 /* Parse a string or character literal from TOKPTR. The string or
2090 character may be wide or unicode. *OUTPTR is set to just after the
2091 end of the literal in the input string. The resulting token is
2092 stored in VALUE. This returns a token value, either STRING or
2093 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2094 number of host characters in the literal. */
2096 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
2100 enum c_string_type type;
2103 /* Build the gdb internal form of the input string in tempbuf. Note
2104 that the buffer is null byte terminated *only* for the
2105 convenience of debugging gdb itself and printing the buffer
2106 contents when the buffer contains no embedded nulls. Gdb does
2107 not depend upon the buffer being null byte terminated, it uses
2108 the length string instead. This allows gdb to handle C strings
2109 (as well as strings in other languages) with embedded null
2115 obstack_free (&tempbuf, NULL);
2116 obstack_init (&tempbuf);
2118 /* Record the string type. */
2121 type = C_WIDE_STRING;
2124 else if (*tokptr == 'u')
2129 else if (*tokptr == 'U')
2134 else if (*tokptr == '@')
2136 /* An Objective C string. */
2144 /* Skip the quote. */
2158 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2160 else if (c == quote)
2164 obstack_1grow (&tempbuf, c);
2166 /* FIXME: this does the wrong thing with multi-byte host
2167 characters. We could use mbrlen here, but that would
2168 make "set host-charset" a bit less useful. */
2173 if (*tokptr != quote)
2176 error (_("Unterminated string in expression."));
2178 error (_("Unmatched single quote."));
2183 value->ptr = obstack_base (&tempbuf);
2184 value->length = obstack_object_size (&tempbuf);
2188 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2191 /* This is used to associate some attributes with a token. */
2195 /* If this bit is set, the token is C++-only. */
2199 /* If this bit is set, the token is conditional: if there is a
2200 symbol of the same name, then the token is a symbol; otherwise,
2201 the token is a keyword. */
2210 enum exp_opcode opcode;
2211 enum token_flags flags;
2214 static const struct token tokentab3[] =
2216 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2217 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2218 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2219 {"...", DOTDOTDOT, BINOP_END, 0}
2222 static const struct token tokentab2[] =
2224 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2225 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2226 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2227 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2228 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2229 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2230 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2231 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2232 {"++", INCREMENT, BINOP_END, 0},
2233 {"--", DECREMENT, BINOP_END, 0},
2234 {"->", ARROW, BINOP_END, 0},
2235 {"&&", ANDAND, BINOP_END, 0},
2236 {"||", OROR, BINOP_END, 0},
2237 /* "::" is *not* only C++: gdb overrides its meaning in several
2238 different ways, e.g., 'filename'::func, function::variable. */
2239 {"::", COLONCOLON, BINOP_END, 0},
2240 {"<<", LSH, BINOP_END, 0},
2241 {">>", RSH, BINOP_END, 0},
2242 {"==", EQUAL, BINOP_END, 0},
2243 {"!=", NOTEQUAL, BINOP_END, 0},
2244 {"<=", LEQ, BINOP_END, 0},
2245 {">=", GEQ, BINOP_END, 0},
2246 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2249 /* Identifier-like tokens. */
2250 static const struct token ident_tokens[] =
2252 {"unsigned", UNSIGNED, OP_NULL, 0},
2253 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2254 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2255 {"struct", STRUCT, OP_NULL, 0},
2256 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2257 {"sizeof", SIZEOF, OP_NULL, 0},
2258 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2259 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2260 {"class", CLASS, OP_NULL, FLAG_CXX},
2261 {"union", UNION, OP_NULL, 0},
2262 {"short", SHORT, OP_NULL, 0},
2263 {"const", CONST_KEYWORD, OP_NULL, 0},
2264 {"enum", ENUM, OP_NULL, 0},
2265 {"long", LONG, OP_NULL, 0},
2266 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2267 {"int", INT_KEYWORD, OP_NULL, 0},
2268 {"new", NEW, OP_NULL, FLAG_CXX},
2269 {"delete", DELETE, OP_NULL, FLAG_CXX},
2270 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2272 {"and", ANDAND, BINOP_END, FLAG_CXX},
2273 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2274 {"bitand", '&', OP_NULL, FLAG_CXX},
2275 {"bitor", '|', OP_NULL, FLAG_CXX},
2276 {"compl", '~', OP_NULL, FLAG_CXX},
2277 {"not", '!', OP_NULL, FLAG_CXX},
2278 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2279 {"or", OROR, BINOP_END, FLAG_CXX},
2280 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2281 {"xor", '^', OP_NULL, FLAG_CXX},
2282 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2284 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2285 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2286 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2287 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2289 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2290 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2291 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2292 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2293 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW }
2296 /* When we find that lexptr (the global var defined in parse.c) is
2297 pointing at a macro invocation, we expand the invocation, and call
2298 scan_macro_expansion to save the old lexptr here and point lexptr
2299 into the expanded text. When we reach the end of that, we call
2300 end_macro_expansion to pop back to the value we saved here. The
2301 macro expansion code promises to return only fully-expanded text,
2302 so we don't need to "push" more than one level.
2304 This is disgusting, of course. It would be cleaner to do all macro
2305 expansion beforehand, and then hand that to lexptr. But we don't
2306 really know where the expression ends. Remember, in a command like
2308 (gdb) break *ADDRESS if CONDITION
2310 we evaluate ADDRESS in the scope of the current frame, but we
2311 evaluate CONDITION in the scope of the breakpoint's location. So
2312 it's simply wrong to try to macro-expand the whole thing at once. */
2313 static char *macro_original_text;
2315 /* We save all intermediate macro expansions on this obstack for the
2316 duration of a single parse. The expansion text may sometimes have
2317 to live past the end of the expansion, due to yacc lookahead.
2318 Rather than try to be clever about saving the data for a single
2319 token, we simply keep it all and delete it after parsing has
2321 static struct obstack expansion_obstack;
2324 scan_macro_expansion (char *expansion)
2328 /* We'd better not be trying to push the stack twice. */
2329 gdb_assert (! macro_original_text);
2331 /* Copy to the obstack, and then free the intermediate
2333 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2336 /* Save the old lexptr value, so we can return to it when we're done
2337 parsing the expanded text. */
2338 macro_original_text = lexptr;
2344 scanning_macro_expansion (void)
2346 return macro_original_text != 0;
2351 finished_macro_expansion (void)
2353 /* There'd better be something to pop back to. */
2354 gdb_assert (macro_original_text);
2356 /* Pop back to the original text. */
2357 lexptr = macro_original_text;
2358 macro_original_text = 0;
2363 scan_macro_cleanup (void *dummy)
2365 if (macro_original_text)
2366 finished_macro_expansion ();
2368 obstack_free (&expansion_obstack, NULL);
2371 /* Return true iff the token represents a C++ cast operator. */
2374 is_cast_operator (const char *token, int len)
2376 return (! strncmp (token, "dynamic_cast", len)
2377 || ! strncmp (token, "static_cast", len)
2378 || ! strncmp (token, "reinterpret_cast", len)
2379 || ! strncmp (token, "const_cast", len));
2382 /* The scope used for macro expansion. */
2383 static struct macro_scope *expression_macro_scope;
2385 /* This is set if a NAME token appeared at the very end of the input
2386 string, with no whitespace separating the name from the EOF. This
2387 is used only when parsing to do field name completion. */
2388 static int saw_name_at_eof;
2390 /* This is set if the previously-returned token was a structure
2391 operator -- either '.' or ARROW. This is used only when parsing to
2392 do field name completion. */
2393 static int last_was_structop;
2395 /* Read one token, getting characters through lexptr. */
2398 lex_one_token (void)
2404 int saw_structop = last_was_structop;
2407 last_was_structop = 0;
2411 /* Check if this is a macro invocation that we need to expand. */
2412 if (! scanning_macro_expansion ())
2414 char *expanded = macro_expand_next (&lexptr,
2415 standard_macro_lookup,
2416 expression_macro_scope);
2419 scan_macro_expansion (expanded);
2422 prev_lexptr = lexptr;
2425 /* See if it is a special token of length 3. */
2426 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2427 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2429 if ((tokentab3[i].flags & FLAG_CXX) != 0
2430 && parse_language->la_language != language_cplus)
2434 yylval.opcode = tokentab3[i].opcode;
2435 return tokentab3[i].token;
2438 /* See if it is a special token of length 2. */
2439 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2440 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2442 if ((tokentab2[i].flags & FLAG_CXX) != 0
2443 && parse_language->la_language != language_cplus)
2447 yylval.opcode = tokentab2[i].opcode;
2448 if (parse_completion && tokentab2[i].token == ARROW)
2449 last_was_structop = 1;
2450 return tokentab2[i].token;
2453 switch (c = *tokstart)
2456 /* If we were just scanning the result of a macro expansion,
2457 then we need to resume scanning the original text.
2458 If we're parsing for field name completion, and the previous
2459 token allows such completion, return a COMPLETE token.
2460 Otherwise, we were already scanning the original text, and
2461 we're really done. */
2462 if (scanning_macro_expansion ())
2464 finished_macro_expansion ();
2467 else if (saw_name_at_eof)
2469 saw_name_at_eof = 0;
2472 else if (saw_structop)
2487 if (parse_language->la_language == language_objc && c == '[')
2493 if (paren_depth == 0)
2500 if (comma_terminates
2502 && ! scanning_macro_expansion ())
2508 /* Might be a floating point number. */
2509 if (lexptr[1] < '0' || lexptr[1] > '9')
2511 if (parse_completion)
2512 last_was_structop = 1;
2513 goto symbol; /* Nope, must be a symbol. */
2515 /* FALL THRU into number case. */
2528 /* It's a number. */
2529 int got_dot = 0, got_e = 0, toktype;
2531 int hex = input_radix > 10;
2533 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2538 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2546 /* This test includes !hex because 'e' is a valid hex digit
2547 and thus does not indicate a floating point number when
2548 the radix is hex. */
2549 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2550 got_dot = got_e = 1;
2551 /* This test does not include !hex, because a '.' always indicates
2552 a decimal floating point number regardless of the radix. */
2553 else if (!got_dot && *p == '.')
2555 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2556 && (*p == '-' || *p == '+'))
2557 /* This is the sign of the exponent, not the end of the
2560 /* We will take any letters or digits. parse_number will
2561 complain if past the radix, or if L or U are not final. */
2562 else if ((*p < '0' || *p > '9')
2563 && ((*p < 'a' || *p > 'z')
2564 && (*p < 'A' || *p > 'Z')))
2567 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2568 if (toktype == ERROR)
2570 char *err_copy = (char *) alloca (p - tokstart + 1);
2572 memcpy (err_copy, tokstart, p - tokstart);
2573 err_copy[p - tokstart] = 0;
2574 error (_("Invalid number \"%s\"."), err_copy);
2582 char *p = &tokstart[1];
2583 size_t len = strlen ("entry");
2585 if (parse_language->la_language == language_objc)
2587 size_t len = strlen ("selector");
2589 if (strncmp (p, "selector", len) == 0
2590 && (p[len] == '\0' || isspace (p[len])))
2599 while (isspace (*p))
2601 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2633 if (tokstart[1] != '"' && tokstart[1] != '\'')
2642 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2647 error (_("Empty character constant."));
2648 else if (host_len > 2 && c == '\'')
2651 namelen = lexptr - tokstart - 1;
2654 else if (host_len > 1)
2655 error (_("Invalid character constant."));
2661 if (!(c == '_' || c == '$'
2662 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2663 /* We must have come across a bad character (e.g. ';'). */
2664 error (_("Invalid character '%c' in expression."), c);
2666 /* It's a name. See how long it is. */
2668 for (c = tokstart[namelen];
2669 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2670 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2672 /* Template parameter lists are part of the name.
2673 FIXME: This mishandles `print $a<4&&$a>3'. */
2677 if (! is_cast_operator (tokstart, namelen))
2679 /* Scan ahead to get rest of the template specification. Note
2680 that we look ahead only when the '<' adjoins non-whitespace
2681 characters; for comparison expressions, e.g. "a < b > c",
2682 there must be spaces before the '<', etc. */
2684 char * p = find_template_name_end (tokstart + namelen);
2686 namelen = p - tokstart;
2690 c = tokstart[++namelen];
2693 /* The token "if" terminates the expression and is NOT removed from
2694 the input stream. It doesn't count if it appears in the
2695 expansion of a macro. */
2697 && tokstart[0] == 'i'
2698 && tokstart[1] == 'f'
2699 && ! scanning_macro_expansion ())
2704 /* For the same reason (breakpoint conditions), "thread N"
2705 terminates the expression. "thread" could be an identifier, but
2706 an identifier is never followed by a number without intervening
2707 punctuation. "task" is similar. Handle abbreviations of these,
2708 similarly to breakpoint.c:find_condition_and_thread. */
2710 && (strncmp (tokstart, "thread", namelen) == 0
2711 || strncmp (tokstart, "task", namelen) == 0)
2712 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2713 && ! scanning_macro_expansion ())
2715 char *p = tokstart + namelen + 1;
2716 while (*p == ' ' || *p == '\t')
2718 if (*p >= '0' && *p <= '9')
2726 yylval.sval.ptr = tokstart;
2727 yylval.sval.length = namelen;
2729 /* Catch specific keywords. */
2730 copy = copy_name (yylval.sval);
2731 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2732 if (strcmp (copy, ident_tokens[i].operator) == 0)
2734 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2735 && parse_language->la_language != language_cplus)
2738 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2740 struct field_of_this_result is_a_field_of_this;
2742 if (lookup_symbol (copy, expression_context_block,
2744 (parse_language->la_language == language_cplus
2745 ? &is_a_field_of_this
2749 /* The keyword is shadowed. */
2754 /* It is ok to always set this, even though we don't always
2755 strictly need to. */
2756 yylval.opcode = ident_tokens[i].opcode;
2757 return ident_tokens[i].token;
2760 if (*tokstart == '$')
2763 if (parse_completion && *lexptr == '\0')
2764 saw_name_at_eof = 1;
2766 yylval.ssym.stoken = yylval.sval;
2767 yylval.ssym.sym = NULL;
2768 yylval.ssym.is_a_field_of_this = 0;
2772 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2779 DEF_VEC_O (token_and_value);
2781 /* A FIFO of tokens that have been read but not yet returned to the
2783 static VEC (token_and_value) *token_fifo;
2785 /* Non-zero if the lexer should return tokens from the FIFO. */
2788 /* Temporary storage for c_lex; this holds symbol names as they are
2790 static struct obstack name_obstack;
2792 /* Classify a NAME token. The contents of the token are in `yylval'.
2793 Updates yylval and returns the new token type. BLOCK is the block
2794 in which lookups start; this can be NULL to mean the global
2797 classify_name (const struct block *block)
2801 struct field_of_this_result is_a_field_of_this;
2803 copy = copy_name (yylval.sval);
2805 /* Initialize this in case we *don't* use it in this call; that way
2806 we can refer to it unconditionally below. */
2807 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2809 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2810 parse_language->la_name_of_this
2811 ? &is_a_field_of_this : NULL);
2813 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2815 yylval.ssym.sym = sym;
2816 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2821 /* See if it's a file name. */
2822 struct symtab *symtab;
2824 symtab = lookup_symtab (copy);
2827 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2831 /* If we found a field of 'this', we might have erroneously
2832 found a constructor where we wanted a type name. Handle this
2833 case by noticing that we found a constructor and then look up
2834 the type tag instead. */
2835 if (is_a_field_of_this.type != NULL
2836 && is_a_field_of_this.fn_field != NULL
2837 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2840 struct field_of_this_result inner_is_a_field_of_this;
2842 sym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2843 &inner_is_a_field_of_this);
2846 yylval.tsym.type = SYMBOL_TYPE (sym);
2852 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2854 yylval.tsym.type = SYMBOL_TYPE (sym);
2859 = language_lookup_primitive_type_by_name (parse_language,
2860 parse_gdbarch, copy);
2861 if (yylval.tsym.type != NULL)
2864 /* See if it's an ObjC classname. */
2865 if (parse_language->la_language == language_objc && !sym)
2867 CORE_ADDR Class = lookup_objc_class (parse_gdbarch, copy);
2870 yylval.class.class = Class;
2871 sym = lookup_struct_typedef (copy, expression_context_block, 1);
2873 yylval.class.type = SYMBOL_TYPE (sym);
2878 /* Input names that aren't symbols but ARE valid hex numbers, when
2879 the input radix permits them, can be names or numbers depending
2880 on the parse. Note we support radixes > 16 here. */
2882 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2883 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2885 YYSTYPE newlval; /* Its value is ignored. */
2886 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2889 yylval.ssym.sym = sym;
2890 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2895 /* Any other kind of symbol */
2896 yylval.ssym.sym = sym;
2897 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2900 && parse_language->la_language == language_cplus
2901 && is_a_field_of_this.type == NULL
2902 && !lookup_minimal_symbol (copy, NULL, NULL))
2903 return UNKNOWN_CPP_NAME;
2908 /* Like classify_name, but used by the inner loop of the lexer, when a
2909 name might have already been seen. CONTEXT is the context type, or
2910 NULL if this is the first component of a name. */
2913 classify_inner_name (const struct block *block, struct type *context)
2918 if (context == NULL)
2919 return classify_name (block);
2921 type = check_typedef (context);
2922 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2923 && TYPE_CODE (type) != TYPE_CODE_UNION
2924 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2927 copy = copy_name (yylval.ssym.stoken);
2928 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block);
2929 if (yylval.ssym.sym == NULL)
2932 switch (SYMBOL_CLASS (yylval.ssym.sym))
2939 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2945 internal_error (__FILE__, __LINE__, _("not reached"));
2948 /* The outer level of a two-level lexer. This calls the inner lexer
2949 to return tokens. It then either returns these tokens, or
2950 aggregates them into a larger token. This lets us work around a
2951 problem in our parsing approach, where the parser could not
2952 distinguish between qualified names and qualified types at the
2955 This approach is still not ideal, because it mishandles template
2956 types. See the comment in lex_one_token for an example. However,
2957 this is still an improvement over the earlier approach, and will
2958 suffice until we move to better parsing technology. */
2962 token_and_value current;
2963 int first_was_coloncolon, last_was_coloncolon;
2964 struct type *context_type = NULL;
2965 int last_to_examine, next_to_examine, checkpoint;
2966 const struct block *search_block;
2968 if (popping && !VEC_empty (token_and_value, token_fifo))
2972 /* Read the first token and decide what to do. Most of the
2973 subsequent code is C++-only; but also depends on seeing a "::" or
2975 current.token = lex_one_token ();
2976 if (current.token == NAME)
2977 current.token = classify_name (expression_context_block);
2978 if (parse_language->la_language != language_cplus
2979 || (current.token != TYPENAME && current.token != COLONCOLON
2980 && current.token != FILENAME))
2981 return current.token;
2983 /* Read any sequence of alternating "::" and name-like tokens into
2985 current.value = yylval;
2986 VEC_safe_push (token_and_value, token_fifo, ¤t);
2987 last_was_coloncolon = current.token == COLONCOLON;
2990 current.token = lex_one_token ();
2991 current.value = yylval;
2992 VEC_safe_push (token_and_value, token_fifo, ¤t);
2994 if ((last_was_coloncolon && current.token != NAME)
2995 || (!last_was_coloncolon && current.token != COLONCOLON))
2997 last_was_coloncolon = !last_was_coloncolon;
3001 /* We always read one extra token, so compute the number of tokens
3002 to examine accordingly. */
3003 last_to_examine = VEC_length (token_and_value, token_fifo) - 2;
3004 next_to_examine = 0;
3006 current = *VEC_index (token_and_value, token_fifo, next_to_examine);
3009 obstack_free (&name_obstack, obstack_base (&name_obstack));
3011 if (current.token == FILENAME)
3012 search_block = current.value.bval;
3013 else if (current.token == COLONCOLON)
3014 search_block = NULL;
3017 gdb_assert (current.token == TYPENAME);
3018 search_block = expression_context_block;
3019 obstack_grow (&name_obstack, current.value.sval.ptr,
3020 current.value.sval.length);
3021 context_type = current.value.tsym.type;
3025 first_was_coloncolon = current.token == COLONCOLON;
3026 last_was_coloncolon = first_was_coloncolon;
3028 while (next_to_examine <= last_to_examine)
3030 token_and_value *next;
3032 next = VEC_index (token_and_value, token_fifo, next_to_examine);
3035 if (next->token == NAME && last_was_coloncolon)
3039 yylval = next->value;
3040 classification = classify_inner_name (search_block, context_type);
3041 /* We keep going until we either run out of names, or until
3042 we have a qualified name which is not a type. */
3043 if (classification != TYPENAME && classification != NAME)
3046 /* Accept up to this token. */
3047 checkpoint = next_to_examine;
3049 /* Update the partial name we are constructing. */
3050 if (context_type != NULL)
3052 /* We don't want to put a leading "::" into the name. */
3053 obstack_grow_str (&name_obstack, "::");
3055 obstack_grow (&name_obstack, next->value.sval.ptr,
3056 next->value.sval.length);
3058 yylval.sval.ptr = obstack_base (&name_obstack);
3059 yylval.sval.length = obstack_object_size (&name_obstack);
3060 current.value = yylval;
3061 current.token = classification;
3063 last_was_coloncolon = 0;
3065 if (classification == NAME)
3068 context_type = yylval.tsym.type;
3070 else if (next->token == COLONCOLON && !last_was_coloncolon)
3071 last_was_coloncolon = 1;
3074 /* We've reached the end of the name. */
3079 /* If we have a replacement token, install it as the first token in
3080 the FIFO, and delete the other constituent tokens. */
3083 current.value.sval.ptr = obstack_copy0 (&expansion_obstack,
3084 current.value.sval.ptr,
3085 current.value.sval.length);
3087 VEC_replace (token_and_value, token_fifo, 0, ¤t);
3089 VEC_block_remove (token_and_value, token_fifo, 1, checkpoint - 1);
3093 current = *VEC_index (token_and_value, token_fifo, 0);
3094 VEC_ordered_remove (token_and_value, token_fifo, 0);
3095 yylval = current.value;
3096 return current.token;
3103 struct cleanup *back_to = make_cleanup (free_current_contents,
3104 &expression_macro_scope);
3106 /* Set up the scope for macro expansion. */
3107 expression_macro_scope = NULL;
3109 if (expression_context_block)
3110 expression_macro_scope
3111 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
3113 expression_macro_scope = default_macro_scope ();
3114 if (! expression_macro_scope)
3115 expression_macro_scope = user_macro_scope ();
3117 /* Initialize macro expansion code. */
3118 obstack_init (&expansion_obstack);
3119 gdb_assert (! macro_original_text);
3120 make_cleanup (scan_macro_cleanup, 0);
3122 make_cleanup_restore_integer (&yydebug);
3123 yydebug = parser_debug;
3125 /* Initialize some state used by the lexer. */
3126 last_was_structop = 0;
3127 saw_name_at_eof = 0;
3129 VEC_free (token_and_value, token_fifo);
3131 obstack_init (&name_obstack);
3132 make_cleanup_obstack_free (&name_obstack);
3134 result = yyparse ();
3135 do_cleanups (back_to);
3139 /* This is called via the YYPRINT macro when parser debugging is
3140 enabled. It prints a token's value. */
3143 c_print_token (FILE *file, int type, YYSTYPE value)
3148 fprintf (file, "typed_val_int<%s, %s>",
3149 TYPE_SAFE_NAME (value.typed_val_int.type),
3150 pulongest (value.typed_val_int.val));
3156 char *copy = alloca (value.tsval.length + 1);
3158 memcpy (copy, value.tsval.ptr, value.tsval.length);
3159 copy[value.tsval.length] = '\0';
3161 fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3167 fprintf (file, "sval<%s>", copy_name (value.sval));
3171 fprintf (file, "tsym<type=%s, name=%s>",
3172 TYPE_SAFE_NAME (value.tsym.type),
3173 copy_name (value.tsym.stoken));
3177 case UNKNOWN_CPP_NAME:
3180 fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3181 copy_name (value.ssym.stoken),
3182 (value.ssym.sym == NULL
3183 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym)),
3184 value.ssym.is_a_field_of_this);
3188 fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3197 lexptr = prev_lexptr;
3199 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);