1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2014 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. */
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);
173 static void c_print_token (FILE *file, int type, YYSTYPE value);
174 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
178 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
180 %type <tval> type typebase
181 %type <tvec> nonempty_typelist func_mod parameter_typelist
182 /* %type <bval> block */
184 /* Fancy type parsing. */
186 %type <lval> array_mod
187 %type <tval> conversion_type_id
189 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
191 %token <typed_val_int> INT
192 %token <typed_val_float> FLOAT
193 %token <typed_val_decfloat> DECFLOAT
195 /* Both NAME and TYPENAME tokens represent symbols in the input,
196 and both convey their data as strings.
197 But a TYPENAME is a string that happens to be defined as a typedef
198 or builtin type name (such as int or char)
199 and a NAME is any other symbol.
200 Contexts where this distinction is not important can use the
201 nonterminal "name", which matches either NAME or TYPENAME. */
203 %token <tsval> STRING
204 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
205 %token SELECTOR /* ObjC "@selector" pseudo-operator */
207 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
208 %token <ssym> UNKNOWN_CPP_NAME
209 %token <voidval> COMPLETE
210 %token <tsym> TYPENAME
211 %token <class> CLASSNAME /* ObjC Class name */
213 %type <svec> string_exp
214 %type <ssym> name_not_typename
215 %type <tsym> typename
217 /* This is like a '[' token, but is only generated when parsing
218 Objective C. This lets us reuse the same parser without
219 erroneously parsing ObjC-specific expressions in C. */
222 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
223 but which would parse as a valid number in the current input radix.
224 E.g. "c" when input_radix==16. Depending on the parse, it will be
225 turned into a name or into a number. */
227 %token <ssym> NAME_OR_INT
230 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
234 %type <sval> operator
235 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
241 /* Special type cases, put in to allow the parser to distinguish different
243 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
245 %token <sval> VARIABLE
247 %token <opcode> ASSIGN_MODIFY
256 %right '=' ASSIGN_MODIFY
264 %left '<' '>' LEQ GEQ
269 %right UNARY INCREMENT DECREMENT
270 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
271 %token <ssym> BLOCKNAME
272 %token <bval> FILENAME
286 { write_exp_elt_opcode(OP_TYPE);
287 write_exp_elt_type($1);
288 write_exp_elt_opcode(OP_TYPE);}
291 write_exp_elt_opcode (OP_TYPEOF);
293 | TYPEOF '(' type ')'
295 write_exp_elt_opcode (OP_TYPE);
296 write_exp_elt_type ($3);
297 write_exp_elt_opcode (OP_TYPE);
299 | DECLTYPE '(' exp ')'
301 write_exp_elt_opcode (OP_DECLTYPE);
305 /* Expressions, including the comma operator. */
308 { write_exp_elt_opcode (BINOP_COMMA); }
311 /* Expressions, not including the comma operator. */
312 exp : '*' exp %prec UNARY
313 { write_exp_elt_opcode (UNOP_IND); }
316 exp : '&' exp %prec UNARY
317 { write_exp_elt_opcode (UNOP_ADDR); }
320 exp : '-' exp %prec UNARY
321 { write_exp_elt_opcode (UNOP_NEG); }
324 exp : '+' exp %prec UNARY
325 { write_exp_elt_opcode (UNOP_PLUS); }
328 exp : '!' exp %prec UNARY
329 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
332 exp : '~' exp %prec UNARY
333 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
336 exp : INCREMENT exp %prec UNARY
337 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
340 exp : DECREMENT exp %prec UNARY
341 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
344 exp : exp INCREMENT %prec UNARY
345 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
348 exp : exp DECREMENT %prec UNARY
349 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
352 exp : TYPEID '(' exp ')' %prec UNARY
353 { write_exp_elt_opcode (OP_TYPEID); }
356 exp : TYPEID '(' type_exp ')' %prec UNARY
357 { write_exp_elt_opcode (OP_TYPEID); }
360 exp : SIZEOF exp %prec UNARY
361 { write_exp_elt_opcode (UNOP_SIZEOF); }
365 { write_exp_elt_opcode (STRUCTOP_PTR);
366 write_exp_string ($3);
367 write_exp_elt_opcode (STRUCTOP_PTR); }
370 exp : exp ARROW name COMPLETE
371 { mark_struct_expression ();
372 write_exp_elt_opcode (STRUCTOP_PTR);
373 write_exp_string ($3);
374 write_exp_elt_opcode (STRUCTOP_PTR); }
377 exp : exp ARROW COMPLETE
379 mark_struct_expression ();
380 write_exp_elt_opcode (STRUCTOP_PTR);
383 write_exp_string (s);
384 write_exp_elt_opcode (STRUCTOP_PTR); }
387 exp : exp ARROW '~' name
388 { write_exp_elt_opcode (STRUCTOP_PTR);
389 write_destructor_name ($4);
390 write_exp_elt_opcode (STRUCTOP_PTR); }
393 exp : exp ARROW '~' name COMPLETE
394 { mark_struct_expression ();
395 write_exp_elt_opcode (STRUCTOP_PTR);
396 write_destructor_name ($4);
397 write_exp_elt_opcode (STRUCTOP_PTR); }
400 exp : exp ARROW qualified_name
401 { /* exp->type::name becomes exp->*(&type::name) */
402 /* Note: this doesn't work if name is a
403 static member! FIXME */
404 write_exp_elt_opcode (UNOP_ADDR);
405 write_exp_elt_opcode (STRUCTOP_MPTR); }
408 exp : exp ARROW_STAR exp
409 { write_exp_elt_opcode (STRUCTOP_MPTR); }
413 { write_exp_elt_opcode (STRUCTOP_STRUCT);
414 write_exp_string ($3);
415 write_exp_elt_opcode (STRUCTOP_STRUCT); }
418 exp : exp '.' name COMPLETE
419 { mark_struct_expression ();
420 write_exp_elt_opcode (STRUCTOP_STRUCT);
421 write_exp_string ($3);
422 write_exp_elt_opcode (STRUCTOP_STRUCT); }
425 exp : exp '.' COMPLETE
427 mark_struct_expression ();
428 write_exp_elt_opcode (STRUCTOP_STRUCT);
431 write_exp_string (s);
432 write_exp_elt_opcode (STRUCTOP_STRUCT); }
435 exp : exp '.' '~' name
436 { write_exp_elt_opcode (STRUCTOP_STRUCT);
437 write_destructor_name ($4);
438 write_exp_elt_opcode (STRUCTOP_STRUCT); }
441 exp : exp '.' '~' name COMPLETE
442 { mark_struct_expression ();
443 write_exp_elt_opcode (STRUCTOP_STRUCT);
444 write_destructor_name ($4);
445 write_exp_elt_opcode (STRUCTOP_STRUCT); }
448 exp : exp '.' qualified_name
449 { /* exp.type::name becomes exp.*(&type::name) */
450 /* Note: this doesn't work if name is a
451 static member! FIXME */
452 write_exp_elt_opcode (UNOP_ADDR);
453 write_exp_elt_opcode (STRUCTOP_MEMBER); }
456 exp : exp DOT_STAR exp
457 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
460 exp : exp '[' exp1 ']'
461 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
464 exp : exp OBJC_LBRAC exp1 ']'
465 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
469 * The rules below parse ObjC message calls of the form:
470 * '[' target selector {':' argument}* ']'
473 exp : OBJC_LBRAC TYPENAME
477 class = lookup_objc_class (parse_gdbarch,
478 copy_name ($2.stoken));
480 error (_("%s is not an ObjC Class"),
481 copy_name ($2.stoken));
482 write_exp_elt_opcode (OP_LONG);
483 write_exp_elt_type (parse_type->builtin_int);
484 write_exp_elt_longcst ((LONGEST) class);
485 write_exp_elt_opcode (OP_LONG);
489 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
491 write_exp_elt_opcode (OP_OBJC_MSGCALL);
495 exp : OBJC_LBRAC CLASSNAME
497 write_exp_elt_opcode (OP_LONG);
498 write_exp_elt_type (parse_type->builtin_int);
499 write_exp_elt_longcst ((LONGEST) $2.class);
500 write_exp_elt_opcode (OP_LONG);
504 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
506 write_exp_elt_opcode (OP_OBJC_MSGCALL);
513 { write_exp_elt_opcode (OP_OBJC_MSGCALL);
515 write_exp_elt_opcode (OP_OBJC_MSGCALL);
520 { add_msglist(&$1, 0); }
528 msgarg : name ':' exp
529 { add_msglist(&$1, 1); }
530 | ':' exp /* Unnamed arg. */
531 { add_msglist(0, 1); }
532 | ',' exp /* Variable number of args. */
533 { add_msglist(0, 0); }
537 /* This is to save the value of arglist_len
538 being accumulated by an outer function call. */
539 { start_arglist (); }
540 arglist ')' %prec ARROW
541 { write_exp_elt_opcode (OP_FUNCALL);
542 write_exp_elt_longcst ((LONGEST) end_arglist ());
543 write_exp_elt_opcode (OP_FUNCALL); }
546 exp : UNKNOWN_CPP_NAME '('
548 /* This could potentially be a an argument defined
549 lookup function (Koenig). */
550 write_exp_elt_opcode (OP_ADL_FUNC);
551 write_exp_elt_block (expression_context_block);
552 write_exp_elt_sym (NULL); /* Placeholder. */
553 write_exp_string ($1.stoken);
554 write_exp_elt_opcode (OP_ADL_FUNC);
556 /* This is to save the value of arglist_len
557 being accumulated by an outer function call. */
561 arglist ')' %prec ARROW
563 write_exp_elt_opcode (OP_FUNCALL);
564 write_exp_elt_longcst ((LONGEST) end_arglist ());
565 write_exp_elt_opcode (OP_FUNCALL);
570 { start_arglist (); }
580 arglist : arglist ',' exp %prec ABOVE_COMMA
584 exp : exp '(' parameter_typelist ')' const_or_volatile
586 VEC (type_ptr) *type_list = $3;
587 struct type *type_elt;
588 LONGEST len = VEC_length (type_ptr, type_list);
590 write_exp_elt_opcode (TYPE_INSTANCE);
591 write_exp_elt_longcst (len);
593 VEC_iterate (type_ptr, type_list, i, type_elt);
595 write_exp_elt_type (type_elt);
596 write_exp_elt_longcst(len);
597 write_exp_elt_opcode (TYPE_INSTANCE);
598 VEC_free (type_ptr, type_list);
603 { $$ = end_arglist () - 1; }
605 exp : lcurly arglist rcurly %prec ARROW
606 { write_exp_elt_opcode (OP_ARRAY);
607 write_exp_elt_longcst ((LONGEST) 0);
608 write_exp_elt_longcst ((LONGEST) $3);
609 write_exp_elt_opcode (OP_ARRAY); }
612 exp : lcurly type_exp rcurly exp %prec UNARY
613 { write_exp_elt_opcode (UNOP_MEMVAL_TYPE); }
616 exp : '(' type_exp ')' exp %prec UNARY
617 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
624 /* Binary operators in order of decreasing precedence. */
627 { write_exp_elt_opcode (BINOP_REPEAT); }
631 { write_exp_elt_opcode (BINOP_MUL); }
635 { write_exp_elt_opcode (BINOP_DIV); }
639 { write_exp_elt_opcode (BINOP_REM); }
643 { write_exp_elt_opcode (BINOP_ADD); }
647 { write_exp_elt_opcode (BINOP_SUB); }
651 { write_exp_elt_opcode (BINOP_LSH); }
655 { write_exp_elt_opcode (BINOP_RSH); }
659 { write_exp_elt_opcode (BINOP_EQUAL); }
662 exp : exp NOTEQUAL exp
663 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
667 { write_exp_elt_opcode (BINOP_LEQ); }
671 { write_exp_elt_opcode (BINOP_GEQ); }
675 { write_exp_elt_opcode (BINOP_LESS); }
679 { write_exp_elt_opcode (BINOP_GTR); }
683 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
687 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
691 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
695 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
699 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
702 exp : exp '?' exp ':' exp %prec '?'
703 { write_exp_elt_opcode (TERNOP_COND); }
707 { write_exp_elt_opcode (BINOP_ASSIGN); }
710 exp : exp ASSIGN_MODIFY exp
711 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
712 write_exp_elt_opcode ($2);
713 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
717 { write_exp_elt_opcode (OP_LONG);
718 write_exp_elt_type ($1.type);
719 write_exp_elt_longcst ((LONGEST)($1.val));
720 write_exp_elt_opcode (OP_LONG); }
725 struct stoken_vector vec;
728 write_exp_string_vector ($1.type, &vec);
734 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
735 write_exp_elt_opcode (OP_LONG);
736 write_exp_elt_type (val.typed_val_int.type);
737 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
738 write_exp_elt_opcode (OP_LONG);
744 { write_exp_elt_opcode (OP_DOUBLE);
745 write_exp_elt_type ($1.type);
746 write_exp_elt_dblcst ($1.dval);
747 write_exp_elt_opcode (OP_DOUBLE); }
751 { write_exp_elt_opcode (OP_DECFLOAT);
752 write_exp_elt_type ($1.type);
753 write_exp_elt_decfloatcst ($1.val);
754 write_exp_elt_opcode (OP_DECFLOAT); }
762 write_dollar_variable ($1);
766 exp : SELECTOR '(' name ')'
768 write_exp_elt_opcode (OP_OBJC_SELECTOR);
769 write_exp_string ($3);
770 write_exp_elt_opcode (OP_OBJC_SELECTOR); }
773 exp : SIZEOF '(' type ')' %prec UNARY
774 { write_exp_elt_opcode (OP_LONG);
775 write_exp_elt_type (lookup_signed_typename
776 (parse_language, parse_gdbarch,
779 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
780 write_exp_elt_opcode (OP_LONG); }
783 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
784 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
787 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
788 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
791 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
792 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
795 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
796 { /* We could do more error checking here, but
797 it doesn't seem worthwhile. */
798 write_exp_elt_opcode (UNOP_CAST_TYPE); }
804 /* We copy the string here, and not in the
805 lexer, to guarantee that we do not leak a
806 string. Note that we follow the
807 NUL-termination convention of the
809 struct typed_stoken *vec = XNEW (struct typed_stoken);
814 vec->length = $1.length;
815 vec->ptr = malloc ($1.length + 1);
816 memcpy (vec->ptr, $1.ptr, $1.length + 1);
821 /* Note that we NUL-terminate here, but just
825 $$.tokens = realloc ($$.tokens,
826 $$.len * sizeof (struct typed_stoken));
828 p = malloc ($2.length + 1);
829 memcpy (p, $2.ptr, $2.length + 1);
831 $$.tokens[$$.len - 1].type = $2.type;
832 $$.tokens[$$.len - 1].length = $2.length;
833 $$.tokens[$$.len - 1].ptr = p;
840 enum c_string_type type = C_STRING;
842 for (i = 0; i < $1.len; ++i)
844 switch ($1.tokens[i].type)
852 && type != $1.tokens[i].type)
853 error (_("Undefined string concatenation."));
854 type = $1.tokens[i].type;
858 internal_error (__FILE__, __LINE__,
859 "unrecognized type in string concatenation");
863 write_exp_string_vector (type, &$1);
864 for (i = 0; i < $1.len; ++i)
865 free ($1.tokens[i].ptr);
870 exp : NSSTRING /* ObjC NextStep NSString constant
871 * of the form '@' '"' string '"'.
873 { write_exp_elt_opcode (OP_OBJC_NSSTRING);
874 write_exp_string ($1);
875 write_exp_elt_opcode (OP_OBJC_NSSTRING); }
880 { write_exp_elt_opcode (OP_LONG);
881 write_exp_elt_type (parse_type->builtin_bool);
882 write_exp_elt_longcst ((LONGEST) 1);
883 write_exp_elt_opcode (OP_LONG); }
887 { write_exp_elt_opcode (OP_LONG);
888 write_exp_elt_type (parse_type->builtin_bool);
889 write_exp_elt_longcst ((LONGEST) 0);
890 write_exp_elt_opcode (OP_LONG); }
898 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
900 error (_("No file or function \"%s\"."),
901 copy_name ($1.stoken));
909 block : block COLONCOLON name
911 = lookup_symbol (copy_name ($3), $1,
913 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
914 error (_("No function \"%s\" in specified context."),
916 $$ = SYMBOL_BLOCK_VALUE (tem); }
919 variable: name_not_typename ENTRY
920 { struct symbol *sym = $1.sym;
922 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
923 || !symbol_read_needs_frame (sym))
924 error (_("@entry can be used only for function "
925 "parameters, not for \"%s\""),
926 copy_name ($1.stoken));
928 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
929 write_exp_elt_sym (sym);
930 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
934 variable: block COLONCOLON name
935 { struct symbol *sym;
936 sym = lookup_symbol (copy_name ($3), $1,
939 error (_("No symbol \"%s\" in specified context."),
941 if (symbol_read_needs_frame (sym))
943 if (innermost_block == 0
944 || contained_in (block_found,
946 innermost_block = block_found;
949 write_exp_elt_opcode (OP_VAR_VALUE);
950 /* block_found is set by lookup_symbol. */
951 write_exp_elt_block (block_found);
952 write_exp_elt_sym (sym);
953 write_exp_elt_opcode (OP_VAR_VALUE); }
956 qualified_name: TYPENAME COLONCOLON name
958 struct type *type = $1.type;
959 CHECK_TYPEDEF (type);
960 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
961 && TYPE_CODE (type) != TYPE_CODE_UNION
962 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
963 error (_("`%s' is not defined as an aggregate type."),
964 TYPE_SAFE_NAME (type));
966 write_exp_elt_opcode (OP_SCOPE);
967 write_exp_elt_type (type);
968 write_exp_string ($3);
969 write_exp_elt_opcode (OP_SCOPE);
971 | TYPENAME COLONCOLON '~' name
973 struct type *type = $1.type;
974 struct stoken tmp_token;
977 CHECK_TYPEDEF (type);
978 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
979 && TYPE_CODE (type) != TYPE_CODE_UNION
980 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
981 error (_("`%s' is not defined as an aggregate type."),
982 TYPE_SAFE_NAME (type));
983 buf = alloca ($4.length + 2);
985 tmp_token.length = $4.length + 1;
987 memcpy (buf+1, $4.ptr, $4.length);
988 buf[tmp_token.length] = 0;
990 /* Check for valid destructor name. */
991 destructor_name_p (tmp_token.ptr, $1.type);
992 write_exp_elt_opcode (OP_SCOPE);
993 write_exp_elt_type (type);
994 write_exp_string (tmp_token);
995 write_exp_elt_opcode (OP_SCOPE);
997 | TYPENAME COLONCOLON name COLONCOLON name
999 char *copy = copy_name ($3);
1000 error (_("No type \"%s\" within class "
1001 "or namespace \"%s\"."),
1002 copy, TYPE_SAFE_NAME ($1.type));
1006 variable: qualified_name
1007 | COLONCOLON name_not_typename
1009 char *name = copy_name ($2.stoken);
1011 struct bound_minimal_symbol msymbol;
1014 lookup_symbol (name, (const struct block *) NULL,
1018 write_exp_elt_opcode (OP_VAR_VALUE);
1019 write_exp_elt_block (NULL);
1020 write_exp_elt_sym (sym);
1021 write_exp_elt_opcode (OP_VAR_VALUE);
1025 msymbol = lookup_bound_minimal_symbol (name);
1026 if (msymbol.minsym != NULL)
1027 write_exp_msymbol (msymbol);
1028 else if (!have_full_symbols () && !have_partial_symbols ())
1029 error (_("No symbol table is loaded. Use the \"file\" command."));
1031 error (_("No symbol \"%s\" in current context."), name);
1035 variable: name_not_typename
1036 { struct symbol *sym = $1.sym;
1040 if (symbol_read_needs_frame (sym))
1042 if (innermost_block == 0
1043 || contained_in (block_found,
1045 innermost_block = block_found;
1048 write_exp_elt_opcode (OP_VAR_VALUE);
1049 /* We want to use the selected frame, not
1050 another more inner frame which happens to
1051 be in the same block. */
1052 write_exp_elt_block (NULL);
1053 write_exp_elt_sym (sym);
1054 write_exp_elt_opcode (OP_VAR_VALUE);
1056 else if ($1.is_a_field_of_this)
1058 /* C++: it hangs off of `this'. Must
1059 not inadvertently convert from a method call
1061 if (innermost_block == 0
1062 || contained_in (block_found,
1064 innermost_block = block_found;
1065 write_exp_elt_opcode (OP_THIS);
1066 write_exp_elt_opcode (OP_THIS);
1067 write_exp_elt_opcode (STRUCTOP_PTR);
1068 write_exp_string ($1.stoken);
1069 write_exp_elt_opcode (STRUCTOP_PTR);
1073 struct bound_minimal_symbol msymbol;
1074 char *arg = copy_name ($1.stoken);
1077 lookup_bound_minimal_symbol (arg);
1078 if (msymbol.minsym != NULL)
1079 write_exp_msymbol (msymbol);
1080 else if (!have_full_symbols () && !have_partial_symbols ())
1081 error (_("No symbol table is loaded. Use the \"file\" command."));
1083 error (_("No symbol \"%s\" in current context."),
1084 copy_name ($1.stoken));
1089 space_identifier : '@' NAME
1090 { insert_type_address_space (copy_name ($2.stoken)); }
1093 const_or_volatile: const_or_volatile_noopt
1097 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1100 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1101 | const_or_volatile_noopt
1104 const_or_volatile_or_space_identifier:
1105 const_or_volatile_or_space_identifier_noopt
1111 { insert_type (tp_pointer); }
1112 const_or_volatile_or_space_identifier
1114 { insert_type (tp_pointer); }
1115 const_or_volatile_or_space_identifier
1117 { insert_type (tp_reference); }
1119 { insert_type (tp_reference); }
1122 ptr_operator_ts: ptr_operator
1124 $$ = get_type_stack ();
1125 /* This cleanup is eventually run by
1127 make_cleanup (type_stack_cleanup, $$);
1131 abs_decl: ptr_operator_ts direct_abs_decl
1132 { $$ = append_type_stack ($2, $1); }
1137 direct_abs_decl: '(' abs_decl ')'
1139 | direct_abs_decl array_mod
1141 push_type_stack ($1);
1143 push_type (tp_array);
1144 $$ = get_type_stack ();
1149 push_type (tp_array);
1150 $$ = get_type_stack ();
1153 | direct_abs_decl func_mod
1155 push_type_stack ($1);
1157 $$ = get_type_stack ();
1162 $$ = get_type_stack ();
1172 | OBJC_LBRAC INT ']'
1178 | '(' parameter_typelist ')'
1182 /* We used to try to recognize pointer to member types here, but
1183 that didn't work (shift/reduce conflicts meant that these rules never
1184 got executed). The problem is that
1185 int (foo::bar::baz::bizzle)
1186 is a function type but
1187 int (foo::bar::baz::bizzle::*)
1188 is a pointer to member type. Stroustrup loses again! */
1193 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1197 { $$ = lookup_signed_typename (parse_language,
1201 { $$ = lookup_signed_typename (parse_language,
1205 { $$ = lookup_signed_typename (parse_language,
1209 { $$ = lookup_signed_typename (parse_language,
1212 | LONG SIGNED_KEYWORD INT_KEYWORD
1213 { $$ = lookup_signed_typename (parse_language,
1216 | LONG SIGNED_KEYWORD
1217 { $$ = lookup_signed_typename (parse_language,
1220 | SIGNED_KEYWORD LONG INT_KEYWORD
1221 { $$ = lookup_signed_typename (parse_language,
1224 | UNSIGNED LONG INT_KEYWORD
1225 { $$ = lookup_unsigned_typename (parse_language,
1228 | LONG UNSIGNED INT_KEYWORD
1229 { $$ = lookup_unsigned_typename (parse_language,
1233 { $$ = lookup_unsigned_typename (parse_language,
1237 { $$ = lookup_signed_typename (parse_language,
1240 | LONG LONG INT_KEYWORD
1241 { $$ = lookup_signed_typename (parse_language,
1244 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1245 { $$ = lookup_signed_typename (parse_language,
1248 | LONG LONG SIGNED_KEYWORD
1249 { $$ = lookup_signed_typename (parse_language,
1252 | SIGNED_KEYWORD LONG LONG
1253 { $$ = lookup_signed_typename (parse_language,
1256 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1257 { $$ = lookup_signed_typename (parse_language,
1260 | UNSIGNED LONG LONG
1261 { $$ = lookup_unsigned_typename (parse_language,
1264 | UNSIGNED LONG LONG INT_KEYWORD
1265 { $$ = lookup_unsigned_typename (parse_language,
1268 | LONG LONG UNSIGNED
1269 { $$ = lookup_unsigned_typename (parse_language,
1272 | LONG LONG UNSIGNED INT_KEYWORD
1273 { $$ = lookup_unsigned_typename (parse_language,
1277 { $$ = lookup_signed_typename (parse_language,
1280 | SHORT SIGNED_KEYWORD INT_KEYWORD
1281 { $$ = lookup_signed_typename (parse_language,
1284 | SHORT SIGNED_KEYWORD
1285 { $$ = lookup_signed_typename (parse_language,
1288 | UNSIGNED SHORT INT_KEYWORD
1289 { $$ = lookup_unsigned_typename (parse_language,
1293 { $$ = lookup_unsigned_typename (parse_language,
1296 | SHORT UNSIGNED INT_KEYWORD
1297 { $$ = lookup_unsigned_typename (parse_language,
1301 { $$ = lookup_typename (parse_language, parse_gdbarch,
1302 "double", (struct block *) NULL,
1304 | LONG DOUBLE_KEYWORD
1305 { $$ = lookup_typename (parse_language, parse_gdbarch,
1307 (struct block *) NULL, 0); }
1309 { $$ = lookup_struct (copy_name ($2),
1310 expression_context_block); }
1313 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1316 | STRUCT name COMPLETE
1318 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1323 { $$ = lookup_struct (copy_name ($2),
1324 expression_context_block); }
1327 mark_completion_tag (TYPE_CODE_CLASS, "", 0);
1330 | CLASS name COMPLETE
1332 mark_completion_tag (TYPE_CODE_CLASS, $2.ptr,
1337 { $$ = lookup_union (copy_name ($2),
1338 expression_context_block); }
1341 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1344 | UNION name COMPLETE
1346 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1351 { $$ = lookup_enum (copy_name ($2),
1352 expression_context_block); }
1355 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1358 | ENUM name COMPLETE
1360 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1365 { $$ = lookup_unsigned_typename (parse_language,
1367 TYPE_NAME($2.type)); }
1369 { $$ = lookup_unsigned_typename (parse_language,
1372 | SIGNED_KEYWORD typename
1373 { $$ = lookup_signed_typename (parse_language,
1375 TYPE_NAME($2.type)); }
1377 { $$ = lookup_signed_typename (parse_language,
1380 /* It appears that this rule for templates is never
1381 reduced; template recognition happens by lookahead
1382 in the token processing code in yylex. */
1383 | TEMPLATE name '<' type '>'
1384 { $$ = lookup_template_type(copy_name($2), $4,
1385 expression_context_block);
1387 | const_or_volatile_or_space_identifier_noopt typebase
1388 { $$ = follow_types ($2); }
1389 | typebase const_or_volatile_or_space_identifier_noopt
1390 { $$ = follow_types ($1); }
1396 $$.stoken.ptr = "int";
1397 $$.stoken.length = 3;
1398 $$.type = lookup_signed_typename (parse_language,
1404 $$.stoken.ptr = "long";
1405 $$.stoken.length = 4;
1406 $$.type = lookup_signed_typename (parse_language,
1412 $$.stoken.ptr = "short";
1413 $$.stoken.length = 5;
1414 $$.type = lookup_signed_typename (parse_language,
1422 { check_parameter_typelist ($1); }
1423 | nonempty_typelist ',' DOTDOTDOT
1425 VEC_safe_push (type_ptr, $1, NULL);
1426 check_parameter_typelist ($1);
1434 VEC (type_ptr) *typelist = NULL;
1435 VEC_safe_push (type_ptr, typelist, $1);
1438 | nonempty_typelist ',' type
1440 VEC_safe_push (type_ptr, $1, $3);
1448 push_type_stack ($2);
1449 $$ = follow_types ($1);
1453 conversion_type_id: typebase conversion_declarator
1454 { $$ = follow_types ($1); }
1457 conversion_declarator: /* Nothing. */
1458 | ptr_operator conversion_declarator
1461 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1462 | VOLATILE_KEYWORD CONST_KEYWORD
1465 const_or_volatile_noopt: const_and_volatile
1466 { insert_type (tp_const);
1467 insert_type (tp_volatile);
1470 { insert_type (tp_const); }
1472 { insert_type (tp_volatile); }
1475 operator: OPERATOR NEW
1476 { $$ = operator_stoken (" new"); }
1478 { $$ = operator_stoken (" delete"); }
1479 | OPERATOR NEW '[' ']'
1480 { $$ = operator_stoken (" new[]"); }
1481 | OPERATOR DELETE '[' ']'
1482 { $$ = operator_stoken (" delete[]"); }
1483 | OPERATOR NEW OBJC_LBRAC ']'
1484 { $$ = operator_stoken (" new[]"); }
1485 | OPERATOR DELETE OBJC_LBRAC ']'
1486 { $$ = operator_stoken (" delete[]"); }
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 ("<"); }
1512 { $$ = operator_stoken (">"); }
1513 | OPERATOR ASSIGN_MODIFY
1514 { const char *op = "unknown";
1538 case BINOP_BITWISE_IOR:
1541 case BINOP_BITWISE_AND:
1544 case BINOP_BITWISE_XOR:
1551 $$ = operator_stoken (op);
1554 { $$ = operator_stoken ("<<"); }
1556 { $$ = operator_stoken (">>"); }
1558 { $$ = operator_stoken ("=="); }
1560 { $$ = operator_stoken ("!="); }
1562 { $$ = operator_stoken ("<="); }
1564 { $$ = operator_stoken (">="); }
1566 { $$ = operator_stoken ("&&"); }
1568 { $$ = operator_stoken ("||"); }
1569 | OPERATOR INCREMENT
1570 { $$ = operator_stoken ("++"); }
1571 | OPERATOR DECREMENT
1572 { $$ = operator_stoken ("--"); }
1574 { $$ = operator_stoken (","); }
1575 | OPERATOR ARROW_STAR
1576 { $$ = operator_stoken ("->*"); }
1578 { $$ = operator_stoken ("->"); }
1580 { $$ = operator_stoken ("()"); }
1582 { $$ = operator_stoken ("[]"); }
1583 | OPERATOR OBJC_LBRAC ']'
1584 { $$ = operator_stoken ("[]"); }
1585 | OPERATOR conversion_type_id
1588 struct ui_file *buf = mem_fileopen ();
1590 c_print_type ($2, NULL, buf, -1, 0,
1591 &type_print_raw_options);
1592 name = ui_file_xstrdup (buf, &length);
1593 ui_file_delete (buf);
1594 $$ = operator_stoken (name);
1601 name : NAME { $$ = $1.stoken; }
1602 | BLOCKNAME { $$ = $1.stoken; }
1603 | TYPENAME { $$ = $1.stoken; }
1604 | NAME_OR_INT { $$ = $1.stoken; }
1605 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1606 | operator { $$ = $1; }
1609 name_not_typename : NAME
1611 /* These would be useful if name_not_typename was useful, but it is just
1612 a fake for "variable", so these cause reduce/reduce conflicts because
1613 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1614 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1615 context where only a name could occur, this might be useful.
1620 struct field_of_this_result is_a_field_of_this;
1623 $$.sym = lookup_symbol ($1.ptr,
1624 expression_context_block,
1626 &is_a_field_of_this);
1627 $$.is_a_field_of_this
1628 = is_a_field_of_this.type != NULL;
1635 /* Like write_exp_string, but prepends a '~'. */
1638 write_destructor_name (struct stoken token)
1640 char *copy = alloca (token.length + 1);
1643 memcpy (©[1], token.ptr, token.length);
1648 write_exp_string (token);
1651 /* Returns a stoken of the operator name given by OP (which does not
1652 include the string "operator"). */
1653 static struct stoken
1654 operator_stoken (const char *op)
1656 static const char *operator_string = "operator";
1657 struct stoken st = { NULL, 0 };
1660 st.length = strlen (operator_string) + strlen (op);
1661 buf = malloc (st.length + 1);
1662 strcpy (buf, operator_string);
1666 /* The toplevel (c_parse) will free the memory allocated here. */
1667 make_cleanup (free, buf);
1671 /* Validate a parameter typelist. */
1674 check_parameter_typelist (VEC (type_ptr) *params)
1679 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1681 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1685 if (VEC_length (type_ptr, params) == 1)
1690 VEC_free (type_ptr, params);
1691 error (_("parameter types following 'void'"));
1695 VEC_free (type_ptr, params);
1696 error (_("'void' invalid as parameter type"));
1702 /* Take care of parsing a number (anything that starts with a digit).
1703 Set yylval and return the token type; update lexptr.
1704 LEN is the number of characters in it. */
1706 /*** Needs some error checking for the float case ***/
1709 parse_number (const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1711 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1712 here, and we do kind of silly things like cast to unsigned. */
1719 int base = input_radix;
1722 /* Number of "L" suffixes encountered. */
1725 /* We have found a "L" or "U" suffix. */
1726 int found_suffix = 0;
1729 struct type *signed_type;
1730 struct type *unsigned_type;
1734 memcpy (p, buf, len);
1738 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1739 point. Return DECFLOAT. */
1741 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1744 putithere->typed_val_decfloat.type
1745 = parse_type->builtin_decfloat;
1746 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1747 gdbarch_byte_order (parse_gdbarch), p);
1752 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1755 putithere->typed_val_decfloat.type
1756 = parse_type->builtin_decdouble;
1757 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1758 gdbarch_byte_order (parse_gdbarch), p);
1763 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1766 putithere->typed_val_decfloat.type
1767 = parse_type->builtin_declong;
1768 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1769 gdbarch_byte_order (parse_gdbarch), p);
1774 if (! parse_c_float (parse_gdbarch, p, len,
1775 &putithere->typed_val_float.dval,
1776 &putithere->typed_val_float.type))
1781 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1825 if (c >= 'A' && c <= 'Z')
1827 if (c != 'l' && c != 'u')
1829 if (c >= '0' && c <= '9')
1837 if (base > 10 && c >= 'a' && c <= 'f')
1841 n += i = c - 'a' + 10;
1854 return ERROR; /* Char not a digit */
1857 return ERROR; /* Invalid digit in this base */
1859 /* Portably test for overflow (only works for nonzero values, so make
1860 a second check for zero). FIXME: Can't we just make n and prevn
1861 unsigned and avoid this? */
1862 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1863 unsigned_p = 1; /* Try something unsigned */
1865 /* Portably test for unsigned overflow.
1866 FIXME: This check is wrong; for example it doesn't find overflow
1867 on 0x123456789 when LONGEST is 32 bits. */
1868 if (c != 'l' && c != 'u' && n != 0)
1870 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1871 error (_("Numeric constant too large."));
1876 /* An integer constant is an int, a long, or a long long. An L
1877 suffix forces it to be long; an LL suffix forces it to be long
1878 long. If not forced to a larger size, it gets the first type of
1879 the above that it fits in. To figure out whether it fits, we
1880 shift it right and see whether anything remains. Note that we
1881 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1882 operation, because many compilers will warn about such a shift
1883 (which always produces a zero result). Sometimes gdbarch_int_bit
1884 or gdbarch_long_bit will be that big, sometimes not. To deal with
1885 the case where it is we just always shift the value more than
1886 once, with fewer bits each time. */
1888 un = (ULONGEST)n >> 2;
1890 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1892 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1894 /* A large decimal (not hex or octal) constant (between INT_MAX
1895 and UINT_MAX) is a long or unsigned long, according to ANSI,
1896 never an unsigned int, but this code treats it as unsigned
1897 int. This probably should be fixed. GCC gives a warning on
1900 unsigned_type = parse_type->builtin_unsigned_int;
1901 signed_type = parse_type->builtin_int;
1903 else if (long_p <= 1
1904 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1906 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1907 unsigned_type = parse_type->builtin_unsigned_long;
1908 signed_type = parse_type->builtin_long;
1913 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1914 < gdbarch_long_long_bit (parse_gdbarch))
1915 /* A long long does not fit in a LONGEST. */
1916 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1918 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1919 high_bit = (ULONGEST) 1 << shift;
1920 unsigned_type = parse_type->builtin_unsigned_long_long;
1921 signed_type = parse_type->builtin_long_long;
1924 putithere->typed_val_int.val = n;
1926 /* If the high bit of the worked out type is set then this number
1927 has to be unsigned. */
1929 if (unsigned_p || (n & high_bit))
1931 putithere->typed_val_int.type = unsigned_type;
1935 putithere->typed_val_int.type = signed_type;
1941 /* Temporary obstack used for holding strings. */
1942 static struct obstack tempbuf;
1943 static int tempbuf_init;
1945 /* Parse a C escape sequence. The initial backslash of the sequence
1946 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1947 last character of the sequence. If OUTPUT is not NULL, the
1948 translated form of the escape sequence will be written there. If
1949 OUTPUT is NULL, no output is written and the call will only affect
1950 *PTR. If an escape sequence is expressed in target bytes, then the
1951 entire sequence will simply be copied to OUTPUT. Return 1 if any
1952 character was emitted, 0 otherwise. */
1955 c_parse_escape (const char **ptr, struct obstack *output)
1957 const char *tokptr = *ptr;
1960 /* Some escape sequences undergo character set conversion. Those we
1964 /* Hex escapes do not undergo character set conversion, so keep
1965 the escape sequence for later. */
1968 obstack_grow_str (output, "\\x");
1970 if (!isxdigit (*tokptr))
1971 error (_("\\x escape without a following hex digit"));
1972 while (isxdigit (*tokptr))
1975 obstack_1grow (output, *tokptr);
1980 /* Octal escapes do not undergo character set conversion, so
1981 keep the escape sequence for later. */
1993 obstack_grow_str (output, "\\");
1995 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1999 obstack_1grow (output, *tokptr);
2005 /* We handle UCNs later. We could handle them here, but that
2006 would mean a spurious error in the case where the UCN could
2007 be converted to the target charset but not the host
2013 int i, len = c == 'U' ? 8 : 4;
2016 obstack_1grow (output, '\\');
2017 obstack_1grow (output, *tokptr);
2020 if (!isxdigit (*tokptr))
2021 error (_("\\%c escape without a following hex digit"), c);
2022 for (i = 0; i < len && isxdigit (*tokptr); ++i)
2025 obstack_1grow (output, *tokptr);
2031 /* We must pass backslash through so that it does not
2032 cause quoting during the second expansion. */
2035 obstack_grow_str (output, "\\\\");
2039 /* Escapes which undergo conversion. */
2042 obstack_1grow (output, '\a');
2047 obstack_1grow (output, '\b');
2052 obstack_1grow (output, '\f');
2057 obstack_1grow (output, '\n');
2062 obstack_1grow (output, '\r');
2067 obstack_1grow (output, '\t');
2072 obstack_1grow (output, '\v');
2076 /* GCC extension. */
2079 obstack_1grow (output, HOST_ESCAPE_CHAR);
2083 /* Backslash-newline expands to nothing at all. */
2089 /* A few escapes just expand to the character itself. */
2093 /* GCC extensions. */
2098 /* Unrecognized escapes turn into the character itself. */
2101 obstack_1grow (output, *tokptr);
2109 /* Parse a string or character literal from TOKPTR. The string or
2110 character may be wide or unicode. *OUTPTR is set to just after the
2111 end of the literal in the input string. The resulting token is
2112 stored in VALUE. This returns a token value, either STRING or
2113 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2114 number of host characters in the literal. */
2116 parse_string_or_char (const char *tokptr, const char **outptr,
2117 struct typed_stoken *value, int *host_chars)
2120 enum c_string_type type;
2123 /* Build the gdb internal form of the input string in tempbuf. Note
2124 that the buffer is null byte terminated *only* for the
2125 convenience of debugging gdb itself and printing the buffer
2126 contents when the buffer contains no embedded nulls. Gdb does
2127 not depend upon the buffer being null byte terminated, it uses
2128 the length string instead. This allows gdb to handle C strings
2129 (as well as strings in other languages) with embedded null
2135 obstack_free (&tempbuf, NULL);
2136 obstack_init (&tempbuf);
2138 /* Record the string type. */
2141 type = C_WIDE_STRING;
2144 else if (*tokptr == 'u')
2149 else if (*tokptr == 'U')
2154 else if (*tokptr == '@')
2156 /* An Objective C string. */
2164 /* Skip the quote. */
2178 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2180 else if (c == quote)
2184 obstack_1grow (&tempbuf, c);
2186 /* FIXME: this does the wrong thing with multi-byte host
2187 characters. We could use mbrlen here, but that would
2188 make "set host-charset" a bit less useful. */
2193 if (*tokptr != quote)
2196 error (_("Unterminated string in expression."));
2198 error (_("Unmatched single quote."));
2203 value->ptr = obstack_base (&tempbuf);
2204 value->length = obstack_object_size (&tempbuf);
2208 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2211 /* This is used to associate some attributes with a token. */
2215 /* If this bit is set, the token is C++-only. */
2219 /* If this bit is set, the token is conditional: if there is a
2220 symbol of the same name, then the token is a symbol; otherwise,
2221 the token is a keyword. */
2230 enum exp_opcode opcode;
2231 enum token_flags flags;
2234 static const struct token tokentab3[] =
2236 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2237 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2238 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2239 {"...", DOTDOTDOT, BINOP_END, 0}
2242 static const struct token tokentab2[] =
2244 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2245 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2246 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2247 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2248 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2249 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2250 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2251 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2252 {"++", INCREMENT, BINOP_END, 0},
2253 {"--", DECREMENT, BINOP_END, 0},
2254 {"->", ARROW, BINOP_END, 0},
2255 {"&&", ANDAND, BINOP_END, 0},
2256 {"||", OROR, BINOP_END, 0},
2257 /* "::" is *not* only C++: gdb overrides its meaning in several
2258 different ways, e.g., 'filename'::func, function::variable. */
2259 {"::", COLONCOLON, BINOP_END, 0},
2260 {"<<", LSH, BINOP_END, 0},
2261 {">>", RSH, BINOP_END, 0},
2262 {"==", EQUAL, BINOP_END, 0},
2263 {"!=", NOTEQUAL, BINOP_END, 0},
2264 {"<=", LEQ, BINOP_END, 0},
2265 {">=", GEQ, BINOP_END, 0},
2266 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2269 /* Identifier-like tokens. */
2270 static const struct token ident_tokens[] =
2272 {"unsigned", UNSIGNED, OP_NULL, 0},
2273 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2274 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2275 {"struct", STRUCT, OP_NULL, 0},
2276 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2277 {"sizeof", SIZEOF, OP_NULL, 0},
2278 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2279 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2280 {"class", CLASS, OP_NULL, FLAG_CXX},
2281 {"union", UNION, OP_NULL, 0},
2282 {"short", SHORT, OP_NULL, 0},
2283 {"const", CONST_KEYWORD, OP_NULL, 0},
2284 {"enum", ENUM, OP_NULL, 0},
2285 {"long", LONG, OP_NULL, 0},
2286 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2287 {"int", INT_KEYWORD, OP_NULL, 0},
2288 {"new", NEW, OP_NULL, FLAG_CXX},
2289 {"delete", DELETE, OP_NULL, FLAG_CXX},
2290 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2292 {"and", ANDAND, BINOP_END, FLAG_CXX},
2293 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2294 {"bitand", '&', OP_NULL, FLAG_CXX},
2295 {"bitor", '|', OP_NULL, FLAG_CXX},
2296 {"compl", '~', OP_NULL, FLAG_CXX},
2297 {"not", '!', OP_NULL, FLAG_CXX},
2298 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2299 {"or", OROR, BINOP_END, FLAG_CXX},
2300 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2301 {"xor", '^', OP_NULL, FLAG_CXX},
2302 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2304 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2305 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2306 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2307 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2309 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2310 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2311 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2312 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2313 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2315 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2318 /* When we find that lexptr (the global var defined in parse.c) is
2319 pointing at a macro invocation, we expand the invocation, and call
2320 scan_macro_expansion to save the old lexptr here and point lexptr
2321 into the expanded text. When we reach the end of that, we call
2322 end_macro_expansion to pop back to the value we saved here. The
2323 macro expansion code promises to return only fully-expanded text,
2324 so we don't need to "push" more than one level.
2326 This is disgusting, of course. It would be cleaner to do all macro
2327 expansion beforehand, and then hand that to lexptr. But we don't
2328 really know where the expression ends. Remember, in a command like
2330 (gdb) break *ADDRESS if CONDITION
2332 we evaluate ADDRESS in the scope of the current frame, but we
2333 evaluate CONDITION in the scope of the breakpoint's location. So
2334 it's simply wrong to try to macro-expand the whole thing at once. */
2335 static const char *macro_original_text;
2337 /* We save all intermediate macro expansions on this obstack for the
2338 duration of a single parse. The expansion text may sometimes have
2339 to live past the end of the expansion, due to yacc lookahead.
2340 Rather than try to be clever about saving the data for a single
2341 token, we simply keep it all and delete it after parsing has
2343 static struct obstack expansion_obstack;
2346 scan_macro_expansion (char *expansion)
2350 /* We'd better not be trying to push the stack twice. */
2351 gdb_assert (! macro_original_text);
2353 /* Copy to the obstack, and then free the intermediate
2355 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2358 /* Save the old lexptr value, so we can return to it when we're done
2359 parsing the expanded text. */
2360 macro_original_text = lexptr;
2366 scanning_macro_expansion (void)
2368 return macro_original_text != 0;
2373 finished_macro_expansion (void)
2375 /* There'd better be something to pop back to. */
2376 gdb_assert (macro_original_text);
2378 /* Pop back to the original text. */
2379 lexptr = macro_original_text;
2380 macro_original_text = 0;
2385 scan_macro_cleanup (void *dummy)
2387 if (macro_original_text)
2388 finished_macro_expansion ();
2390 obstack_free (&expansion_obstack, NULL);
2393 /* Return true iff the token represents a C++ cast operator. */
2396 is_cast_operator (const char *token, int len)
2398 return (! strncmp (token, "dynamic_cast", len)
2399 || ! strncmp (token, "static_cast", len)
2400 || ! strncmp (token, "reinterpret_cast", len)
2401 || ! strncmp (token, "const_cast", len));
2404 /* The scope used for macro expansion. */
2405 static struct macro_scope *expression_macro_scope;
2407 /* This is set if a NAME token appeared at the very end of the input
2408 string, with no whitespace separating the name from the EOF. This
2409 is used only when parsing to do field name completion. */
2410 static int saw_name_at_eof;
2412 /* This is set if the previously-returned token was a structure
2413 operator -- either '.' or ARROW. This is used only when parsing to
2414 do field name completion. */
2415 static int last_was_structop;
2417 /* Read one token, getting characters through lexptr. */
2420 lex_one_token (int *is_quoted_name)
2425 const char *tokstart;
2426 int saw_structop = last_was_structop;
2429 last_was_structop = 0;
2430 *is_quoted_name = 0;
2434 /* Check if this is a macro invocation that we need to expand. */
2435 if (! scanning_macro_expansion ())
2437 char *expanded = macro_expand_next (&lexptr,
2438 standard_macro_lookup,
2439 expression_macro_scope);
2442 scan_macro_expansion (expanded);
2445 prev_lexptr = lexptr;
2448 /* See if it is a special token of length 3. */
2449 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2450 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2452 if ((tokentab3[i].flags & FLAG_CXX) != 0
2453 && parse_language->la_language != language_cplus)
2457 yylval.opcode = tokentab3[i].opcode;
2458 return tokentab3[i].token;
2461 /* See if it is a special token of length 2. */
2462 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2463 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2465 if ((tokentab2[i].flags & FLAG_CXX) != 0
2466 && parse_language->la_language != language_cplus)
2470 yylval.opcode = tokentab2[i].opcode;
2471 if (parse_completion && tokentab2[i].token == ARROW)
2472 last_was_structop = 1;
2473 return tokentab2[i].token;
2476 switch (c = *tokstart)
2479 /* If we were just scanning the result of a macro expansion,
2480 then we need to resume scanning the original text.
2481 If we're parsing for field name completion, and the previous
2482 token allows such completion, return a COMPLETE token.
2483 Otherwise, we were already scanning the original text, and
2484 we're really done. */
2485 if (scanning_macro_expansion ())
2487 finished_macro_expansion ();
2490 else if (saw_name_at_eof)
2492 saw_name_at_eof = 0;
2495 else if (saw_structop)
2510 if (parse_language->la_language == language_objc && c == '[')
2516 if (paren_depth == 0)
2523 if (comma_terminates
2525 && ! scanning_macro_expansion ())
2531 /* Might be a floating point number. */
2532 if (lexptr[1] < '0' || lexptr[1] > '9')
2534 if (parse_completion)
2535 last_was_structop = 1;
2536 goto symbol; /* Nope, must be a symbol. */
2538 /* FALL THRU into number case. */
2551 /* It's a number. */
2552 int got_dot = 0, got_e = 0, toktype;
2553 const char *p = tokstart;
2554 int hex = input_radix > 10;
2556 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2561 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2569 /* This test includes !hex because 'e' is a valid hex digit
2570 and thus does not indicate a floating point number when
2571 the radix is hex. */
2572 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2573 got_dot = got_e = 1;
2574 /* This test does not include !hex, because a '.' always indicates
2575 a decimal floating point number regardless of the radix. */
2576 else if (!got_dot && *p == '.')
2578 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2579 && (*p == '-' || *p == '+'))
2580 /* This is the sign of the exponent, not the end of the
2583 /* We will take any letters or digits. parse_number will
2584 complain if past the radix, or if L or U are not final. */
2585 else if ((*p < '0' || *p > '9')
2586 && ((*p < 'a' || *p > 'z')
2587 && (*p < 'A' || *p > 'Z')))
2590 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2591 if (toktype == ERROR)
2593 char *err_copy = (char *) alloca (p - tokstart + 1);
2595 memcpy (err_copy, tokstart, p - tokstart);
2596 err_copy[p - tokstart] = 0;
2597 error (_("Invalid number \"%s\"."), err_copy);
2605 const char *p = &tokstart[1];
2606 size_t len = strlen ("entry");
2608 if (parse_language->la_language == language_objc)
2610 size_t len = strlen ("selector");
2612 if (strncmp (p, "selector", len) == 0
2613 && (p[len] == '\0' || isspace (p[len])))
2622 while (isspace (*p))
2624 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2656 if (tokstart[1] != '"' && tokstart[1] != '\'')
2665 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2670 error (_("Empty character constant."));
2671 else if (host_len > 2 && c == '\'')
2674 namelen = lexptr - tokstart - 1;
2675 *is_quoted_name = 1;
2679 else if (host_len > 1)
2680 error (_("Invalid character constant."));
2686 if (!(c == '_' || c == '$'
2687 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2688 /* We must have come across a bad character (e.g. ';'). */
2689 error (_("Invalid character '%c' in expression."), c);
2691 /* It's a name. See how long it is. */
2693 for (c = tokstart[namelen];
2694 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2695 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2697 /* Template parameter lists are part of the name.
2698 FIXME: This mishandles `print $a<4&&$a>3'. */
2702 if (! is_cast_operator (tokstart, namelen))
2704 /* Scan ahead to get rest of the template specification. Note
2705 that we look ahead only when the '<' adjoins non-whitespace
2706 characters; for comparison expressions, e.g. "a < b > c",
2707 there must be spaces before the '<', etc. */
2709 const char *p = find_template_name_end (tokstart + namelen);
2712 namelen = p - tokstart;
2716 c = tokstart[++namelen];
2719 /* The token "if" terminates the expression and is NOT removed from
2720 the input stream. It doesn't count if it appears in the
2721 expansion of a macro. */
2723 && tokstart[0] == 'i'
2724 && tokstart[1] == 'f'
2725 && ! scanning_macro_expansion ())
2730 /* For the same reason (breakpoint conditions), "thread N"
2731 terminates the expression. "thread" could be an identifier, but
2732 an identifier is never followed by a number without intervening
2733 punctuation. "task" is similar. Handle abbreviations of these,
2734 similarly to breakpoint.c:find_condition_and_thread. */
2736 && (strncmp (tokstart, "thread", namelen) == 0
2737 || strncmp (tokstart, "task", namelen) == 0)
2738 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2739 && ! scanning_macro_expansion ())
2741 const char *p = tokstart + namelen + 1;
2743 while (*p == ' ' || *p == '\t')
2745 if (*p >= '0' && *p <= '9')
2753 yylval.sval.ptr = tokstart;
2754 yylval.sval.length = namelen;
2756 /* Catch specific keywords. */
2757 copy = copy_name (yylval.sval);
2758 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2759 if (strcmp (copy, ident_tokens[i].operator) == 0)
2761 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2762 && parse_language->la_language != language_cplus)
2765 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2767 struct field_of_this_result is_a_field_of_this;
2769 if (lookup_symbol (copy, expression_context_block,
2771 (parse_language->la_language == language_cplus
2772 ? &is_a_field_of_this
2776 /* The keyword is shadowed. */
2781 /* It is ok to always set this, even though we don't always
2782 strictly need to. */
2783 yylval.opcode = ident_tokens[i].opcode;
2784 return ident_tokens[i].token;
2787 if (*tokstart == '$')
2790 if (parse_completion && *lexptr == '\0')
2791 saw_name_at_eof = 1;
2793 yylval.ssym.stoken = yylval.sval;
2794 yylval.ssym.sym = NULL;
2795 yylval.ssym.is_a_field_of_this = 0;
2799 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2806 DEF_VEC_O (token_and_value);
2808 /* A FIFO of tokens that have been read but not yet returned to the
2810 static VEC (token_and_value) *token_fifo;
2812 /* Non-zero if the lexer should return tokens from the FIFO. */
2815 /* Temporary storage for c_lex; this holds symbol names as they are
2817 static struct obstack name_obstack;
2819 /* Classify a NAME token. The contents of the token are in `yylval'.
2820 Updates yylval and returns the new token type. BLOCK is the block
2821 in which lookups start; this can be NULL to mean the global scope.
2822 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2823 in single quotes. */
2825 classify_name (const struct block *block, int is_quoted_name)
2829 struct field_of_this_result is_a_field_of_this;
2831 copy = copy_name (yylval.sval);
2833 /* Initialize this in case we *don't* use it in this call; that way
2834 we can refer to it unconditionally below. */
2835 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2837 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2838 parse_language->la_name_of_this
2839 ? &is_a_field_of_this : NULL);
2841 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2843 yylval.ssym.sym = sym;
2844 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2849 /* If we found a field of 'this', we might have erroneously
2850 found a constructor where we wanted a type name. Handle this
2851 case by noticing that we found a constructor and then look up
2852 the type tag instead. */
2853 if (is_a_field_of_this.type != NULL
2854 && is_a_field_of_this.fn_field != NULL
2855 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2858 struct field_of_this_result inner_is_a_field_of_this;
2860 sym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2861 &inner_is_a_field_of_this);
2864 yylval.tsym.type = SYMBOL_TYPE (sym);
2869 /* If we found a field, then we want to prefer it over a
2870 filename. However, if the name was quoted, then it is better
2871 to check for a filename or a block, since this is the only
2872 way the user has of requiring the extension to be used. */
2873 if (is_a_field_of_this.type == NULL || is_quoted_name)
2875 /* See if it's a file name. */
2876 struct symtab *symtab;
2878 symtab = lookup_symtab (copy);
2881 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
2888 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2890 yylval.tsym.type = SYMBOL_TYPE (sym);
2895 = language_lookup_primitive_type_by_name (parse_language,
2896 parse_gdbarch, copy);
2897 if (yylval.tsym.type != NULL)
2900 /* See if it's an ObjC classname. */
2901 if (parse_language->la_language == language_objc && !sym)
2903 CORE_ADDR Class = lookup_objc_class (parse_gdbarch, copy);
2906 yylval.class.class = Class;
2907 sym = lookup_struct_typedef (copy, expression_context_block, 1);
2909 yylval.class.type = SYMBOL_TYPE (sym);
2914 /* Input names that aren't symbols but ARE valid hex numbers, when
2915 the input radix permits them, can be names or numbers depending
2916 on the parse. Note we support radixes > 16 here. */
2918 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2919 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2921 YYSTYPE newlval; /* Its value is ignored. */
2922 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2925 yylval.ssym.sym = sym;
2926 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2931 /* Any other kind of symbol */
2932 yylval.ssym.sym = sym;
2933 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2936 && parse_language->la_language == language_cplus
2937 && is_a_field_of_this.type == NULL
2938 && lookup_minimal_symbol (copy, NULL, NULL).minsym == NULL)
2939 return UNKNOWN_CPP_NAME;
2944 /* Like classify_name, but used by the inner loop of the lexer, when a
2945 name might have already been seen. CONTEXT is the context type, or
2946 NULL if this is the first component of a name. */
2949 classify_inner_name (const struct block *block, struct type *context)
2954 if (context == NULL)
2955 return classify_name (block, 0);
2957 type = check_typedef (context);
2958 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2959 && TYPE_CODE (type) != TYPE_CODE_UNION
2960 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2963 copy = copy_name (yylval.ssym.stoken);
2964 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block);
2966 /* If no symbol was found, search for a matching base class named
2967 COPY. This will allow users to enter qualified names of class members
2968 relative to the `this' pointer. */
2969 if (yylval.ssym.sym == NULL)
2971 struct type *base_type = find_type_baseclass_by_name (type, copy);
2973 if (base_type != NULL)
2975 yylval.tsym.type = base_type;
2982 switch (SYMBOL_CLASS (yylval.ssym.sym))
2986 /* cp_lookup_nested_symbol might have accidentally found a constructor
2987 named COPY when we really wanted a base class of the same name.
2988 Double-check this case by looking for a base class. */
2990 struct type *base_type = find_type_baseclass_by_name (type, copy);
2992 if (base_type != NULL)
2994 yylval.tsym.type = base_type;
3001 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
3007 internal_error (__FILE__, __LINE__, _("not reached"));
3010 /* The outer level of a two-level lexer. This calls the inner lexer
3011 to return tokens. It then either returns these tokens, or
3012 aggregates them into a larger token. This lets us work around a
3013 problem in our parsing approach, where the parser could not
3014 distinguish between qualified names and qualified types at the
3017 This approach is still not ideal, because it mishandles template
3018 types. See the comment in lex_one_token for an example. However,
3019 this is still an improvement over the earlier approach, and will
3020 suffice until we move to better parsing technology. */
3024 token_and_value current;
3025 int first_was_coloncolon, last_was_coloncolon;
3026 struct type *context_type = NULL;
3027 int last_to_examine, next_to_examine, checkpoint;
3028 const struct block *search_block;
3031 if (popping && !VEC_empty (token_and_value, token_fifo))
3035 /* Read the first token and decide what to do. Most of the
3036 subsequent code is C++-only; but also depends on seeing a "::" or
3038 current.token = lex_one_token (&is_quoted_name);
3039 if (current.token == NAME)
3040 current.token = classify_name (expression_context_block, is_quoted_name);
3041 if (parse_language->la_language != language_cplus
3042 || (current.token != TYPENAME && current.token != COLONCOLON
3043 && current.token != FILENAME))
3044 return current.token;
3046 /* Read any sequence of alternating "::" and name-like tokens into
3048 current.value = yylval;
3049 VEC_safe_push (token_and_value, token_fifo, ¤t);
3050 last_was_coloncolon = current.token == COLONCOLON;
3055 /* We ignore quoted names other than the very first one.
3056 Subsequent ones do not have any special meaning. */
3057 current.token = lex_one_token (&ignore);
3058 current.value = yylval;
3059 VEC_safe_push (token_and_value, token_fifo, ¤t);
3061 if ((last_was_coloncolon && current.token != NAME)
3062 || (!last_was_coloncolon && current.token != COLONCOLON))
3064 last_was_coloncolon = !last_was_coloncolon;
3068 /* We always read one extra token, so compute the number of tokens
3069 to examine accordingly. */
3070 last_to_examine = VEC_length (token_and_value, token_fifo) - 2;
3071 next_to_examine = 0;
3073 current = *VEC_index (token_and_value, token_fifo, next_to_examine);
3076 obstack_free (&name_obstack, obstack_base (&name_obstack));
3078 if (current.token == FILENAME)
3079 search_block = current.value.bval;
3080 else if (current.token == COLONCOLON)
3081 search_block = NULL;
3084 gdb_assert (current.token == TYPENAME);
3085 search_block = expression_context_block;
3086 obstack_grow (&name_obstack, current.value.sval.ptr,
3087 current.value.sval.length);
3088 context_type = current.value.tsym.type;
3092 first_was_coloncolon = current.token == COLONCOLON;
3093 last_was_coloncolon = first_was_coloncolon;
3095 while (next_to_examine <= last_to_examine)
3097 token_and_value *next;
3099 next = VEC_index (token_and_value, token_fifo, next_to_examine);
3102 if (next->token == NAME && last_was_coloncolon)
3106 yylval = next->value;
3107 classification = classify_inner_name (search_block, context_type);
3108 /* We keep going until we either run out of names, or until
3109 we have a qualified name which is not a type. */
3110 if (classification != TYPENAME && classification != NAME)
3113 /* Accept up to this token. */
3114 checkpoint = next_to_examine;
3116 /* Update the partial name we are constructing. */
3117 if (context_type != NULL)
3119 /* We don't want to put a leading "::" into the name. */
3120 obstack_grow_str (&name_obstack, "::");
3122 obstack_grow (&name_obstack, next->value.sval.ptr,
3123 next->value.sval.length);
3125 yylval.sval.ptr = obstack_base (&name_obstack);
3126 yylval.sval.length = obstack_object_size (&name_obstack);
3127 current.value = yylval;
3128 current.token = classification;
3130 last_was_coloncolon = 0;
3132 if (classification == NAME)
3135 context_type = yylval.tsym.type;
3137 else if (next->token == COLONCOLON && !last_was_coloncolon)
3138 last_was_coloncolon = 1;
3141 /* We've reached the end of the name. */
3146 /* If we have a replacement token, install it as the first token in
3147 the FIFO, and delete the other constituent tokens. */
3150 current.value.sval.ptr = obstack_copy0 (&expansion_obstack,
3151 current.value.sval.ptr,
3152 current.value.sval.length);
3154 VEC_replace (token_and_value, token_fifo, 0, ¤t);
3156 VEC_block_remove (token_and_value, token_fifo, 1, checkpoint - 1);
3160 current = *VEC_index (token_and_value, token_fifo, 0);
3161 VEC_ordered_remove (token_and_value, token_fifo, 0);
3162 yylval = current.value;
3163 return current.token;
3170 struct cleanup *back_to = make_cleanup (free_current_contents,
3171 &expression_macro_scope);
3173 /* Set up the scope for macro expansion. */
3174 expression_macro_scope = NULL;
3176 if (expression_context_block)
3177 expression_macro_scope
3178 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
3180 expression_macro_scope = default_macro_scope ();
3181 if (! expression_macro_scope)
3182 expression_macro_scope = user_macro_scope ();
3184 /* Initialize macro expansion code. */
3185 obstack_init (&expansion_obstack);
3186 gdb_assert (! macro_original_text);
3187 make_cleanup (scan_macro_cleanup, 0);
3189 make_cleanup_restore_integer (&yydebug);
3190 yydebug = parser_debug;
3192 /* Initialize some state used by the lexer. */
3193 last_was_structop = 0;
3194 saw_name_at_eof = 0;
3196 VEC_free (token_and_value, token_fifo);
3198 obstack_init (&name_obstack);
3199 make_cleanup_obstack_free (&name_obstack);
3201 result = yyparse ();
3202 do_cleanups (back_to);
3208 /* This is called via the YYPRINT macro when parser debugging is
3209 enabled. It prints a token's value. */
3212 c_print_token (FILE *file, int type, YYSTYPE value)
3217 fprintf (file, "typed_val_int<%s, %s>",
3218 TYPE_SAFE_NAME (value.typed_val_int.type),
3219 pulongest (value.typed_val_int.val));
3225 char *copy = alloca (value.tsval.length + 1);
3227 memcpy (copy, value.tsval.ptr, value.tsval.length);
3228 copy[value.tsval.length] = '\0';
3230 fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3236 fprintf (file, "sval<%s>", copy_name (value.sval));
3240 fprintf (file, "tsym<type=%s, name=%s>",
3241 TYPE_SAFE_NAME (value.tsym.type),
3242 copy_name (value.tsym.stoken));
3246 case UNKNOWN_CPP_NAME:
3249 fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3250 copy_name (value.ssym.stoken),
3251 (value.ssym.sym == NULL
3252 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym)),
3253 value.ssym.is_a_field_of_this);
3257 fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3268 lexptr = prev_lexptr;
3270 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);