1 /* YACC parser for D expressions, for GDB.
3 Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This file is derived from c-exp.y, jv-exp.y. */
22 /* Parse a D expression from text in a string,
23 and return the result as a struct expression pointer.
24 That structure contains arithmetic operations in reverse polish,
25 with constants represented by operations that are followed by special data.
26 See expression.h for the details of the format.
27 What is important here is that it can be built up sequentially
28 during the process of parsing; the lower levels of the tree always
29 come first in the result.
31 Note that malloc's and realloc's in this file are transformed to
32 xmalloc and xrealloc respectively by the same sed command in the
33 makefile that remaps any other malloc/realloc inserted by the parser
34 generator. Doing this with #defines and trying to control the interaction
35 with include files (<malloc.h> and <stdlib.h> for example) just became
36 too messy, particularly when such includes can be inserted at random
37 times by the parser generator. */
43 #include "expression.h"
45 #include "parser-defs.h"
49 #include "bfd.h" /* Required by objfiles.h. */
50 #include "symfile.h" /* Required by objfiles.h. */
51 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
55 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
56 #define parse_d_type(ps) builtin_d_type (parse_gdbarch (ps))
58 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
60 #define GDB_YY_REMAP_PREFIX d_
63 /* The state of the parser, used internally when we are parsing the
66 static struct parser_state
*pstate
= NULL
;
70 static int yylex (void);
72 void yyerror (char *);
74 static int type_aggregate_p
(struct type
*);
78 /* Although the yacc "value" of an expression is not used,
79 since the result is stored in the structure being created,
80 other node types do have values. */
94 struct typed_stoken tsval
;
101 enum exp_opcode opcode
;
102 struct stoken_vector svec
;
106 /* YYSTYPE gets defined by %union */
107 static int parse_number
(struct parser_state
*, const char *,
108 int, int, YYSTYPE *);
111 %token
<sval
> IDENTIFIER UNKNOWN_NAME
112 %token
<tsym
> TYPENAME
113 %token
<voidval
> COMPLETE
115 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
116 but which would parse as a valid number in the current input radix.
117 E.g. "c" when input_radix==16. Depending on the parse, it will be
118 turned into a name or into a number. */
120 %token
<sval
> NAME_OR_INT
122 %token
<typed_val_int
> INTEGER_LITERAL
123 %token
<typed_val_float
> FLOAT_LITERAL
124 %token
<tsval
> CHARACTER_LITERAL
125 %token
<tsval
> STRING_LITERAL
127 %type
<svec
> StringExp
128 %type
<tval
> BasicType TypeExp
129 %type
<sval
> IdentifierExp
130 %type
<ival
> ArrayLiteral
135 /* Keywords that have a constant value. */
136 %token TRUE_KEYWORD FALSE_KEYWORD NULL_KEYWORD
137 /* Class 'super' accessor. */
140 %token CAST_KEYWORD SIZEOF_KEYWORD
141 %token TYPEOF_KEYWORD TYPEID_KEYWORD
143 /* Comparison keywords. */
144 /* Type storage classes. */
145 %token IMMUTABLE_KEYWORD CONST_KEYWORD SHARED_KEYWORD
146 /* Non-scalar type keywords. */
147 %token STRUCT_KEYWORD UNION_KEYWORD
148 %token CLASS_KEYWORD INTERFACE_KEYWORD
149 %token ENUM_KEYWORD TEMPLATE_KEYWORD
150 %token DELEGATE_KEYWORD FUNCTION_KEYWORD
152 %token
<sval
> DOLLAR_VARIABLE
154 %token
<opcode
> ASSIGN_MODIFY
157 %right
'=' ASSIGN_MODIFY
164 %left EQUAL NOTEQUAL
'<' '>' LEQ GEQ
169 %left IDENTITY NOTIDENTITY
170 %right INCREMENT DECREMENT
182 /* Expressions, including the comma operator. */
190 | AssignExpression
',' CommaExpression
191 { write_exp_elt_opcode
(pstate
, BINOP_COMMA
); }
195 ConditionalExpression
196 | ConditionalExpression
'=' AssignExpression
197 { write_exp_elt_opcode
(pstate
, BINOP_ASSIGN
); }
198 | ConditionalExpression ASSIGN_MODIFY AssignExpression
199 { write_exp_elt_opcode
(pstate
, BINOP_ASSIGN_MODIFY
);
200 write_exp_elt_opcode
(pstate
, $2);
201 write_exp_elt_opcode
(pstate
, BINOP_ASSIGN_MODIFY
); }
204 ConditionalExpression:
206 | OrOrExpression
'?' Expression
':' ConditionalExpression
207 { write_exp_elt_opcode
(pstate
, TERNOP_COND
); }
212 | OrOrExpression OROR AndAndExpression
213 { write_exp_elt_opcode
(pstate
, BINOP_LOGICAL_OR
); }
218 | AndAndExpression ANDAND OrExpression
219 { write_exp_elt_opcode
(pstate
, BINOP_LOGICAL_AND
); }
224 | OrExpression
'|' XorExpression
225 { write_exp_elt_opcode
(pstate
, BINOP_BITWISE_IOR
); }
230 | XorExpression
'^' AndExpression
231 { write_exp_elt_opcode
(pstate
, BINOP_BITWISE_XOR
); }
236 | AndExpression
'&' CmpExpression
237 { write_exp_elt_opcode
(pstate
, BINOP_BITWISE_AND
); }
248 ShiftExpression EQUAL ShiftExpression
249 { write_exp_elt_opcode
(pstate
, BINOP_EQUAL
); }
250 | ShiftExpression NOTEQUAL ShiftExpression
251 { write_exp_elt_opcode
(pstate
, BINOP_NOTEQUAL
); }
255 ShiftExpression IDENTITY ShiftExpression
256 { write_exp_elt_opcode
(pstate
, BINOP_EQUAL
); }
257 | ShiftExpression NOTIDENTITY ShiftExpression
258 { write_exp_elt_opcode
(pstate
, BINOP_NOTEQUAL
); }
262 ShiftExpression
'<' ShiftExpression
263 { write_exp_elt_opcode
(pstate
, BINOP_LESS
); }
264 | ShiftExpression LEQ ShiftExpression
265 { write_exp_elt_opcode
(pstate
, BINOP_LEQ
); }
266 | ShiftExpression
'>' ShiftExpression
267 { write_exp_elt_opcode
(pstate
, BINOP_GTR
); }
268 | ShiftExpression GEQ ShiftExpression
269 { write_exp_elt_opcode
(pstate
, BINOP_GEQ
); }
274 | ShiftExpression LSH AddExpression
275 { write_exp_elt_opcode
(pstate
, BINOP_LSH
); }
276 | ShiftExpression RSH AddExpression
277 { write_exp_elt_opcode
(pstate
, BINOP_RSH
); }
282 | AddExpression
'+' MulExpression
283 { write_exp_elt_opcode
(pstate
, BINOP_ADD
); }
284 | AddExpression
'-' MulExpression
285 { write_exp_elt_opcode
(pstate
, BINOP_SUB
); }
286 | AddExpression
'~' MulExpression
287 { write_exp_elt_opcode
(pstate
, BINOP_CONCAT
); }
292 | MulExpression
'*' UnaryExpression
293 { write_exp_elt_opcode
(pstate
, BINOP_MUL
); }
294 | MulExpression
'/' UnaryExpression
295 { write_exp_elt_opcode
(pstate
, BINOP_DIV
); }
296 | MulExpression
'%' UnaryExpression
297 { write_exp_elt_opcode
(pstate
, BINOP_REM
); }
301 { write_exp_elt_opcode
(pstate
, UNOP_ADDR
); }
302 | INCREMENT UnaryExpression
303 { write_exp_elt_opcode
(pstate
, UNOP_PREINCREMENT
); }
304 | DECREMENT UnaryExpression
305 { write_exp_elt_opcode
(pstate
, UNOP_PREDECREMENT
); }
306 |
'*' UnaryExpression
307 { write_exp_elt_opcode
(pstate
, UNOP_IND
); }
308 |
'-' UnaryExpression
309 { write_exp_elt_opcode
(pstate
, UNOP_NEG
); }
310 |
'+' UnaryExpression
311 { write_exp_elt_opcode
(pstate
, UNOP_PLUS
); }
312 |
'!' UnaryExpression
313 { write_exp_elt_opcode
(pstate
, UNOP_LOGICAL_NOT
); }
314 |
'~' UnaryExpression
315 { write_exp_elt_opcode
(pstate
, UNOP_COMPLEMENT
); }
316 | TypeExp
'.' SIZEOF_KEYWORD
317 { write_exp_elt_opcode
(pstate
, UNOP_SIZEOF
); }
323 CAST_KEYWORD
'(' TypeExp
')' UnaryExpression
324 { write_exp_elt_opcode
(pstate
, UNOP_CAST
);
325 write_exp_elt_type
(pstate
, $3);
326 write_exp_elt_opcode
(pstate
, UNOP_CAST
); }
327 /* C style cast is illegal D, but is still recognised in
328 the grammar, so we keep this around for convenience. */
329 |
'(' TypeExp
')' UnaryExpression
330 { write_exp_elt_opcode
(pstate
, UNOP_CAST
);
331 write_exp_elt_type
(pstate
, $2);
332 write_exp_elt_opcode
(pstate
, UNOP_CAST
); }
337 | PostfixExpression HATHAT UnaryExpression
338 { write_exp_elt_opcode
(pstate
, BINOP_EXP
); }
343 | PostfixExpression
'.' COMPLETE
345 mark_struct_expression
(pstate
);
346 write_exp_elt_opcode
(pstate
, STRUCTOP_STRUCT
);
349 write_exp_string
(pstate
, s
);
350 write_exp_elt_opcode
(pstate
, STRUCTOP_STRUCT
); }
351 | PostfixExpression
'.' IDENTIFIER
352 { write_exp_elt_opcode
(pstate
, STRUCTOP_STRUCT
);
353 write_exp_string
(pstate
, $3);
354 write_exp_elt_opcode
(pstate
, STRUCTOP_STRUCT
); }
355 | PostfixExpression
'.' IDENTIFIER COMPLETE
356 { mark_struct_expression
(pstate
);
357 write_exp_elt_opcode
(pstate
, STRUCTOP_STRUCT
);
358 write_exp_string
(pstate
, $3);
359 write_exp_elt_opcode
(pstate
, STRUCTOP_STRUCT
); }
360 | PostfixExpression
'.' SIZEOF_KEYWORD
361 { write_exp_elt_opcode
(pstate
, UNOP_SIZEOF
); }
362 | PostfixExpression INCREMENT
363 { write_exp_elt_opcode
(pstate
, UNOP_POSTINCREMENT
); }
364 | PostfixExpression DECREMENT
365 { write_exp_elt_opcode
(pstate
, UNOP_POSTDECREMENT
); }
374 | ArgumentList
',' AssignExpression
385 PostfixExpression
'('
386 { start_arglist
(); }
388 { write_exp_elt_opcode
(pstate
, OP_FUNCALL
);
389 write_exp_elt_longcst
(pstate
, (LONGEST
) end_arglist
());
390 write_exp_elt_opcode
(pstate
, OP_FUNCALL
); }
394 PostfixExpression
'[' ArgumentList
']'
395 { if
(arglist_len
> 0)
397 write_exp_elt_opcode
(pstate
, MULTI_SUBSCRIPT
);
398 write_exp_elt_longcst
(pstate
, (LONGEST
) arglist_len
);
399 write_exp_elt_opcode
(pstate
, MULTI_SUBSCRIPT
);
402 write_exp_elt_opcode
(pstate
, BINOP_SUBSCRIPT
);
407 PostfixExpression
'[' ']'
408 { /* Do nothing. */ }
409 | PostfixExpression
'[' AssignExpression DOTDOT AssignExpression
']'
410 { write_exp_elt_opcode
(pstate
, TERNOP_SLICE
); }
415 { /* Do nothing. */ }
417 { struct bound_minimal_symbol msymbol
;
418 char *copy
= copy_name
($1);
419 struct field_of_this_result is_a_field_of_this
;
420 struct block_symbol sym
;
422 /* Handle VAR, which could be local or global. */
423 sym
= lookup_symbol
(copy
, expression_context_block
, VAR_DOMAIN
,
424 &is_a_field_of_this
);
425 if
(sym.symbol
&& SYMBOL_CLASS
(sym.symbol
) != LOC_TYPEDEF
)
427 if
(symbol_read_needs_frame
(sym.symbol
))
429 if
(innermost_block
== 0
430 || contained_in
(sym.block
, innermost_block
))
431 innermost_block
= sym.block
;
434 write_exp_elt_opcode
(pstate
, OP_VAR_VALUE
);
435 write_exp_elt_block
(pstate
, sym.block
);
436 write_exp_elt_sym
(pstate
, sym.symbol
);
437 write_exp_elt_opcode
(pstate
, OP_VAR_VALUE
);
439 else if
(is_a_field_of_this.type
!= NULL
)
441 /* It hangs off of `this'. Must not inadvertently convert from a
442 method call to data ref. */
443 if
(innermost_block
== 0
444 || contained_in
(sym.block
, innermost_block
))
445 innermost_block
= sym.block
;
446 write_exp_elt_opcode
(pstate
, OP_THIS
);
447 write_exp_elt_opcode
(pstate
, OP_THIS
);
448 write_exp_elt_opcode
(pstate
, STRUCTOP_PTR
);
449 write_exp_string
(pstate
, $1);
450 write_exp_elt_opcode
(pstate
, STRUCTOP_PTR
);
454 /* Lookup foreign name in global static symbols. */
455 msymbol
= lookup_bound_minimal_symbol
(copy
);
456 if
(msymbol.minsym
!= NULL
)
457 write_exp_msymbol
(pstate
, msymbol
);
458 else if
(!have_full_symbols
() && !have_partial_symbols
())
459 error (_
("No symbol table is loaded. Use the \"file\" command"));
461 error (_
("No symbol \"%s\" in current context."), copy
);
464 | TypeExp
'.' IdentifierExp
465 { struct type
*type
= check_typedef
($1);
467 /* Check if the qualified name is in the global
468 context. However if the symbol has not already
469 been resolved, it's not likely to be found. */
470 if
(TYPE_CODE
(type
) == TYPE_CODE_MODULE
)
472 struct bound_minimal_symbol msymbol
;
473 struct block_symbol sym
;
474 const char *type_name
= TYPE_SAFE_NAME
(type
);
475 int type_name_len
= strlen
(type_name
);
478 name
= xstrprintf
("%.*s.%.*s",
479 type_name_len
, type_name
,
481 make_cleanup
(xfree
, name
);
484 lookup_symbol
(name
, (const struct block
*) NULL
,
488 write_exp_elt_opcode
(pstate
, OP_VAR_VALUE
);
489 write_exp_elt_block
(pstate
, sym.block
);
490 write_exp_elt_sym
(pstate
, sym.symbol
);
491 write_exp_elt_opcode
(pstate
, OP_VAR_VALUE
);
495 msymbol
= lookup_bound_minimal_symbol
(name
);
496 if
(msymbol.minsym
!= NULL
)
497 write_exp_msymbol
(pstate
, msymbol
);
498 else if
(!have_full_symbols
() && !have_partial_symbols
())
499 error (_
("No symbol table is loaded. Use the \"file\" command."));
501 error (_
("No symbol \"%s\" in current context."), name
);
504 /* Check if the qualified name resolves as a member
505 of an aggregate or an enum type. */
506 if
(!type_aggregate_p
(type
))
507 error (_
("`%s' is not defined as an aggregate type."),
508 TYPE_SAFE_NAME
(type
));
510 write_exp_elt_opcode
(pstate
, OP_SCOPE
);
511 write_exp_elt_type
(pstate
, type
);
512 write_exp_string
(pstate
, $3);
513 write_exp_elt_opcode
(pstate
, OP_SCOPE
);
516 { write_dollar_variable
(pstate
, $1); }
519 parse_number
(pstate
, $1.ptr
, $1.length
, 0, &val
);
520 write_exp_elt_opcode
(pstate
, OP_LONG
);
521 write_exp_elt_type
(pstate
, val.typed_val_int.type
);
522 write_exp_elt_longcst
(pstate
,
523 (LONGEST
) val.typed_val_int.val
);
524 write_exp_elt_opcode
(pstate
, OP_LONG
); }
526 { struct type
*type
= parse_d_type
(pstate
)->builtin_void
;
527 type
= lookup_pointer_type
(type
);
528 write_exp_elt_opcode
(pstate
, OP_LONG
);
529 write_exp_elt_type
(pstate
, type
);
530 write_exp_elt_longcst
(pstate
, (LONGEST
) 0);
531 write_exp_elt_opcode
(pstate
, OP_LONG
); }
533 { write_exp_elt_opcode
(pstate
, OP_BOOL
);
534 write_exp_elt_longcst
(pstate
, (LONGEST
) 1);
535 write_exp_elt_opcode
(pstate
, OP_BOOL
); }
537 { write_exp_elt_opcode
(pstate
, OP_BOOL
);
538 write_exp_elt_longcst
(pstate
, (LONGEST
) 0);
539 write_exp_elt_opcode
(pstate
, OP_BOOL
); }
541 { write_exp_elt_opcode
(pstate
, OP_LONG
);
542 write_exp_elt_type
(pstate
, $1.type
);
543 write_exp_elt_longcst
(pstate
, (LONGEST
)($1.val
));
544 write_exp_elt_opcode
(pstate
, OP_LONG
); }
546 { write_exp_elt_opcode
(pstate
, OP_DOUBLE
);
547 write_exp_elt_type
(pstate
, $1.type
);
548 write_exp_elt_dblcst
(pstate
, $1.dval
);
549 write_exp_elt_opcode
(pstate
, OP_DOUBLE
); }
551 { struct stoken_vector vec
;
554 write_exp_string_vector
(pstate
, $1.type
, &vec
); }
557 write_exp_string_vector
(pstate
, 0, &$1);
558 for
(i
= 0; i
< $1.len
; ++i
)
559 free
($1.tokens
[i
].ptr
);
562 { write_exp_elt_opcode
(pstate
, OP_ARRAY
);
563 write_exp_elt_longcst
(pstate
, (LONGEST
) 0);
564 write_exp_elt_longcst
(pstate
, (LONGEST
) $1 - 1);
565 write_exp_elt_opcode
(pstate
, OP_ARRAY
); }
566 | TYPEOF_KEYWORD
'(' Expression
')'
567 { write_exp_elt_opcode
(pstate
, OP_TYPEOF
); }
571 '[' ArgumentList_opt
']'
572 { $$
= arglist_len
; }
581 { /* We copy the string here, and not in the
582 lexer, to guarantee that we do not leak a
583 string. Note that we follow the
584 NUL-termination convention of the
586 struct typed_stoken
*vec
= XNEW
(struct typed_stoken
);
591 vec
->length
= $1.length
;
592 vec
->ptr
= (char *) malloc
($1.length
+ 1);
593 memcpy
(vec
->ptr
, $1.ptr
, $1.length
+ 1);
595 | StringExp STRING_LITERAL
596 { /* Note that we NUL-terminate here, but just
601 = XRESIZEVEC
(struct typed_stoken
, $$.tokens
, $$.len
);
603 p
= (char *) malloc
($2.length
+ 1);
604 memcpy
(p
, $2.ptr
, $2.length
+ 1);
606 $$.tokens
[$$.len
- 1].type
= $2.type
;
607 $$.tokens
[$$.len
- 1].length
= $2.length
;
608 $$.tokens
[$$.len
- 1].ptr
= p
;
614 { /* Do nothing. */ }
616 { write_exp_elt_opcode
(pstate
, OP_TYPE
);
617 write_exp_elt_type
(pstate
, $1);
618 write_exp_elt_opcode
(pstate
, OP_TYPE
); }
619 | BasicType BasicType2
620 { $$
= follow_types
($1);
621 write_exp_elt_opcode
(pstate
, OP_TYPE
);
622 write_exp_elt_type
(pstate
, $$
);
623 write_exp_elt_opcode
(pstate
, OP_TYPE
);
629 { push_type
(tp_pointer
); }
631 { push_type
(tp_pointer
); }
632 |
'[' INTEGER_LITERAL
']'
633 { push_type_int
($2.val
);
634 push_type
(tp_array
); }
635 |
'[' INTEGER_LITERAL
']' BasicType2
636 { push_type_int
($2.val
);
637 push_type
(tp_array
); }
647 /* Return true if the type is aggregate-like. */
650 type_aggregate_p
(struct type
*type
)
652 return
(TYPE_CODE
(type
) == TYPE_CODE_STRUCT
653 || TYPE_CODE
(type
) == TYPE_CODE_UNION
654 ||
(TYPE_CODE
(type
) == TYPE_CODE_ENUM
655 && TYPE_DECLARED_CLASS
(type
)));
658 /* Take care of parsing a number (anything that starts with a digit).
659 Set yylval and return the token type; update lexptr.
660 LEN is the number of characters in it. */
662 /*** Needs some error checking for the float case ***/
665 parse_number
(struct parser_state
*ps
, const char *p
,
666 int len
, int parsed_float
, YYSTYPE *putithere
)
674 int base
= input_radix
;
678 /* We have found a "L" or "U" suffix. */
679 int found_suffix
= 0;
682 struct type
*signed_type
;
683 struct type
*unsigned_type
;
691 /* Strip out all embedded '_' before passing to parse_float. */
692 s
= (char *) alloca
(len
+ 1);
703 if
(! parse_float
(s
, len
, &putithere
->typed_val_float.dval
, &suffix
))
706 suffix_len
= s
+ len
- suffix
;
710 putithere
->typed_val_float.type
711 = parse_d_type
(ps
)->builtin_double
;
713 else if
(suffix_len
== 1)
715 /* Check suffix for `f', `l', or `i' (float, real, or idouble). */
716 if
(tolower
(*suffix
) == 'f')
718 putithere
->typed_val_float.type
719 = parse_d_type
(ps
)->builtin_float
;
721 else if
(tolower
(*suffix
) == 'l')
723 putithere
->typed_val_float.type
724 = parse_d_type
(ps
)->builtin_real
;
726 else if
(tolower
(*suffix
) == 'i')
728 putithere
->typed_val_float.type
729 = parse_d_type
(ps
)->builtin_idouble
;
734 else if
(suffix_len
== 2)
736 /* Check suffix for `fi' or `li' (ifloat or ireal). */
737 if
(tolower
(suffix
[0]) == 'f' && tolower
(suffix
[1] == 'i'))
739 putithere
->typed_val_float.type
740 = parse_d_type
(ps
)->builtin_ifloat
;
742 else if
(tolower
(suffix
[0]) == 'l' && tolower
(suffix
[1] == 'i'))
744 putithere
->typed_val_float.type
745 = parse_d_type
(ps
)->builtin_ireal
;
753 return FLOAT_LITERAL
;
756 /* Handle base-switching prefixes 0x, 0b, 0 */
789 continue
; /* Ignore embedded '_'. */
790 if
(c
>= 'A' && c
<= 'Z')
792 if
(c
!= 'l' && c
!= 'u')
794 if
(c
>= '0' && c
<= '9')
802 if
(base
> 10 && c
>= 'a' && c
<= 'f')
806 n
+= i
= c
- 'a' + 10;
808 else if
(c
== 'l' && long_p
== 0)
813 else if
(c
== 'u' && unsigned_p
== 0)
819 return ERROR
; /* Char not a digit */
822 return ERROR
; /* Invalid digit in this base. */
823 /* Portably test for integer overflow. */
824 if
(c
!= 'l' && c
!= 'u')
826 ULONGEST n2
= prevn
* base
;
827 if
((n2
/ base
!= prevn
) ||
(n2
+ i
< prevn
))
828 error (_
("Numeric constant too large."));
833 /* An integer constant is an int or a long. An L suffix forces it to
834 be long, and a U suffix forces it to be unsigned. To figure out
835 whether it fits, we shift it right and see whether anything remains.
836 Note that we can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or
837 more in one operation, because many compilers will warn about such a
838 shift (which always produces a zero result). To deal with the case
839 where it is we just always shift the value more than once, with fewer
841 un
= (ULONGEST
) n
>> 2;
842 if
(long_p
== 0 && (un
>> 30) == 0)
844 high_bit
= ((ULONGEST
) 1) << 31;
845 signed_type
= parse_d_type
(ps
)->builtin_int
;
846 /* For decimal notation, keep the sign of the worked out type. */
847 if
(base
== 10 && !unsigned_p
)
848 unsigned_type
= parse_d_type
(ps
)->builtin_long
;
850 unsigned_type
= parse_d_type
(ps
)->builtin_uint
;
855 if
(sizeof
(ULONGEST
) * HOST_CHAR_BIT
< 64)
856 /* A long long does not fit in a LONGEST. */
857 shift
= (sizeof
(ULONGEST
) * HOST_CHAR_BIT
- 1);
860 high_bit
= (ULONGEST
) 1 << shift
;
861 signed_type
= parse_d_type
(ps
)->builtin_long
;
862 unsigned_type
= parse_d_type
(ps
)->builtin_ulong
;
865 putithere
->typed_val_int.val
= n
;
867 /* If the high bit of the worked out type is set then this number
868 has to be unsigned_type. */
869 if
(unsigned_p ||
(n
& high_bit
))
870 putithere
->typed_val_int.type
= unsigned_type
;
872 putithere
->typed_val_int.type
= signed_type
;
874 return INTEGER_LITERAL
;
877 /* Temporary obstack used for holding strings. */
878 static struct obstack tempbuf
;
879 static int tempbuf_init
;
881 /* Parse a string or character literal from TOKPTR. The string or
882 character may be wide or unicode. *OUTPTR is set to just after the
883 end of the literal in the input string. The resulting token is
884 stored in VALUE. This returns a token value, either STRING or
885 CHAR, depending on what was parsed. *HOST_CHARS is set to the
886 number of host characters in the literal. */
889 parse_string_or_char
(const char *tokptr
, const char **outptr
,
890 struct typed_stoken
*value
, int *host_chars
)
894 /* Build the gdb internal form of the input string in tempbuf. Note
895 that the buffer is null byte terminated *only* for the
896 convenience of debugging gdb itself and printing the buffer
897 contents when the buffer contains no embedded nulls. Gdb does
898 not depend upon the buffer being null byte terminated, it uses
899 the length string instead. This allows gdb to handle C strings
900 (as well as strings in other languages) with embedded null
906 obstack_free
(&tempbuf
, NULL
);
907 obstack_init
(&tempbuf
);
909 /* Skip the quote. */
921 *host_chars
+= c_parse_escape
(&tokptr
, &tempbuf
);
927 obstack_1grow
(&tempbuf
, c
);
929 /* FIXME: this does the wrong thing with multi-byte host
930 characters. We could use mbrlen here, but that would
931 make "set host-charset" a bit less useful. */
936 if
(*tokptr
!= quote
)
938 if
(quote
== '"' || quote
== '`')
939 error (_
("Unterminated string in expression."));
941 error (_
("Unmatched single quote."));
945 /* FIXME: should instead use own language string_type enum
946 and handle D-specific string suffixes here. */
948 value
->type
= C_CHAR
;
950 value
->type
= C_STRING
;
952 value
->ptr
= (char *) obstack_base
(&tempbuf
);
953 value
->length
= obstack_object_size
(&tempbuf
);
957 return quote
== '\'' ? CHARACTER_LITERAL
: STRING_LITERAL
;
964 enum exp_opcode opcode
;
967 static const struct token tokentab3
[] =
969 {"^^=", ASSIGN_MODIFY
, BINOP_EXP
},
970 {"<<=", ASSIGN_MODIFY
, BINOP_LSH
},
971 {">>=", ASSIGN_MODIFY
, BINOP_RSH
},
974 static const struct token tokentab2
[] =
976 {"+=", ASSIGN_MODIFY
, BINOP_ADD
},
977 {"-=", ASSIGN_MODIFY
, BINOP_SUB
},
978 {"*=", ASSIGN_MODIFY
, BINOP_MUL
},
979 {"/=", ASSIGN_MODIFY
, BINOP_DIV
},
980 {"%=", ASSIGN_MODIFY
, BINOP_REM
},
981 {"|=", ASSIGN_MODIFY
, BINOP_BITWISE_IOR
},
982 {"&=", ASSIGN_MODIFY
, BINOP_BITWISE_AND
},
983 {"^=", ASSIGN_MODIFY
, BINOP_BITWISE_XOR
},
984 {"++", INCREMENT
, BINOP_END
},
985 {"--", DECREMENT
, BINOP_END
},
986 {"&&", ANDAND
, BINOP_END
},
987 {"||", OROR
, BINOP_END
},
988 {"^^", HATHAT
, BINOP_END
},
989 {"<<", LSH
, BINOP_END
},
990 {">>", RSH
, BINOP_END
},
991 {"==", EQUAL
, BINOP_END
},
992 {"!=", NOTEQUAL
, BINOP_END
},
993 {"<=", LEQ
, BINOP_END
},
994 {">=", GEQ
, BINOP_END
},
995 {"..", DOTDOT
, BINOP_END
},
998 /* Identifier-like tokens. */
999 static const struct token ident_tokens
[] =
1001 {"is", IDENTITY
, BINOP_END
},
1002 {"!is", NOTIDENTITY
, BINOP_END
},
1004 {"cast", CAST_KEYWORD
, OP_NULL
},
1005 {"const", CONST_KEYWORD
, OP_NULL
},
1006 {"immutable", IMMUTABLE_KEYWORD
, OP_NULL
},
1007 {"shared", SHARED_KEYWORD
, OP_NULL
},
1008 {"super", SUPER_KEYWORD
, OP_NULL
},
1010 {"null", NULL_KEYWORD
, OP_NULL
},
1011 {"true", TRUE_KEYWORD
, OP_NULL
},
1012 {"false", FALSE_KEYWORD
, OP_NULL
},
1014 {"init", INIT_KEYWORD
, OP_NULL
},
1015 {"sizeof", SIZEOF_KEYWORD
, OP_NULL
},
1016 {"typeof", TYPEOF_KEYWORD
, OP_NULL
},
1017 {"typeid", TYPEID_KEYWORD
, OP_NULL
},
1019 {"delegate", DELEGATE_KEYWORD
, OP_NULL
},
1020 {"function", FUNCTION_KEYWORD
, OP_NULL
},
1021 {"struct", STRUCT_KEYWORD
, OP_NULL
},
1022 {"union", UNION_KEYWORD
, OP_NULL
},
1023 {"class", CLASS_KEYWORD
, OP_NULL
},
1024 {"interface", INTERFACE_KEYWORD
, OP_NULL
},
1025 {"enum", ENUM_KEYWORD
, OP_NULL
},
1026 {"template", TEMPLATE_KEYWORD
, OP_NULL
},
1029 /* This is set if a NAME token appeared at the very end of the input
1030 string, with no whitespace separating the name from the EOF. This
1031 is used only when parsing to do field name completion. */
1032 static int saw_name_at_eof
;
1034 /* This is set if the previously-returned token was a structure operator.
1035 This is used only when parsing to do field name completion. */
1036 static int last_was_structop
;
1038 /* Read one token, getting characters through lexptr. */
1041 lex_one_token
(struct parser_state
*par_state
)
1046 const char *tokstart
;
1047 int saw_structop
= last_was_structop
;
1050 last_was_structop
= 0;
1054 prev_lexptr
= lexptr
;
1057 /* See if it is a special token of length 3. */
1058 for
(i
= 0; i
< sizeof tokentab3
/ sizeof tokentab3
[0]; i
++)
1059 if
(strncmp
(tokstart
, tokentab3
[i
].oper
, 3) == 0)
1062 yylval.opcode
= tokentab3
[i
].opcode
;
1063 return tokentab3
[i
].token
;
1066 /* See if it is a special token of length 2. */
1067 for
(i
= 0; i
< sizeof tokentab2
/ sizeof tokentab2
[0]; i
++)
1068 if
(strncmp
(tokstart
, tokentab2
[i
].oper
, 2) == 0)
1071 yylval.opcode
= tokentab2
[i
].opcode
;
1072 return tokentab2
[i
].token
;
1075 switch
(c
= *tokstart
)
1078 /* If we're parsing for field name completion, and the previous
1079 token allows such completion, return a COMPLETE token.
1080 Otherwise, we were already scanning the original text, and
1081 we're really done. */
1082 if
(saw_name_at_eof
)
1084 saw_name_at_eof
= 0;
1087 else if
(saw_structop
)
1106 if
(paren_depth
== 0)
1113 if
(comma_terminates
&& paren_depth
== 0)
1119 /* Might be a floating point number. */
1120 if
(lexptr
[1] < '0' || lexptr
[1] > '9')
1122 if
(parse_completion
)
1123 last_was_structop
= 1;
1124 goto symbol
; /* Nope, must be a symbol. */
1126 /* FALL THRU into number case. */
1139 /* It's a number. */
1140 int got_dot
= 0, got_e
= 0, toktype
;
1141 const char *p
= tokstart
;
1142 int hex
= input_radix
> 10;
1144 if
(c
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
1152 /* Hex exponents start with 'p', because 'e' is a valid hex
1153 digit and thus does not indicate a floating point number
1154 when the radix is hex. */
1155 if
((!hex
&& !got_e
&& tolower
(p
[0]) == 'e')
1156 ||
(hex
&& !got_e
&& tolower
(p
[0] == 'p')))
1157 got_dot
= got_e
= 1;
1158 /* A '.' always indicates a decimal floating point number
1159 regardless of the radix. If we have a '..' then its the
1160 end of the number and the beginning of a slice. */
1161 else if
(!got_dot
&& (p
[0] == '.' && p
[1] != '.'))
1163 /* This is the sign of the exponent, not the end of the number. */
1164 else if
(got_e
&& (tolower
(p
[-1]) == 'e' || tolower
(p
[-1]) == 'p')
1165 && (*p
== '-' ||
*p
== '+'))
1167 /* We will take any letters or digits, ignoring any embedded '_'.
1168 parse_number will complain if past the radix, or if L or U are
1170 else if
((*p
< '0' ||
*p
> '9') && (*p
!= '_')
1171 && ((*p
< 'a' ||
*p
> 'z') && (*p
< 'A' ||
*p
> 'Z')))
1175 toktype
= parse_number
(par_state
, tokstart
, p
- tokstart
,
1176 got_dot|got_e
, &yylval);
1177 if
(toktype
== ERROR
)
1179 char *err_copy
= (char *) alloca
(p
- tokstart
+ 1);
1181 memcpy
(err_copy
, tokstart
, p
- tokstart
);
1182 err_copy
[p
- tokstart
] = 0;
1183 error (_
("Invalid number \"%s\"."), err_copy
);
1191 const char *p
= &tokstart
[1];
1192 size_t len
= strlen
("entry");
1194 while
(isspace
(*p
))
1196 if
(strncmp
(p
, "entry", len
) == 0 && !isalnum
(p
[len
])
1230 int result
= parse_string_or_char
(tokstart
, &lexptr
, &yylval.tsval
,
1232 if
(result
== CHARACTER_LITERAL
)
1235 error (_
("Empty character constant."));
1236 else if
(host_len
> 2 && c
== '\'')
1239 namelen
= lexptr
- tokstart
- 1;
1242 else if
(host_len
> 1)
1243 error (_
("Invalid character constant."));
1249 if
(!(c
== '_' || c
== '$'
1250 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z')))
1251 /* We must have come across a bad character (e.g. ';'). */
1252 error (_
("Invalid character '%c' in expression"), c
);
1254 /* It's a name. See how long it is. */
1256 for
(c
= tokstart
[namelen
];
1257 (c
== '_' || c
== '$' ||
(c
>= '0' && c
<= '9')
1258 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z'));)
1259 c
= tokstart
[++namelen
];
1261 /* The token "if" terminates the expression and is NOT
1262 removed from the input stream. */
1263 if
(namelen
== 2 && tokstart
[0] == 'i' && tokstart
[1] == 'f')
1266 /* For the same reason (breakpoint conditions), "thread N"
1267 terminates the expression. "thread" could be an identifier, but
1268 an identifier is never followed by a number without intervening
1269 punctuation. "task" is similar. Handle abbreviations of these,
1270 similarly to breakpoint.c:find_condition_and_thread. */
1272 && (strncmp
(tokstart
, "thread", namelen
) == 0
1273 || strncmp
(tokstart
, "task", namelen
) == 0)
1274 && (tokstart
[namelen
] == ' ' || tokstart
[namelen
] == '\t'))
1276 const char *p
= tokstart
+ namelen
+ 1;
1278 while
(*p
== ' ' ||
*p
== '\t')
1280 if
(*p
>= '0' && *p
<= '9')
1288 yylval.sval.ptr
= tokstart
;
1289 yylval.sval.length
= namelen
;
1291 /* Catch specific keywords. */
1292 copy
= copy_name
(yylval.sval
);
1293 for
(i
= 0; i
< sizeof ident_tokens
/ sizeof ident_tokens
[0]; i
++)
1294 if
(strcmp
(copy
, ident_tokens
[i
].oper
) == 0)
1296 /* It is ok to always set this, even though we don't always
1297 strictly need to. */
1298 yylval.opcode
= ident_tokens
[i
].opcode
;
1299 return ident_tokens
[i
].token
;
1302 if
(*tokstart
== '$')
1303 return DOLLAR_VARIABLE
;
1306 = language_lookup_primitive_type
(parse_language
(par_state
),
1307 parse_gdbarch
(par_state
), copy
);
1308 if
(yylval.tsym.type
!= NULL
)
1311 /* Input names that aren't symbols but ARE valid hex numbers,
1312 when the input radix permits them, can be names or numbers
1313 depending on the parse. Note we support radixes > 16 here. */
1314 if
((tokstart
[0] >= 'a' && tokstart
[0] < 'a' + input_radix
- 10)
1315 ||
(tokstart
[0] >= 'A' && tokstart
[0] < 'A' + input_radix
- 10))
1317 YYSTYPE newlval
; /* Its value is ignored. */
1318 int hextype
= parse_number
(par_state
, tokstart
, namelen
, 0, &newlval
);
1319 if
(hextype
== INTEGER_LITERAL
)
1323 if
(parse_completion
&& *lexptr
== '\0')
1324 saw_name_at_eof
= 1;
1329 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
1336 DEF_VEC_O
(token_and_value
);
1338 /* A FIFO of tokens that have been read but not yet returned to the
1340 static VEC
(token_and_value
) *token_fifo
;
1342 /* Non-zero if the lexer should return tokens from the FIFO. */
1345 /* Temporary storage for yylex; this holds symbol names as they are
1347 static struct obstack name_obstack
;
1349 /* Classify an IDENTIFIER token. The contents of the token are in `yylval'.
1350 Updates yylval and returns the new token type. BLOCK is the block
1351 in which lookups start; this can be NULL to mean the global scope. */
1354 classify_name
(struct parser_state
*par_state
, const struct block
*block
)
1356 struct block_symbol sym
;
1358 struct field_of_this_result is_a_field_of_this
;
1360 copy
= copy_name
(yylval.sval
);
1362 sym
= lookup_symbol
(copy
, block
, VAR_DOMAIN
, &is_a_field_of_this
);
1363 if
(sym.symbol
&& SYMBOL_CLASS
(sym.symbol
) == LOC_TYPEDEF
)
1365 yylval.tsym.type
= SYMBOL_TYPE
(sym.symbol
);
1368 else if
(sym.symbol
== NULL
)
1370 /* Look-up first for a module name, then a type. */
1371 sym
= lookup_symbol
(copy
, block
, MODULE_DOMAIN
, NULL
);
1372 if
(sym.symbol
== NULL
)
1373 sym
= lookup_symbol
(copy
, block
, STRUCT_DOMAIN
, NULL
);
1375 if
(sym.symbol
!= NULL
)
1377 yylval.tsym.type
= SYMBOL_TYPE
(sym.symbol
);
1381 return UNKNOWN_NAME
;
1387 /* Like classify_name, but used by the inner loop of the lexer, when a
1388 name might have already been seen. CONTEXT is the context type, or
1389 NULL if this is the first component of a name. */
1392 classify_inner_name
(struct parser_state
*par_state
,
1393 const struct block
*block
, struct type
*context
)
1398 if
(context
== NULL
)
1399 return classify_name
(par_state
, block
);
1401 type
= check_typedef
(context
);
1402 if
(!type_aggregate_p
(type
))
1405 copy
= copy_name
(yylval.ssym.stoken
);
1406 yylval.ssym.sym
= d_lookup_nested_symbol
(type
, copy
, block
);
1408 if
(yylval.ssym.sym.symbol
== NULL
)
1411 if
(SYMBOL_CLASS
(yylval.ssym.sym.symbol
) == LOC_TYPEDEF
)
1413 yylval.tsym.type
= SYMBOL_TYPE
(yylval.ssym.sym.symbol
);
1420 /* The outer level of a two-level lexer. This calls the inner lexer
1421 to return tokens. It then either returns these tokens, or
1422 aggregates them into a larger token. This lets us work around a
1423 problem in our parsing approach, where the parser could not
1424 distinguish between qualified names and qualified types at the
1430 token_and_value current
;
1432 struct type
*context_type
= NULL
;
1433 int last_to_examine
, next_to_examine
, checkpoint
;
1434 const struct block
*search_block
;
1436 if
(popping
&& !VEC_empty
(token_and_value
, token_fifo
))
1440 /* Read the first token and decide what to do. */
1441 current.token
= lex_one_token
(pstate
);
1442 if
(current.token
!= IDENTIFIER
&& current.token
!= '.')
1443 return current.token
;
1445 /* Read any sequence of alternating "." and identifier tokens into
1447 current.value
= yylval;
1448 VEC_safe_push
(token_and_value
, token_fifo
, ¤t
);
1449 last_was_dot
= current.token
== '.';
1453 current.token
= lex_one_token
(pstate
);
1454 current.value
= yylval;
1455 VEC_safe_push
(token_and_value
, token_fifo
, ¤t
);
1457 if
((last_was_dot
&& current.token
!= IDENTIFIER
)
1458 ||
(!last_was_dot
&& current.token
!= '.'))
1461 last_was_dot
= !last_was_dot
;
1465 /* We always read one extra token, so compute the number of tokens
1466 to examine accordingly. */
1467 last_to_examine
= VEC_length
(token_and_value
, token_fifo
) - 2;
1468 next_to_examine
= 0;
1470 current
= *VEC_index
(token_and_value
, token_fifo
, next_to_examine
);
1473 /* If we are not dealing with a typename, now is the time to find out. */
1474 if
(current.token
== IDENTIFIER
)
1476 yylval = current.value
;
1477 current.token
= classify_name
(pstate
, expression_context_block
);
1478 current.value
= yylval;
1481 /* If the IDENTIFIER is not known, it could be a package symbol,
1482 first try building up a name until we find the qualified module. */
1483 if
(current.token
== UNKNOWN_NAME
)
1485 obstack_free
(&name_obstack
, obstack_base
(&name_obstack
));
1486 obstack_grow
(&name_obstack
, current.value.sval.ptr
,
1487 current.value.sval.length
);
1491 while
(next_to_examine
<= last_to_examine
)
1493 token_and_value
*next
;
1495 next
= VEC_index
(token_and_value
, token_fifo
, next_to_examine
);
1498 if
(next
->token
== IDENTIFIER
&& last_was_dot
)
1500 /* Update the partial name we are constructing. */
1501 obstack_grow_str
(&name_obstack
, ".");
1502 obstack_grow
(&name_obstack
, next
->value.sval.ptr
,
1503 next
->value.sval.length
);
1505 yylval.sval.ptr
= (char *) obstack_base
(&name_obstack
);
1506 yylval.sval.length
= obstack_object_size
(&name_obstack
);
1508 current.token
= classify_name
(pstate
, expression_context_block
);
1509 current.value
= yylval;
1511 /* We keep going until we find a TYPENAME. */
1512 if
(current.token
== TYPENAME
)
1514 /* Install it as the first token in the FIFO. */
1515 VEC_replace
(token_and_value
, token_fifo
, 0, ¤t
);
1516 VEC_block_remove
(token_and_value
, token_fifo
, 1,
1517 next_to_examine
- 1);
1521 else if
(next
->token
== '.' && !last_was_dot
)
1525 /* We've reached the end of the name. */
1530 /* Reset our current token back to the start, if we found nothing
1531 this means that we will just jump to do pop. */
1532 current
= *VEC_index
(token_and_value
, token_fifo
, 0);
1533 next_to_examine
= 1;
1535 if
(current.token
!= TYPENAME
&& current.token
!= '.')
1538 obstack_free
(&name_obstack
, obstack_base
(&name_obstack
));
1540 if
(current.token
== '.')
1541 search_block
= NULL
;
1544 gdb_assert
(current.token
== TYPENAME
);
1545 search_block
= expression_context_block
;
1546 obstack_grow
(&name_obstack
, current.value.sval.ptr
,
1547 current.value.sval.length
);
1548 context_type
= current.value.tsym.type
;
1552 last_was_dot
= current.token
== '.';
1554 while
(next_to_examine
<= last_to_examine
)
1556 token_and_value
*next
;
1558 next
= VEC_index
(token_and_value
, token_fifo
, next_to_examine
);
1561 if
(next
->token
== IDENTIFIER
&& last_was_dot
)
1565 yylval = next
->value
;
1566 classification
= classify_inner_name
(pstate
, search_block
,
1568 /* We keep going until we either run out of names, or until
1569 we have a qualified name which is not a type. */
1570 if
(classification
!= TYPENAME
&& classification
!= IDENTIFIER
)
1573 /* Accept up to this token. */
1574 checkpoint
= next_to_examine
;
1576 /* Update the partial name we are constructing. */
1577 if
(context_type
!= NULL
)
1579 /* We don't want to put a leading "." into the name. */
1580 obstack_grow_str
(&name_obstack
, ".");
1582 obstack_grow
(&name_obstack
, next
->value.sval.ptr
,
1583 next
->value.sval.length
);
1585 yylval.sval.ptr
= (char *) obstack_base
(&name_obstack
);
1586 yylval.sval.length
= obstack_object_size
(&name_obstack
);
1587 current.value
= yylval;
1588 current.token
= classification
;
1592 if
(classification
== IDENTIFIER
)
1595 context_type
= yylval.tsym.type
;
1597 else if
(next
->token
== '.' && !last_was_dot
)
1601 /* We've reached the end of the name. */
1606 /* If we have a replacement token, install it as the first token in
1607 the FIFO, and delete the other constituent tokens. */
1610 VEC_replace
(token_and_value
, token_fifo
, 0, ¤t
);
1612 VEC_block_remove
(token_and_value
, token_fifo
, 1, checkpoint
- 1);
1616 current
= *VEC_index
(token_and_value
, token_fifo
, 0);
1617 VEC_ordered_remove
(token_and_value
, token_fifo
, 0);
1618 yylval = current.value
;
1619 return current.token
;
1623 d_parse
(struct parser_state
*par_state
)
1626 struct cleanup
*back_to
;
1628 /* Setting up the parser state. */
1629 gdb_assert
(par_state
!= NULL
);
1632 back_to
= make_cleanup
(null_cleanup
, NULL
);
1634 make_cleanup_restore_integer
(&yydebug);
1635 make_cleanup_clear_parser_state
(&pstate
);
1636 yydebug = parser_debug
;
1638 /* Initialize some state used by the lexer. */
1639 last_was_structop
= 0;
1640 saw_name_at_eof
= 0;
1642 VEC_free
(token_and_value
, token_fifo
);
1644 obstack_init
(&name_obstack
);
1645 make_cleanup_obstack_free
(&name_obstack
);
1647 result
= yyparse ();
1648 do_cleanups
(back_to
);
1656 lexptr
= prev_lexptr
;
1658 error (_
("A %s in expression, near `%s'."), (msg ? msg
: "error"), lexptr
);