1 /* YACC parser for D expressions, for GDB.
3 Copyright (C) 2014-2019 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 */
54 #include "type-stack.h"
56 #define parse_type(ps) builtin_type (ps->gdbarch ())
57 #define parse_d_type(ps) builtin_d_type (ps->gdbarch ())
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
61 #define GDB_YY_REMAP_PREFIX d_
64 /* The state of the parser, used internally when we are parsing the
67 static struct parser_state
*pstate
= NULL
;
69 /* The current type stack. */
70 static struct type_stack
*type_stack
;
74 static int yylex (void);
76 static void yyerror (const char *);
78 static int type_aggregate_p
(struct type
*);
82 /* Although the yacc "value" of an expression is not used,
83 since the result is stored in the structure being created,
84 other node types do have values. */
98 struct typed_stoken tsval
;
101 struct symtoken ssym
;
104 enum exp_opcode opcode
;
105 struct stoken_vector svec
;
109 /* YYSTYPE gets defined by %union */
110 static int parse_number
(struct parser_state
*, const char *,
111 int, int, YYSTYPE *);
114 %token
<sval
> IDENTIFIER UNKNOWN_NAME
115 %token
<tsym
> TYPENAME
116 %token
<voidval
> COMPLETE
118 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
119 but which would parse as a valid number in the current input radix.
120 E.g. "c" when input_radix==16. Depending on the parse, it will be
121 turned into a name or into a number. */
123 %token
<sval
> NAME_OR_INT
125 %token
<typed_val_int
> INTEGER_LITERAL
126 %token
<typed_val_float
> FLOAT_LITERAL
127 %token
<tsval
> CHARACTER_LITERAL
128 %token
<tsval
> STRING_LITERAL
130 %type
<svec
> StringExp
131 %type
<tval
> BasicType TypeExp
132 %type
<sval
> IdentifierExp
133 %type
<ival
> ArrayLiteral
138 /* Keywords that have a constant value. */
139 %token TRUE_KEYWORD FALSE_KEYWORD NULL_KEYWORD
140 /* Class 'super' accessor. */
143 %token CAST_KEYWORD SIZEOF_KEYWORD
144 %token TYPEOF_KEYWORD TYPEID_KEYWORD
146 /* Comparison keywords. */
147 /* Type storage classes. */
148 %token IMMUTABLE_KEYWORD CONST_KEYWORD SHARED_KEYWORD
149 /* Non-scalar type keywords. */
150 %token STRUCT_KEYWORD UNION_KEYWORD
151 %token CLASS_KEYWORD INTERFACE_KEYWORD
152 %token ENUM_KEYWORD TEMPLATE_KEYWORD
153 %token DELEGATE_KEYWORD FUNCTION_KEYWORD
155 %token
<sval
> DOLLAR_VARIABLE
157 %token
<opcode
> ASSIGN_MODIFY
160 %right
'=' ASSIGN_MODIFY
167 %left EQUAL NOTEQUAL
'<' '>' LEQ GEQ
172 %left IDENTITY NOTIDENTITY
173 %right INCREMENT DECREMENT
185 /* Expressions, including the comma operator. */
193 | AssignExpression
',' CommaExpression
194 { write_exp_elt_opcode
(pstate
, BINOP_COMMA
); }
198 ConditionalExpression
199 | ConditionalExpression
'=' AssignExpression
200 { write_exp_elt_opcode
(pstate
, BINOP_ASSIGN
); }
201 | ConditionalExpression ASSIGN_MODIFY AssignExpression
202 { write_exp_elt_opcode
(pstate
, BINOP_ASSIGN_MODIFY
);
203 write_exp_elt_opcode
(pstate
, $2);
204 write_exp_elt_opcode
(pstate
, BINOP_ASSIGN_MODIFY
); }
207 ConditionalExpression:
209 | OrOrExpression
'?' Expression
':' ConditionalExpression
210 { write_exp_elt_opcode
(pstate
, TERNOP_COND
); }
215 | OrOrExpression OROR AndAndExpression
216 { write_exp_elt_opcode
(pstate
, BINOP_LOGICAL_OR
); }
221 | AndAndExpression ANDAND OrExpression
222 { write_exp_elt_opcode
(pstate
, BINOP_LOGICAL_AND
); }
227 | OrExpression
'|' XorExpression
228 { write_exp_elt_opcode
(pstate
, BINOP_BITWISE_IOR
); }
233 | XorExpression
'^' AndExpression
234 { write_exp_elt_opcode
(pstate
, BINOP_BITWISE_XOR
); }
239 | AndExpression
'&' CmpExpression
240 { write_exp_elt_opcode
(pstate
, BINOP_BITWISE_AND
); }
251 ShiftExpression EQUAL ShiftExpression
252 { write_exp_elt_opcode
(pstate
, BINOP_EQUAL
); }
253 | ShiftExpression NOTEQUAL ShiftExpression
254 { write_exp_elt_opcode
(pstate
, BINOP_NOTEQUAL
); }
258 ShiftExpression IDENTITY ShiftExpression
259 { write_exp_elt_opcode
(pstate
, BINOP_EQUAL
); }
260 | ShiftExpression NOTIDENTITY ShiftExpression
261 { write_exp_elt_opcode
(pstate
, BINOP_NOTEQUAL
); }
265 ShiftExpression
'<' ShiftExpression
266 { write_exp_elt_opcode
(pstate
, BINOP_LESS
); }
267 | ShiftExpression LEQ ShiftExpression
268 { write_exp_elt_opcode
(pstate
, BINOP_LEQ
); }
269 | ShiftExpression
'>' ShiftExpression
270 { write_exp_elt_opcode
(pstate
, BINOP_GTR
); }
271 | ShiftExpression GEQ ShiftExpression
272 { write_exp_elt_opcode
(pstate
, BINOP_GEQ
); }
277 | ShiftExpression LSH AddExpression
278 { write_exp_elt_opcode
(pstate
, BINOP_LSH
); }
279 | ShiftExpression RSH AddExpression
280 { write_exp_elt_opcode
(pstate
, BINOP_RSH
); }
285 | AddExpression
'+' MulExpression
286 { write_exp_elt_opcode
(pstate
, BINOP_ADD
); }
287 | AddExpression
'-' MulExpression
288 { write_exp_elt_opcode
(pstate
, BINOP_SUB
); }
289 | AddExpression
'~' MulExpression
290 { write_exp_elt_opcode
(pstate
, BINOP_CONCAT
); }
295 | MulExpression
'*' UnaryExpression
296 { write_exp_elt_opcode
(pstate
, BINOP_MUL
); }
297 | MulExpression
'/' UnaryExpression
298 { write_exp_elt_opcode
(pstate
, BINOP_DIV
); }
299 | MulExpression
'%' UnaryExpression
300 { write_exp_elt_opcode
(pstate
, BINOP_REM
); }
304 { write_exp_elt_opcode
(pstate
, UNOP_ADDR
); }
305 | INCREMENT UnaryExpression
306 { write_exp_elt_opcode
(pstate
, UNOP_PREINCREMENT
); }
307 | DECREMENT UnaryExpression
308 { write_exp_elt_opcode
(pstate
, UNOP_PREDECREMENT
); }
309 |
'*' UnaryExpression
310 { write_exp_elt_opcode
(pstate
, UNOP_IND
); }
311 |
'-' UnaryExpression
312 { write_exp_elt_opcode
(pstate
, UNOP_NEG
); }
313 |
'+' UnaryExpression
314 { write_exp_elt_opcode
(pstate
, UNOP_PLUS
); }
315 |
'!' UnaryExpression
316 { write_exp_elt_opcode
(pstate
, UNOP_LOGICAL_NOT
); }
317 |
'~' UnaryExpression
318 { write_exp_elt_opcode
(pstate
, UNOP_COMPLEMENT
); }
319 | TypeExp
'.' SIZEOF_KEYWORD
320 { write_exp_elt_opcode
(pstate
, UNOP_SIZEOF
); }
326 CAST_KEYWORD
'(' TypeExp
')' UnaryExpression
327 { write_exp_elt_opcode
(pstate
, UNOP_CAST_TYPE
); }
328 /* C style cast is illegal D, but is still recognised in
329 the grammar, so we keep this around for convenience. */
330 |
'(' TypeExp
')' UnaryExpression
331 { write_exp_elt_opcode
(pstate
, UNOP_CAST_TYPE
); }
337 | PostfixExpression HATHAT UnaryExpression
338 { write_exp_elt_opcode
(pstate
, BINOP_EXP
); }
343 | PostfixExpression
'.' COMPLETE
345 pstate
->mark_struct_expression
();
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 { pstate
->mark_struct_expression
();
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
); }
373 { pstate
->arglist_len
= 1; }
374 | ArgumentList
',' AssignExpression
375 { pstate
->arglist_len
++; }
380 { pstate
->arglist_len
= 0; }
385 PostfixExpression
'('
386 { pstate
->start_arglist
(); }
388 { write_exp_elt_opcode
(pstate
, OP_FUNCALL
);
389 write_exp_elt_longcst
(pstate
, pstate
->end_arglist
());
390 write_exp_elt_opcode
(pstate
, OP_FUNCALL
); }
394 PostfixExpression
'[' ArgumentList
']'
395 { if
(pstate
->arglist_len
> 0)
397 write_exp_elt_opcode
(pstate
, MULTI_SUBSCRIPT
);
398 write_exp_elt_longcst
(pstate
, pstate
->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
, pstate
->expression_context_block
,
424 VAR_DOMAIN
, &is_a_field_of_this
);
425 if
(sym.symbol
&& SYMBOL_CLASS
(sym.symbol
) != LOC_TYPEDEF
)
427 if
(symbol_read_needs_frame
(sym.symbol
))
428 pstate
->block_tracker
->update
(sym
);
429 write_exp_elt_opcode
(pstate
, OP_VAR_VALUE
);
430 write_exp_elt_block
(pstate
, sym.block
);
431 write_exp_elt_sym
(pstate
, sym.symbol
);
432 write_exp_elt_opcode
(pstate
, OP_VAR_VALUE
);
434 else if
(is_a_field_of_this.type
!= NULL
)
436 /* It hangs off of `this'. Must not inadvertently convert from a
437 method call to data ref. */
438 pstate
->block_tracker
->update
(sym
);
439 write_exp_elt_opcode
(pstate
, OP_THIS
);
440 write_exp_elt_opcode
(pstate
, OP_THIS
);
441 write_exp_elt_opcode
(pstate
, STRUCTOP_PTR
);
442 write_exp_string
(pstate
, $1);
443 write_exp_elt_opcode
(pstate
, STRUCTOP_PTR
);
447 /* Lookup foreign name in global static symbols. */
448 msymbol
= lookup_bound_minimal_symbol
(copy
);
449 if
(msymbol.minsym
!= NULL
)
450 write_exp_msymbol
(pstate
, msymbol
);
451 else if
(!have_full_symbols
() && !have_partial_symbols
())
452 error (_
("No symbol table is loaded. Use the \"file\" command"));
454 error (_
("No symbol \"%s\" in current context."), copy
);
457 | TypeExp
'.' IdentifierExp
458 { struct type
*type
= check_typedef
($1);
460 /* Check if the qualified name is in the global
461 context. However if the symbol has not already
462 been resolved, it's not likely to be found. */
463 if
(TYPE_CODE
(type
) == TYPE_CODE_MODULE
)
465 struct bound_minimal_symbol msymbol
;
466 struct block_symbol sym
;
467 const char *type_name
= TYPE_SAFE_NAME
(type
);
468 int type_name_len
= strlen
(type_name
);
470 = string_printf
("%.*s.%.*s",
471 type_name_len
, type_name
,
475 lookup_symbol
(name.c_str
(),
476 (const struct block
*) NULL
,
480 write_exp_elt_opcode
(pstate
, OP_VAR_VALUE
);
481 write_exp_elt_block
(pstate
, sym.block
);
482 write_exp_elt_sym
(pstate
, sym.symbol
);
483 write_exp_elt_opcode
(pstate
, OP_VAR_VALUE
);
487 msymbol
= lookup_bound_minimal_symbol
(name.c_str
());
488 if
(msymbol.minsym
!= NULL
)
489 write_exp_msymbol
(pstate
, msymbol
);
490 else if
(!have_full_symbols
() && !have_partial_symbols
())
491 error (_
("No symbol table is loaded. Use the \"file\" command."));
493 error (_
("No symbol \"%s\" in current context."),
497 /* Check if the qualified name resolves as a member
498 of an aggregate or an enum type. */
499 if
(!type_aggregate_p
(type
))
500 error (_
("`%s' is not defined as an aggregate type."),
501 TYPE_SAFE_NAME
(type
));
503 write_exp_elt_opcode
(pstate
, OP_SCOPE
);
504 write_exp_elt_type
(pstate
, type
);
505 write_exp_string
(pstate
, $3);
506 write_exp_elt_opcode
(pstate
, OP_SCOPE
);
509 { write_dollar_variable
(pstate
, $1); }
512 parse_number
(pstate
, $1.ptr
, $1.length
, 0, &val
);
513 write_exp_elt_opcode
(pstate
, OP_LONG
);
514 write_exp_elt_type
(pstate
, val.typed_val_int.type
);
515 write_exp_elt_longcst
(pstate
,
516 (LONGEST
) val.typed_val_int.val
);
517 write_exp_elt_opcode
(pstate
, OP_LONG
); }
519 { struct type
*type
= parse_d_type
(pstate
)->builtin_void
;
520 type
= lookup_pointer_type
(type
);
521 write_exp_elt_opcode
(pstate
, OP_LONG
);
522 write_exp_elt_type
(pstate
, type
);
523 write_exp_elt_longcst
(pstate
, (LONGEST
) 0);
524 write_exp_elt_opcode
(pstate
, OP_LONG
); }
526 { write_exp_elt_opcode
(pstate
, OP_BOOL
);
527 write_exp_elt_longcst
(pstate
, (LONGEST
) 1);
528 write_exp_elt_opcode
(pstate
, OP_BOOL
); }
530 { write_exp_elt_opcode
(pstate
, OP_BOOL
);
531 write_exp_elt_longcst
(pstate
, (LONGEST
) 0);
532 write_exp_elt_opcode
(pstate
, OP_BOOL
); }
534 { write_exp_elt_opcode
(pstate
, OP_LONG
);
535 write_exp_elt_type
(pstate
, $1.type
);
536 write_exp_elt_longcst
(pstate
, (LONGEST
)($1.val
));
537 write_exp_elt_opcode
(pstate
, OP_LONG
); }
539 { write_exp_elt_opcode
(pstate
, OP_FLOAT
);
540 write_exp_elt_type
(pstate
, $1.type
);
541 write_exp_elt_floatcst
(pstate
, $1.val
);
542 write_exp_elt_opcode
(pstate
, OP_FLOAT
); }
544 { struct stoken_vector vec
;
547 write_exp_string_vector
(pstate
, $1.type
, &vec
); }
550 write_exp_string_vector
(pstate
, 0, &$1);
551 for
(i
= 0; i
< $1.len
; ++i
)
552 free
($1.tokens
[i
].ptr
);
555 { write_exp_elt_opcode
(pstate
, OP_ARRAY
);
556 write_exp_elt_longcst
(pstate
, (LONGEST
) 0);
557 write_exp_elt_longcst
(pstate
, (LONGEST
) $1 - 1);
558 write_exp_elt_opcode
(pstate
, OP_ARRAY
); }
559 | TYPEOF_KEYWORD
'(' Expression
')'
560 { write_exp_elt_opcode
(pstate
, OP_TYPEOF
); }
564 '[' ArgumentList_opt
']'
565 { $$
= pstate
->arglist_len
; }
574 { /* We copy the string here, and not in the
575 lexer, to guarantee that we do not leak a
576 string. Note that we follow the
577 NUL-termination convention of the
579 struct typed_stoken
*vec
= XNEW
(struct typed_stoken
);
584 vec
->length
= $1.length
;
585 vec
->ptr
= (char *) malloc
($1.length
+ 1);
586 memcpy
(vec
->ptr
, $1.ptr
, $1.length
+ 1);
588 | StringExp STRING_LITERAL
589 { /* Note that we NUL-terminate here, but just
594 = XRESIZEVEC
(struct typed_stoken
, $$.tokens
, $$.len
);
596 p
= (char *) malloc
($2.length
+ 1);
597 memcpy
(p
, $2.ptr
, $2.length
+ 1);
599 $$.tokens
[$$.len
- 1].type
= $2.type
;
600 $$.tokens
[$$.len
- 1].length
= $2.length
;
601 $$.tokens
[$$.len
- 1].ptr
= p
;
607 { /* Do nothing. */ }
609 { write_exp_elt_opcode
(pstate
, OP_TYPE
);
610 write_exp_elt_type
(pstate
, $1);
611 write_exp_elt_opcode
(pstate
, OP_TYPE
); }
612 | BasicType BasicType2
613 { $$
= type_stack
->follow_types
($1);
614 write_exp_elt_opcode
(pstate
, OP_TYPE
);
615 write_exp_elt_type
(pstate
, $$
);
616 write_exp_elt_opcode
(pstate
, OP_TYPE
);
622 { type_stack
->push
(tp_pointer
); }
624 { type_stack
->push
(tp_pointer
); }
625 |
'[' INTEGER_LITERAL
']'
626 { type_stack
->push
($2.val
);
627 type_stack
->push
(tp_array
); }
628 |
'[' INTEGER_LITERAL
']' BasicType2
629 { type_stack
->push
($2.val
);
630 type_stack
->push
(tp_array
); }
640 /* Return true if the type is aggregate-like. */
643 type_aggregate_p
(struct type
*type
)
645 return
(TYPE_CODE
(type
) == TYPE_CODE_STRUCT
646 || TYPE_CODE
(type
) == TYPE_CODE_UNION
647 || TYPE_CODE
(type
) == TYPE_CODE_MODULE
648 ||
(TYPE_CODE
(type
) == TYPE_CODE_ENUM
649 && TYPE_DECLARED_CLASS
(type
)));
652 /* Take care of parsing a number (anything that starts with a digit).
653 Set yylval and return the token type; update lexptr.
654 LEN is the number of characters in it. */
656 /*** Needs some error checking for the float case ***/
659 parse_number
(struct parser_state
*ps
, const char *p
,
660 int len
, int parsed_float
, YYSTYPE *putithere
)
668 int base
= input_radix
;
672 /* We have found a "L" or "U" suffix. */
673 int found_suffix
= 0;
676 struct type
*signed_type
;
677 struct type
*unsigned_type
;
683 /* Strip out all embedded '_' before passing to parse_float. */
684 s
= (char *) alloca
(len
+ 1);
695 /* Check suffix for `i' , `fi' or `li' (idouble, ifloat or ireal). */
696 if
(len
>= 1 && tolower
(s
[len
- 1]) == 'i')
698 if
(len
>= 2 && tolower
(s
[len
- 2]) == 'f')
700 putithere
->typed_val_float.type
701 = parse_d_type
(ps
)->builtin_ifloat
;
704 else if
(len
>= 2 && tolower
(s
[len
- 2]) == 'l')
706 putithere
->typed_val_float.type
707 = parse_d_type
(ps
)->builtin_ireal
;
712 putithere
->typed_val_float.type
713 = parse_d_type
(ps
)->builtin_idouble
;
717 /* Check suffix for `f' or `l'' (float or real). */
718 else if
(len
>= 1 && tolower
(s
[len
- 1]) == 'f')
720 putithere
->typed_val_float.type
721 = parse_d_type
(ps
)->builtin_float
;
724 else if
(len
>= 1 && tolower
(s
[len
- 1]) == 'l')
726 putithere
->typed_val_float.type
727 = parse_d_type
(ps
)->builtin_real
;
730 /* Default type if no suffix. */
733 putithere
->typed_val_float.type
734 = parse_d_type
(ps
)->builtin_double
;
737 if
(!parse_float
(s
, len
,
738 putithere
->typed_val_float.type
,
739 putithere
->typed_val_float.val
))
742 return FLOAT_LITERAL
;
745 /* Handle base-switching prefixes 0x, 0b, 0 */
778 continue
; /* Ignore embedded '_'. */
779 if
(c
>= 'A' && c
<= 'Z')
781 if
(c
!= 'l' && c
!= 'u')
783 if
(c
>= '0' && c
<= '9')
791 if
(base
> 10 && c
>= 'a' && c
<= 'f')
795 n
+= i
= c
- 'a' + 10;
797 else if
(c
== 'l' && long_p
== 0)
802 else if
(c
== 'u' && unsigned_p
== 0)
808 return ERROR
; /* Char not a digit */
811 return ERROR
; /* Invalid digit in this base. */
812 /* Portably test for integer overflow. */
813 if
(c
!= 'l' && c
!= 'u')
815 ULONGEST n2
= prevn
* base
;
816 if
((n2
/ base
!= prevn
) ||
(n2
+ i
< prevn
))
817 error (_
("Numeric constant too large."));
822 /* An integer constant is an int or a long. An L suffix forces it to
823 be long, and a U suffix forces it to be unsigned. To figure out
824 whether it fits, we shift it right and see whether anything remains.
825 Note that we can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or
826 more in one operation, because many compilers will warn about such a
827 shift (which always produces a zero result). To deal with the case
828 where it is we just always shift the value more than once, with fewer
830 un
= (ULONGEST
) n
>> 2;
831 if
(long_p
== 0 && (un
>> 30) == 0)
833 high_bit
= ((ULONGEST
) 1) << 31;
834 signed_type
= parse_d_type
(ps
)->builtin_int
;
835 /* For decimal notation, keep the sign of the worked out type. */
836 if
(base
== 10 && !unsigned_p
)
837 unsigned_type
= parse_d_type
(ps
)->builtin_long
;
839 unsigned_type
= parse_d_type
(ps
)->builtin_uint
;
844 if
(sizeof
(ULONGEST
) * HOST_CHAR_BIT
< 64)
845 /* A long long does not fit in a LONGEST. */
846 shift
= (sizeof
(ULONGEST
) * HOST_CHAR_BIT
- 1);
849 high_bit
= (ULONGEST
) 1 << shift
;
850 signed_type
= parse_d_type
(ps
)->builtin_long
;
851 unsigned_type
= parse_d_type
(ps
)->builtin_ulong
;
854 putithere
->typed_val_int.val
= n
;
856 /* If the high bit of the worked out type is set then this number
857 has to be unsigned_type. */
858 if
(unsigned_p ||
(n
& high_bit
))
859 putithere
->typed_val_int.type
= unsigned_type
;
861 putithere
->typed_val_int.type
= signed_type
;
863 return INTEGER_LITERAL
;
866 /* Temporary obstack used for holding strings. */
867 static struct obstack tempbuf
;
868 static int tempbuf_init
;
870 /* Parse a string or character literal from TOKPTR. The string or
871 character may be wide or unicode. *OUTPTR is set to just after the
872 end of the literal in the input string. The resulting token is
873 stored in VALUE. This returns a token value, either STRING or
874 CHAR, depending on what was parsed. *HOST_CHARS is set to the
875 number of host characters in the literal. */
878 parse_string_or_char
(const char *tokptr
, const char **outptr
,
879 struct typed_stoken
*value
, int *host_chars
)
883 /* Build the gdb internal form of the input string in tempbuf. Note
884 that the buffer is null byte terminated *only* for the
885 convenience of debugging gdb itself and printing the buffer
886 contents when the buffer contains no embedded nulls. Gdb does
887 not depend upon the buffer being null byte terminated, it uses
888 the length string instead. This allows gdb to handle C strings
889 (as well as strings in other languages) with embedded null
895 obstack_free
(&tempbuf
, NULL
);
896 obstack_init
(&tempbuf
);
898 /* Skip the quote. */
910 *host_chars
+= c_parse_escape
(&tokptr
, &tempbuf
);
916 obstack_1grow
(&tempbuf
, c
);
918 /* FIXME: this does the wrong thing with multi-byte host
919 characters. We could use mbrlen here, but that would
920 make "set host-charset" a bit less useful. */
925 if
(*tokptr
!= quote
)
927 if
(quote
== '"' || quote
== '`')
928 error (_
("Unterminated string in expression."));
930 error (_
("Unmatched single quote."));
934 /* FIXME: should instead use own language string_type enum
935 and handle D-specific string suffixes here. */
937 value
->type
= C_CHAR
;
939 value
->type
= C_STRING
;
941 value
->ptr
= (char *) obstack_base
(&tempbuf
);
942 value
->length
= obstack_object_size
(&tempbuf
);
946 return quote
== '\'' ? CHARACTER_LITERAL
: STRING_LITERAL
;
953 enum exp_opcode opcode
;
956 static const struct token tokentab3
[] =
958 {"^^=", ASSIGN_MODIFY
, BINOP_EXP
},
959 {"<<=", ASSIGN_MODIFY
, BINOP_LSH
},
960 {">>=", ASSIGN_MODIFY
, BINOP_RSH
},
963 static const struct token tokentab2
[] =
965 {"+=", ASSIGN_MODIFY
, BINOP_ADD
},
966 {"-=", ASSIGN_MODIFY
, BINOP_SUB
},
967 {"*=", ASSIGN_MODIFY
, BINOP_MUL
},
968 {"/=", ASSIGN_MODIFY
, BINOP_DIV
},
969 {"%=", ASSIGN_MODIFY
, BINOP_REM
},
970 {"|=", ASSIGN_MODIFY
, BINOP_BITWISE_IOR
},
971 {"&=", ASSIGN_MODIFY
, BINOP_BITWISE_AND
},
972 {"^=", ASSIGN_MODIFY
, BINOP_BITWISE_XOR
},
973 {"++", INCREMENT
, BINOP_END
},
974 {"--", DECREMENT
, BINOP_END
},
975 {"&&", ANDAND
, BINOP_END
},
976 {"||", OROR
, BINOP_END
},
977 {"^^", HATHAT
, BINOP_END
},
978 {"<<", LSH
, BINOP_END
},
979 {">>", RSH
, BINOP_END
},
980 {"==", EQUAL
, BINOP_END
},
981 {"!=", NOTEQUAL
, BINOP_END
},
982 {"<=", LEQ
, BINOP_END
},
983 {">=", GEQ
, BINOP_END
},
984 {"..", DOTDOT
, BINOP_END
},
987 /* Identifier-like tokens. */
988 static const struct token ident_tokens
[] =
990 {"is", IDENTITY
, BINOP_END
},
991 {"!is", NOTIDENTITY
, BINOP_END
},
993 {"cast", CAST_KEYWORD
, OP_NULL
},
994 {"const", CONST_KEYWORD
, OP_NULL
},
995 {"immutable", IMMUTABLE_KEYWORD
, OP_NULL
},
996 {"shared", SHARED_KEYWORD
, OP_NULL
},
997 {"super", SUPER_KEYWORD
, OP_NULL
},
999 {"null", NULL_KEYWORD
, OP_NULL
},
1000 {"true", TRUE_KEYWORD
, OP_NULL
},
1001 {"false", FALSE_KEYWORD
, OP_NULL
},
1003 {"init", INIT_KEYWORD
, OP_NULL
},
1004 {"sizeof", SIZEOF_KEYWORD
, OP_NULL
},
1005 {"typeof", TYPEOF_KEYWORD
, OP_NULL
},
1006 {"typeid", TYPEID_KEYWORD
, OP_NULL
},
1008 {"delegate", DELEGATE_KEYWORD
, OP_NULL
},
1009 {"function", FUNCTION_KEYWORD
, OP_NULL
},
1010 {"struct", STRUCT_KEYWORD
, OP_NULL
},
1011 {"union", UNION_KEYWORD
, OP_NULL
},
1012 {"class", CLASS_KEYWORD
, OP_NULL
},
1013 {"interface", INTERFACE_KEYWORD
, OP_NULL
},
1014 {"enum", ENUM_KEYWORD
, OP_NULL
},
1015 {"template", TEMPLATE_KEYWORD
, OP_NULL
},
1018 /* This is set if a NAME token appeared at the very end of the input
1019 string, with no whitespace separating the name from the EOF. This
1020 is used only when parsing to do field name completion. */
1021 static int saw_name_at_eof
;
1023 /* This is set if the previously-returned token was a structure operator.
1024 This is used only when parsing to do field name completion. */
1025 static int last_was_structop
;
1027 /* Depth of parentheses. */
1028 static int paren_depth
;
1030 /* Read one token, getting characters through lexptr. */
1033 lex_one_token
(struct parser_state
*par_state
)
1038 const char *tokstart
;
1039 int saw_structop
= last_was_structop
;
1042 last_was_structop
= 0;
1046 pstate
->prev_lexptr
= pstate
->lexptr
;
1048 tokstart
= pstate
->lexptr
;
1049 /* See if it is a special token of length 3. */
1050 for
(i
= 0; i
< sizeof tokentab3
/ sizeof tokentab3
[0]; i
++)
1051 if
(strncmp
(tokstart
, tokentab3
[i
].oper
, 3) == 0)
1053 pstate
->lexptr
+= 3;
1054 yylval.opcode
= tokentab3
[i
].opcode
;
1055 return tokentab3
[i
].token
;
1058 /* See if it is a special token of length 2. */
1059 for
(i
= 0; i
< sizeof tokentab2
/ sizeof tokentab2
[0]; i
++)
1060 if
(strncmp
(tokstart
, tokentab2
[i
].oper
, 2) == 0)
1062 pstate
->lexptr
+= 2;
1063 yylval.opcode
= tokentab2
[i
].opcode
;
1064 return tokentab2
[i
].token
;
1067 switch
(c
= *tokstart
)
1070 /* If we're parsing for field name completion, and the previous
1071 token allows such completion, return a COMPLETE token.
1072 Otherwise, we were already scanning the original text, and
1073 we're really done. */
1074 if
(saw_name_at_eof
)
1076 saw_name_at_eof
= 0;
1079 else if
(saw_structop
)
1098 if
(paren_depth
== 0)
1105 if
(pstate
->comma_terminates
&& paren_depth
== 0)
1111 /* Might be a floating point number. */
1112 if
(pstate
->lexptr
[1] < '0' || pstate
->lexptr
[1] > '9')
1114 if
(pstate
->parse_completion
)
1115 last_was_structop
= 1;
1116 goto symbol
; /* Nope, must be a symbol. */
1131 /* It's a number. */
1132 int got_dot
= 0, got_e
= 0, toktype
;
1133 const char *p
= tokstart
;
1134 int hex
= input_radix
> 10;
1136 if
(c
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
1144 /* Hex exponents start with 'p', because 'e' is a valid hex
1145 digit and thus does not indicate a floating point number
1146 when the radix is hex. */
1147 if
((!hex
&& !got_e
&& tolower
(p
[0]) == 'e')
1148 ||
(hex
&& !got_e
&& tolower
(p
[0] == 'p')))
1149 got_dot
= got_e
= 1;
1150 /* A '.' always indicates a decimal floating point number
1151 regardless of the radix. If we have a '..' then its the
1152 end of the number and the beginning of a slice. */
1153 else if
(!got_dot
&& (p
[0] == '.' && p
[1] != '.'))
1155 /* This is the sign of the exponent, not the end of the number. */
1156 else if
(got_e
&& (tolower
(p
[-1]) == 'e' || tolower
(p
[-1]) == 'p')
1157 && (*p
== '-' ||
*p
== '+'))
1159 /* We will take any letters or digits, ignoring any embedded '_'.
1160 parse_number will complain if past the radix, or if L or U are
1162 else if
((*p
< '0' ||
*p
> '9') && (*p
!= '_')
1163 && ((*p
< 'a' ||
*p
> 'z') && (*p
< 'A' ||
*p
> 'Z')))
1167 toktype
= parse_number
(par_state
, tokstart
, p
- tokstart
,
1168 got_dot|got_e
, &yylval);
1169 if
(toktype
== ERROR
)
1171 char *err_copy
= (char *) alloca
(p
- tokstart
+ 1);
1173 memcpy
(err_copy
, tokstart
, p
- tokstart
);
1174 err_copy
[p
- tokstart
] = 0;
1175 error (_
("Invalid number \"%s\"."), err_copy
);
1183 const char *p
= &tokstart
[1];
1184 size_t len
= strlen
("entry");
1186 while
(isspace
(*p
))
1188 if
(strncmp
(p
, "entry", len
) == 0 && !isalnum
(p
[len
])
1191 pstate
->lexptr
= &p
[len
];
1222 int result
= parse_string_or_char
(tokstart
, &pstate
->lexptr
,
1223 &yylval.tsval
, &host_len
);
1224 if
(result
== CHARACTER_LITERAL
)
1227 error (_
("Empty character constant."));
1228 else if
(host_len
> 2 && c
== '\'')
1231 namelen
= pstate
->lexptr
- tokstart
- 1;
1234 else if
(host_len
> 1)
1235 error (_
("Invalid character constant."));
1241 if
(!(c
== '_' || c
== '$'
1242 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z')))
1243 /* We must have come across a bad character (e.g. ';'). */
1244 error (_
("Invalid character '%c' in expression"), c
);
1246 /* It's a name. See how long it is. */
1248 for
(c
= tokstart
[namelen
];
1249 (c
== '_' || c
== '$' ||
(c
>= '0' && c
<= '9')
1250 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z'));)
1251 c
= tokstart
[++namelen
];
1253 /* The token "if" terminates the expression and is NOT
1254 removed from the input stream. */
1255 if
(namelen
== 2 && tokstart
[0] == 'i' && tokstart
[1] == 'f')
1258 /* For the same reason (breakpoint conditions), "thread N"
1259 terminates the expression. "thread" could be an identifier, but
1260 an identifier is never followed by a number without intervening
1261 punctuation. "task" is similar. Handle abbreviations of these,
1262 similarly to breakpoint.c:find_condition_and_thread. */
1264 && (strncmp
(tokstart
, "thread", namelen
) == 0
1265 || strncmp
(tokstart
, "task", namelen
) == 0)
1266 && (tokstart
[namelen
] == ' ' || tokstart
[namelen
] == '\t'))
1268 const char *p
= tokstart
+ namelen
+ 1;
1270 while
(*p
== ' ' ||
*p
== '\t')
1272 if
(*p
>= '0' && *p
<= '9')
1276 pstate
->lexptr
+= namelen
;
1280 yylval.sval.ptr
= tokstart
;
1281 yylval.sval.length
= namelen
;
1283 /* Catch specific keywords. */
1284 copy
= copy_name
(yylval.sval
);
1285 for
(i
= 0; i
< sizeof ident_tokens
/ sizeof ident_tokens
[0]; i
++)
1286 if
(strcmp
(copy
, ident_tokens
[i
].oper
) == 0)
1288 /* It is ok to always set this, even though we don't always
1289 strictly need to. */
1290 yylval.opcode
= ident_tokens
[i
].opcode
;
1291 return ident_tokens
[i
].token
;
1294 if
(*tokstart
== '$')
1295 return DOLLAR_VARIABLE
;
1298 = language_lookup_primitive_type
(par_state
->language
(),
1299 par_state
->gdbarch
(), copy
);
1300 if
(yylval.tsym.type
!= NULL
)
1303 /* Input names that aren't symbols but ARE valid hex numbers,
1304 when the input radix permits them, can be names or numbers
1305 depending on the parse. Note we support radixes > 16 here. */
1306 if
((tokstart
[0] >= 'a' && tokstart
[0] < 'a' + input_radix
- 10)
1307 ||
(tokstart
[0] >= 'A' && tokstart
[0] < 'A' + input_radix
- 10))
1309 YYSTYPE newlval
; /* Its value is ignored. */
1310 int hextype
= parse_number
(par_state
, tokstart
, namelen
, 0, &newlval
);
1311 if
(hextype
== INTEGER_LITERAL
)
1315 if
(pstate
->parse_completion
&& *pstate
->lexptr
== '\0')
1316 saw_name_at_eof
= 1;
1321 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
1322 struct token_and_value
1329 /* A FIFO of tokens that have been read but not yet returned to the
1331 static std
::vector
<token_and_value
> token_fifo
;
1333 /* Non-zero if the lexer should return tokens from the FIFO. */
1336 /* Temporary storage for yylex; this holds symbol names as they are
1338 static auto_obstack name_obstack
;
1340 /* Classify an IDENTIFIER token. The contents of the token are in `yylval'.
1341 Updates yylval and returns the new token type. BLOCK is the block
1342 in which lookups start; this can be NULL to mean the global scope. */
1345 classify_name
(struct parser_state
*par_state
, const struct block
*block
)
1347 struct block_symbol sym
;
1349 struct field_of_this_result is_a_field_of_this
;
1351 copy
= copy_name
(yylval.sval
);
1353 sym
= lookup_symbol
(copy
, block
, VAR_DOMAIN
, &is_a_field_of_this
);
1354 if
(sym.symbol
&& SYMBOL_CLASS
(sym.symbol
) == LOC_TYPEDEF
)
1356 yylval.tsym.type
= SYMBOL_TYPE
(sym.symbol
);
1359 else if
(sym.symbol
== NULL
)
1361 /* Look-up first for a module name, then a type. */
1362 sym
= lookup_symbol
(copy
, block
, MODULE_DOMAIN
, NULL
);
1363 if
(sym.symbol
== NULL
)
1364 sym
= lookup_symbol
(copy
, block
, STRUCT_DOMAIN
, NULL
);
1366 if
(sym.symbol
!= NULL
)
1368 yylval.tsym.type
= SYMBOL_TYPE
(sym.symbol
);
1372 return UNKNOWN_NAME
;
1378 /* Like classify_name, but used by the inner loop of the lexer, when a
1379 name might have already been seen. CONTEXT is the context type, or
1380 NULL if this is the first component of a name. */
1383 classify_inner_name
(struct parser_state
*par_state
,
1384 const struct block
*block
, struct type
*context
)
1389 if
(context
== NULL
)
1390 return classify_name
(par_state
, block
);
1392 type
= check_typedef
(context
);
1393 if
(!type_aggregate_p
(type
))
1396 copy
= copy_name
(yylval.ssym.stoken
);
1397 yylval.ssym.sym
= d_lookup_nested_symbol
(type
, copy
, block
);
1399 if
(yylval.ssym.sym.symbol
== NULL
)
1402 if
(SYMBOL_CLASS
(yylval.ssym.sym.symbol
) == LOC_TYPEDEF
)
1404 yylval.tsym.type
= SYMBOL_TYPE
(yylval.ssym.sym.symbol
);
1411 /* The outer level of a two-level lexer. This calls the inner lexer
1412 to return tokens. It then either returns these tokens, or
1413 aggregates them into a larger token. This lets us work around a
1414 problem in our parsing approach, where the parser could not
1415 distinguish between qualified names and qualified types at the
1421 token_and_value current
;
1423 struct type
*context_type
= NULL
;
1424 int last_to_examine
, next_to_examine
, checkpoint
;
1425 const struct block
*search_block
;
1427 if
(popping
&& !token_fifo.empty
())
1431 /* Read the first token and decide what to do. */
1432 current.token
= lex_one_token
(pstate
);
1433 if
(current.token
!= IDENTIFIER
&& current.token
!= '.')
1434 return current.token
;
1436 /* Read any sequence of alternating "." and identifier tokens into
1438 current.value
= yylval;
1439 token_fifo.push_back
(current
);
1440 last_was_dot
= current.token
== '.';
1444 current.token
= lex_one_token
(pstate
);
1445 current.value
= yylval;
1446 token_fifo.push_back
(current
);
1448 if
((last_was_dot
&& current.token
!= IDENTIFIER
)
1449 ||
(!last_was_dot
&& current.token
!= '.'))
1452 last_was_dot
= !last_was_dot
;
1456 /* We always read one extra token, so compute the number of tokens
1457 to examine accordingly. */
1458 last_to_examine
= token_fifo.size
() - 2;
1459 next_to_examine
= 0;
1461 current
= token_fifo
[next_to_examine
];
1464 /* If we are not dealing with a typename, now is the time to find out. */
1465 if
(current.token
== IDENTIFIER
)
1467 yylval = current.value
;
1468 current.token
= classify_name
(pstate
, pstate
->expression_context_block
);
1469 current.value
= yylval;
1472 /* If the IDENTIFIER is not known, it could be a package symbol,
1473 first try building up a name until we find the qualified module. */
1474 if
(current.token
== UNKNOWN_NAME
)
1476 name_obstack.clear
();
1477 obstack_grow
(&name_obstack
, current.value.sval.ptr
,
1478 current.value.sval.length
);
1482 while
(next_to_examine
<= last_to_examine
)
1484 token_and_value next
;
1486 next
= token_fifo
[next_to_examine
];
1489 if
(next.token
== IDENTIFIER
&& last_was_dot
)
1491 /* Update the partial name we are constructing. */
1492 obstack_grow_str
(&name_obstack
, ".");
1493 obstack_grow
(&name_obstack
, next.value.sval.ptr
,
1494 next.value.sval.length
);
1496 yylval.sval.ptr
= (char *) obstack_base
(&name_obstack
);
1497 yylval.sval.length
= obstack_object_size
(&name_obstack
);
1499 current.token
= classify_name
(pstate
,
1500 pstate
->expression_context_block
);
1501 current.value
= yylval;
1503 /* We keep going until we find a TYPENAME. */
1504 if
(current.token
== TYPENAME
)
1506 /* Install it as the first token in the FIFO. */
1507 token_fifo
[0] = current
;
1508 token_fifo.erase
(token_fifo.begin
() + 1,
1509 token_fifo.begin
() + next_to_examine
);
1513 else if
(next.token
== '.' && !last_was_dot
)
1517 /* We've reached the end of the name. */
1522 /* Reset our current token back to the start, if we found nothing
1523 this means that we will just jump to do pop. */
1524 current
= token_fifo
[0];
1525 next_to_examine
= 1;
1527 if
(current.token
!= TYPENAME
&& current.token
!= '.')
1530 name_obstack.clear
();
1532 if
(current.token
== '.')
1533 search_block
= NULL
;
1536 gdb_assert
(current.token
== TYPENAME
);
1537 search_block
= pstate
->expression_context_block
;
1538 obstack_grow
(&name_obstack
, current.value.sval.ptr
,
1539 current.value.sval.length
);
1540 context_type
= current.value.tsym.type
;
1544 last_was_dot
= current.token
== '.';
1546 while
(next_to_examine
<= last_to_examine
)
1548 token_and_value next
;
1550 next
= token_fifo
[next_to_examine
];
1553 if
(next.token
== IDENTIFIER
&& last_was_dot
)
1557 yylval = next.value
;
1558 classification
= classify_inner_name
(pstate
, search_block
,
1560 /* We keep going until we either run out of names, or until
1561 we have a qualified name which is not a type. */
1562 if
(classification
!= TYPENAME
&& classification
!= IDENTIFIER
)
1565 /* Accept up to this token. */
1566 checkpoint
= next_to_examine
;
1568 /* Update the partial name we are constructing. */
1569 if
(context_type
!= NULL
)
1571 /* We don't want to put a leading "." into the name. */
1572 obstack_grow_str
(&name_obstack
, ".");
1574 obstack_grow
(&name_obstack
, next.value.sval.ptr
,
1575 next.value.sval.length
);
1577 yylval.sval.ptr
= (char *) obstack_base
(&name_obstack
);
1578 yylval.sval.length
= obstack_object_size
(&name_obstack
);
1579 current.value
= yylval;
1580 current.token
= classification
;
1584 if
(classification
== IDENTIFIER
)
1587 context_type
= yylval.tsym.type
;
1589 else if
(next.token
== '.' && !last_was_dot
)
1593 /* We've reached the end of the name. */
1598 /* If we have a replacement token, install it as the first token in
1599 the FIFO, and delete the other constituent tokens. */
1602 token_fifo
[0] = current
;
1604 token_fifo.erase
(token_fifo.begin
() + 1,
1605 token_fifo.begin
() + checkpoint
);
1609 current
= token_fifo
[0];
1610 token_fifo.erase
(token_fifo.begin
());
1611 yylval = current.value
;
1612 return current.token
;
1616 d_parse
(struct parser_state
*par_state
)
1618 /* Setting up the parser state. */
1619 scoped_restore pstate_restore
= make_scoped_restore
(&pstate
);
1620 gdb_assert
(par_state
!= NULL
);
1623 scoped_restore restore_yydebug
= make_scoped_restore
(&yydebug,
1626 struct type_stack stack
;
1627 scoped_restore restore_type_stack
= make_scoped_restore
(&type_stack
,
1630 /* Initialize some state used by the lexer. */
1631 last_was_structop
= 0;
1632 saw_name_at_eof
= 0;
1635 token_fifo.clear
();
1637 name_obstack.clear
();
1643 yyerror (const char *msg
)
1645 if
(pstate
->prev_lexptr
)
1646 pstate
->lexptr
= pstate
->prev_lexptr
;
1648 error (_
("A %s in expression, near `%s'."), msg
, pstate
->lexptr
);