]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/d-exp.y
[gdb/tdep] Fix inferior call return of small char array for ppc64 v1 abi some more
[thirdparty/binutils-gdb.git] / gdb / d-exp.y
CommitLineData
3ed9baed
IB
1/* YACC parser for D expressions, for GDB.
2
d01e8234 3 Copyright (C) 2014-2025 Free Software Foundation, Inc.
3ed9baed
IB
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20/* This file is derived from c-exp.y, jv-exp.y. */
21
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.
30
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. */
38
39%{
40
3ed9baed
IB
41#include "expression.h"
42#include "value.h"
43#include "parser-defs.h"
44#include "language.h"
45#include "c-lang.h"
46#include "d-lang.h"
3ed9baed
IB
47#include "charset.h"
48#include "block.h"
dac43e32 49#include "type-stack.h"
9412fdcc 50#include "expop.h"
3ed9baed 51
fa9f5be6
TT
52#define parse_type(ps) builtin_type (ps->gdbarch ())
53#define parse_d_type(ps) builtin_d_type (ps->gdbarch ())
3ed9baed 54
b3f11165
PA
55/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
56 etc). */
57#define GDB_YY_REMAP_PREFIX d_
58#include "yy-remap.h"
3ed9baed
IB
59
60/* The state of the parser, used internally when we are parsing the
61 expression. */
62
63static struct parser_state *pstate = NULL;
64
dac43e32
TT
65/* The current type stack. */
66static struct type_stack *type_stack;
67
3ed9baed
IB
68int yyparse (void);
69
70static int yylex (void);
71
69d340c6 72static void yyerror (const char *);
3ed9baed 73
7f3706eb
IB
74static int type_aggregate_p (struct type *);
75
9412fdcc
TT
76using namespace expr;
77
3ed9baed
IB
78%}
79
80/* Although the yacc "value" of an expression is not used,
81 since the result is stored in the structure being created,
82 other node types do have values. */
83
84%union
85 {
86 struct {
87 LONGEST val;
88 struct type *type;
89 } typed_val_int;
90 struct {
edd079d9 91 gdb_byte val[16];
3ed9baed
IB
92 struct type *type;
93 } typed_val_float;
94 struct symbol *sym;
95 struct type *tval;
96 struct typed_stoken tsval;
97 struct stoken sval;
98 struct ttype tsym;
99 struct symtoken ssym;
100 int ival;
444c1ed8 101 int voidval;
3ed9baed
IB
102 enum exp_opcode opcode;
103 struct stoken_vector svec;
104 }
105
106%{
107/* YYSTYPE gets defined by %union */
108static int parse_number (struct parser_state *, const char *,
109 int, int, YYSTYPE *);
3ed9baed
IB
110%}
111
444c1ed8 112%token <sval> IDENTIFIER UNKNOWN_NAME
3ed9baed
IB
113%token <tsym> TYPENAME
114%token <voidval> COMPLETE
115
116/* A NAME_OR_INT is a symbol which is not known in the symbol table,
117 but which would parse as a valid number in the current input radix.
118 E.g. "c" when input_radix==16. Depending on the parse, it will be
119 turned into a name or into a number. */
120
121%token <sval> NAME_OR_INT
122
123%token <typed_val_int> INTEGER_LITERAL
124%token <typed_val_float> FLOAT_LITERAL
125%token <tsval> CHARACTER_LITERAL
126%token <tsval> STRING_LITERAL
127
128%type <svec> StringExp
129%type <tval> BasicType TypeExp
130%type <sval> IdentifierExp
131%type <ival> ArrayLiteral
132
133%token ENTRY
134%token ERROR
135
136/* Keywords that have a constant value. */
137%token TRUE_KEYWORD FALSE_KEYWORD NULL_KEYWORD
138/* Class 'super' accessor. */
139%token SUPER_KEYWORD
140/* Properties. */
141%token CAST_KEYWORD SIZEOF_KEYWORD
142%token TYPEOF_KEYWORD TYPEID_KEYWORD
143%token INIT_KEYWORD
144/* Comparison keywords. */
145/* Type storage classes. */
146%token IMMUTABLE_KEYWORD CONST_KEYWORD SHARED_KEYWORD
147/* Non-scalar type keywords. */
148%token STRUCT_KEYWORD UNION_KEYWORD
149%token CLASS_KEYWORD INTERFACE_KEYWORD
150%token ENUM_KEYWORD TEMPLATE_KEYWORD
151%token DELEGATE_KEYWORD FUNCTION_KEYWORD
152
153%token <sval> DOLLAR_VARIABLE
154
155%token <opcode> ASSIGN_MODIFY
156
157%left ','
158%right '=' ASSIGN_MODIFY
159%right '?'
160%left OROR
161%left ANDAND
162%left '|'
163%left '^'
164%left '&'
165%left EQUAL NOTEQUAL '<' '>' LEQ GEQ
166%right LSH RSH
167%left '+' '-'
168%left '*' '/' '%'
169%right HATHAT
170%left IDENTITY NOTIDENTITY
171%right INCREMENT DECREMENT
172%right '.' '[' '('
173%token DOTDOT
174
175\f
176%%
177
178start :
179 Expression
180| TypeExp
181;
182
183/* Expressions, including the comma operator. */
184
185Expression:
186 CommaExpression
187;
188
189CommaExpression:
190 AssignExpression
191| AssignExpression ',' CommaExpression
9412fdcc 192 { pstate->wrap2<comma_operation> (); }
3ed9baed
IB
193;
194
195AssignExpression:
196 ConditionalExpression
197| ConditionalExpression '=' AssignExpression
9412fdcc 198 { pstate->wrap2<assign_operation> (); }
3ed9baed 199| ConditionalExpression ASSIGN_MODIFY AssignExpression
9412fdcc
TT
200 {
201 operation_up rhs = pstate->pop ();
202 operation_up lhs = pstate->pop ();
203 pstate->push_new<assign_modify_operation>
204 ($2, std::move (lhs), std::move (rhs));
205 }
3ed9baed
IB
206;
207
208ConditionalExpression:
209 OrOrExpression
210| OrOrExpression '?' Expression ':' ConditionalExpression
9412fdcc
TT
211 {
212 operation_up last = pstate->pop ();
213 operation_up mid = pstate->pop ();
214 operation_up first = pstate->pop ();
215 pstate->push_new<ternop_cond_operation>
216 (std::move (first), std::move (mid),
217 std::move (last));
218 }
3ed9baed
IB
219;
220
221OrOrExpression:
222 AndAndExpression
223| OrOrExpression OROR AndAndExpression
9412fdcc 224 { pstate->wrap2<logical_or_operation> (); }
3ed9baed
IB
225;
226
227AndAndExpression:
228 OrExpression
229| AndAndExpression ANDAND OrExpression
9412fdcc 230 { pstate->wrap2<logical_and_operation> (); }
3ed9baed
IB
231;
232
233OrExpression:
234 XorExpression
235| OrExpression '|' XorExpression
9412fdcc 236 { pstate->wrap2<bitwise_ior_operation> (); }
3ed9baed
IB
237;
238
239XorExpression:
240 AndExpression
241| XorExpression '^' AndExpression
9412fdcc 242 { pstate->wrap2<bitwise_xor_operation> (); }
3ed9baed
IB
243;
244
245AndExpression:
246 CmpExpression
247| AndExpression '&' CmpExpression
9412fdcc 248 { pstate->wrap2<bitwise_and_operation> (); }
3ed9baed
IB
249;
250
251CmpExpression:
252 ShiftExpression
253| EqualExpression
254| IdentityExpression
255| RelExpression
256;
257
258EqualExpression:
259 ShiftExpression EQUAL ShiftExpression
9412fdcc 260 { pstate->wrap2<equal_operation> (); }
3ed9baed 261| ShiftExpression NOTEQUAL ShiftExpression
9412fdcc 262 { pstate->wrap2<notequal_operation> (); }
3ed9baed
IB
263;
264
265IdentityExpression:
266 ShiftExpression IDENTITY ShiftExpression
9412fdcc 267 { pstate->wrap2<equal_operation> (); }
3ed9baed 268| ShiftExpression NOTIDENTITY ShiftExpression
9412fdcc 269 { pstate->wrap2<notequal_operation> (); }
3ed9baed
IB
270;
271
272RelExpression:
273 ShiftExpression '<' ShiftExpression
9412fdcc 274 { pstate->wrap2<less_operation> (); }
3ed9baed 275| ShiftExpression LEQ ShiftExpression
9412fdcc 276 { pstate->wrap2<leq_operation> (); }
3ed9baed 277| ShiftExpression '>' ShiftExpression
9412fdcc 278 { pstate->wrap2<gtr_operation> (); }
3ed9baed 279| ShiftExpression GEQ ShiftExpression
9412fdcc 280 { pstate->wrap2<geq_operation> (); }
3ed9baed
IB
281;
282
283ShiftExpression:
284 AddExpression
285| ShiftExpression LSH AddExpression
9412fdcc 286 { pstate->wrap2<lsh_operation> (); }
3ed9baed 287| ShiftExpression RSH AddExpression
9412fdcc 288 { pstate->wrap2<rsh_operation> (); }
3ed9baed
IB
289;
290
291AddExpression:
292 MulExpression
293| AddExpression '+' MulExpression
9412fdcc 294 { pstate->wrap2<add_operation> (); }
3ed9baed 295| AddExpression '-' MulExpression
9412fdcc 296 { pstate->wrap2<sub_operation> (); }
3ed9baed 297| AddExpression '~' MulExpression
9412fdcc 298 { pstate->wrap2<concat_operation> (); }
3ed9baed
IB
299;
300
301MulExpression:
302 UnaryExpression
303| MulExpression '*' UnaryExpression
9412fdcc 304 { pstate->wrap2<mul_operation> (); }
3ed9baed 305| MulExpression '/' UnaryExpression
9412fdcc 306 { pstate->wrap2<div_operation> (); }
3ed9baed 307| MulExpression '%' UnaryExpression
9412fdcc 308 { pstate->wrap2<rem_operation> (); }
3ed9baed
IB
309
310UnaryExpression:
311 '&' UnaryExpression
9412fdcc 312 { pstate->wrap<unop_addr_operation> (); }
3ed9baed 313| INCREMENT UnaryExpression
9412fdcc 314 { pstate->wrap<preinc_operation> (); }
3ed9baed 315| DECREMENT UnaryExpression
9412fdcc 316 { pstate->wrap<predec_operation> (); }
3ed9baed 317| '*' UnaryExpression
9412fdcc 318 { pstate->wrap<unop_ind_operation> (); }
3ed9baed 319| '-' UnaryExpression
9412fdcc 320 { pstate->wrap<unary_neg_operation> (); }
3ed9baed 321| '+' UnaryExpression
9412fdcc 322 { pstate->wrap<unary_plus_operation> (); }
3ed9baed 323| '!' UnaryExpression
9412fdcc 324 { pstate->wrap<unary_logical_not_operation> (); }
3ed9baed 325| '~' UnaryExpression
9412fdcc 326 { pstate->wrap<unary_complement_operation> (); }
d5d8c4e1 327| TypeExp '.' SIZEOF_KEYWORD
9412fdcc 328 { pstate->wrap<unop_sizeof_operation> (); }
3ed9baed
IB
329| CastExpression
330| PowExpression
331;
332
333CastExpression:
334 CAST_KEYWORD '(' TypeExp ')' UnaryExpression
9412fdcc 335 { pstate->wrap2<unop_cast_type_operation> (); }
3ed9baed
IB
336 /* C style cast is illegal D, but is still recognised in
337 the grammar, so we keep this around for convenience. */
338| '(' TypeExp ')' UnaryExpression
9412fdcc 339 { pstate->wrap2<unop_cast_type_operation> (); }
3ed9baed
IB
340;
341
342PowExpression:
343 PostfixExpression
344| PostfixExpression HATHAT UnaryExpression
9412fdcc 345 { pstate->wrap2<exp_operation> (); }
3ed9baed
IB
346;
347
348PostfixExpression:
349 PrimaryExpression
444c1ed8 350| PostfixExpression '.' COMPLETE
9412fdcc
TT
351 {
352 structop_base_operation *op
353 = new structop_ptr_operation (pstate->pop (), "");
354 pstate->mark_struct_expression (op);
355 pstate->push (operation_up (op));
356 }
444c1ed8 357| PostfixExpression '.' IDENTIFIER
9412fdcc
TT
358 {
359 pstate->push_new<structop_operation>
360 (pstate->pop (), copy_name ($3));
361 }
444c1ed8 362| PostfixExpression '.' IDENTIFIER COMPLETE
9412fdcc
TT
363 {
364 structop_base_operation *op
365 = new structop_operation (pstate->pop (), copy_name ($3));
366 pstate->mark_struct_expression (op);
367 pstate->push (operation_up (op));
368 }
d5d8c4e1 369| PostfixExpression '.' SIZEOF_KEYWORD
9412fdcc 370 { pstate->wrap<unop_sizeof_operation> (); }
3ed9baed 371| PostfixExpression INCREMENT
9412fdcc 372 { pstate->wrap<postinc_operation> (); }
3ed9baed 373| PostfixExpression DECREMENT
9412fdcc 374 { pstate->wrap<postdec_operation> (); }
3ed9baed
IB
375| CallExpression
376| IndexExpression
377| SliceExpression
378;
379
380ArgumentList:
381 AssignExpression
43476f0b 382 { pstate->arglist_len = 1; }
3ed9baed 383| ArgumentList ',' AssignExpression
43476f0b 384 { pstate->arglist_len++; }
3ed9baed
IB
385;
386
387ArgumentList_opt:
388 /* EMPTY */
43476f0b 389 { pstate->arglist_len = 0; }
3ed9baed
IB
390| ArgumentList
391;
392
393CallExpression:
394 PostfixExpression '('
43476f0b 395 { pstate->start_arglist (); }
3ed9baed 396 ArgumentList_opt ')'
9412fdcc
TT
397 {
398 std::vector<operation_up> args
399 = pstate->pop_vector (pstate->end_arglist ());
400 pstate->push_new<funcall_operation>
401 (pstate->pop (), std::move (args));
402 }
3ed9baed
IB
403;
404
405IndexExpression:
406 PostfixExpression '[' ArgumentList ']'
43476f0b 407 { if (pstate->arglist_len > 0)
3ed9baed 408 {
9412fdcc
TT
409 std::vector<operation_up> args
410 = pstate->pop_vector (pstate->arglist_len);
411 pstate->push_new<multi_subscript_operation>
412 (pstate->pop (), std::move (args));
3ed9baed
IB
413 }
414 else
9412fdcc 415 pstate->wrap2<subscript_operation> ();
3ed9baed
IB
416 }
417;
418
419SliceExpression:
420 PostfixExpression '[' ']'
421 { /* Do nothing. */ }
422| PostfixExpression '[' AssignExpression DOTDOT AssignExpression ']'
9412fdcc
TT
423 {
424 operation_up last = pstate->pop ();
425 operation_up mid = pstate->pop ();
426 operation_up first = pstate->pop ();
427 pstate->push_new<ternop_slice_operation>
428 (std::move (first), std::move (mid),
429 std::move (last));
430 }
3ed9baed
IB
431;
432
433PrimaryExpression:
434 '(' Expression ')'
435 { /* Do nothing. */ }
436| IdentifierExp
03b40f6f 437 {
61f4b350 438 std::string copy = copy_name ($1);
444c1ed8
IB
439 struct field_of_this_result is_a_field_of_this;
440 struct block_symbol sym;
441
442 /* Handle VAR, which could be local or global. */
61f4b350
TT
443 sym = lookup_symbol (copy.c_str (),
444 pstate->expression_context_block,
ccf41c24 445 SEARCH_VFT, &is_a_field_of_this);
4260abb7 446 if (sym.symbol && sym.symbol->loc_class () != LOC_TYPEDEF)
444c1ed8
IB
447 {
448 if (symbol_read_needs_frame (sym.symbol))
699bd4cf 449 pstate->block_tracker->update (sym);
9e5e03df 450 pstate->push_new<var_value_operation> (sym);
444c1ed8
IB
451 }
452 else if (is_a_field_of_this.type != NULL)
453 {
454 /* It hangs off of `this'. Must not inadvertently convert from a
455 method call to data ref. */
699bd4cf 456 pstate->block_tracker->update (sym);
9412fdcc
TT
457 operation_up thisop
458 = make_operation<op_this_operation> ();
459 pstate->push_new<structop_ptr_operation>
460 (std::move (thisop), std::move (copy));
444c1ed8
IB
461 }
462 else
463 {
464 /* Lookup foreign name in global static symbols. */
03b40f6f 465 bound_minimal_symbol msymbol
4144d36a 466 = lookup_minimal_symbol (current_program_space, copy.c_str ());
444c1ed8 467 if (msymbol.minsym != NULL)
9c79936b 468 pstate->push_new<var_msym_value_operation> (msymbol);
9c067e28
SM
469 else if (!have_full_symbols (current_program_space)
470 && !have_partial_symbols (current_program_space))
444c1ed8
IB
471 error (_("No symbol table is loaded. Use the \"file\" command"));
472 else
61f4b350
TT
473 error (_("No symbol \"%s\" in current context."),
474 copy.c_str ());
444c1ed8
IB
475 }
476 }
477| TypeExp '.' IdentifierExp
478 { struct type *type = check_typedef ($1);
479
480 /* Check if the qualified name is in the global
481 context. However if the symbol has not already
482 been resolved, it's not likely to be found. */
78134374 483 if (type->code () == TYPE_CODE_MODULE)
444c1ed8 484 {
444c1ed8 485 struct block_symbol sym;
b56ccc20
KS
486 const char *type_name = TYPE_SAFE_NAME (type);
487 int type_name_len = strlen (type_name);
5613c585
TT
488 std::string name
489 = string_printf ("%.*s.%.*s",
b56ccc20 490 type_name_len, type_name,
c0fe2ae7 491 $3.length, $3.ptr);
444c1ed8
IB
492
493 sym =
5613c585
TT
494 lookup_symbol (name.c_str (),
495 (const struct block *) NULL,
ccf41c24 496 SEARCH_VFT, NULL);
9412fdcc 497 pstate->push_symbol (name.c_str (), sym);
1b30f421
TT
498 }
499 else
500 {
501 /* Check if the qualified name resolves as a member
502 of an aggregate or an enum type. */
503 if (!type_aggregate_p (type))
504 error (_("`%s' is not defined as an aggregate type."),
505 TYPE_SAFE_NAME (type));
506
9412fdcc
TT
507 pstate->push_new<scope_operation>
508 (type, copy_name ($3));
444c1ed8 509 }
444c1ed8 510 }
3ed9baed 511| DOLLAR_VARIABLE
9412fdcc 512 { pstate->push_dollar ($1); }
3ed9baed
IB
513| NAME_OR_INT
514 { YYSTYPE val;
dda83cd7 515 parse_number (pstate, $1.ptr, $1.length, 0, &val);
9412fdcc
TT
516 pstate->push_new<long_const_operation>
517 (val.typed_val_int.type, val.typed_val_int.val); }
3ed9baed
IB
518| NULL_KEYWORD
519 { struct type *type = parse_d_type (pstate)->builtin_void;
520 type = lookup_pointer_type (type);
9412fdcc 521 pstate->push_new<long_const_operation> (type, 0); }
3ed9baed 522| TRUE_KEYWORD
9412fdcc 523 { pstate->push_new<bool_operation> (true); }
3ed9baed 524| FALSE_KEYWORD
9412fdcc 525 { pstate->push_new<bool_operation> (false); }
3ed9baed 526| INTEGER_LITERAL
9412fdcc 527 { pstate->push_new<long_const_operation> ($1.type, $1.val); }
3ed9baed 528| FLOAT_LITERAL
9412fdcc
TT
529 {
530 float_data data;
531 std::copy (std::begin ($1.val), std::end ($1.val),
532 std::begin (data));
533 pstate->push_new<float_const_operation> ($1.type, data);
534 }
3ed9baed
IB
535| CHARACTER_LITERAL
536 { struct stoken_vector vec;
537 vec.len = 1;
538 vec.tokens = &$1;
9412fdcc 539 pstate->push_c_string (0, &vec); }
3ed9baed
IB
540| StringExp
541 { int i;
9412fdcc 542 pstate->push_c_string (0, &$1);
3ed9baed
IB
543 for (i = 0; i < $1.len; ++i)
544 free ($1.tokens[i].ptr);
545 free ($1.tokens); }
546| ArrayLiteral
9412fdcc
TT
547 {
548 std::vector<operation_up> args
549 = pstate->pop_vector ($1);
550 pstate->push_new<array_operation>
551 (0, $1 - 1, std::move (args));
552 }
d5d8c4e1 553| TYPEOF_KEYWORD '(' Expression ')'
9412fdcc 554 { pstate->wrap<typeof_operation> (); }
3ed9baed
IB
555;
556
557ArrayLiteral:
558 '[' ArgumentList_opt ']'
43476f0b 559 { $$ = pstate->arglist_len; }
3ed9baed
IB
560;
561
562IdentifierExp:
563 IDENTIFIER
3ed9baed
IB
564;
565
566StringExp:
567 STRING_LITERAL
568 { /* We copy the string here, and not in the
569 lexer, to guarantee that we do not leak a
570 string. Note that we follow the
571 NUL-termination convention of the
572 lexer. */
573 struct typed_stoken *vec = XNEW (struct typed_stoken);
574 $$.len = 1;
575 $$.tokens = vec;
576
577 vec->type = $1.type;
578 vec->length = $1.length;
224c3ddb 579 vec->ptr = (char *) malloc ($1.length + 1);
3ed9baed
IB
580 memcpy (vec->ptr, $1.ptr, $1.length + 1);
581 }
582| StringExp STRING_LITERAL
583 { /* Note that we NUL-terminate here, but just
584 for convenience. */
585 char *p;
586 ++$$.len;
224c3ddb
SM
587 $$.tokens
588 = XRESIZEVEC (struct typed_stoken, $$.tokens, $$.len);
3ed9baed 589
224c3ddb 590 p = (char *) malloc ($2.length + 1);
3ed9baed
IB
591 memcpy (p, $2.ptr, $2.length + 1);
592
593 $$.tokens[$$.len - 1].type = $2.type;
594 $$.tokens[$$.len - 1].length = $2.length;
595 $$.tokens[$$.len - 1].ptr = p;
596 }
597;
598
599TypeExp:
444c1ed8
IB
600 '(' TypeExp ')'
601 { /* Do nothing. */ }
602| BasicType
9412fdcc 603 { pstate->push_new<type_operation> ($1); }
3ed9baed 604| BasicType BasicType2
dac43e32 605 { $$ = type_stack->follow_types ($1);
9412fdcc 606 pstate->push_new<type_operation> ($$);
3ed9baed
IB
607 }
608;
609
610BasicType2:
611 '*'
dac43e32 612 { type_stack->push (tp_pointer); }
3ed9baed 613| '*' BasicType2
dac43e32 614 { type_stack->push (tp_pointer); }
3ed9baed 615| '[' INTEGER_LITERAL ']'
8ea36fc9 616 { type_stack->push (tp_array, $2.val); }
3ed9baed 617| '[' INTEGER_LITERAL ']' BasicType2
8ea36fc9 618 { type_stack->push (tp_array, $2.val); }
3ed9baed
IB
619;
620
621BasicType:
622 TYPENAME
623 { $$ = $1.type; }
3ed9baed
IB
624;
625
626%%
627
7f3706eb
IB
628/* Return true if the type is aggregate-like. */
629
630static int
631type_aggregate_p (struct type *type)
632{
78134374
SM
633 return (type->code () == TYPE_CODE_STRUCT
634 || type->code () == TYPE_CODE_UNION
635 || type->code () == TYPE_CODE_MODULE
636 || (type->code () == TYPE_CODE_ENUM
3bc440a2 637 && type->is_declared_class ()));
7f3706eb
IB
638}
639
3ed9baed
IB
640/* Take care of parsing a number (anything that starts with a digit).
641 Set yylval and return the token type; update lexptr.
642 LEN is the number of characters in it. */
643
644/*** Needs some error checking for the float case ***/
645
646static int
647parse_number (struct parser_state *ps, const char *p,
648 int len, int parsed_float, YYSTYPE *putithere)
649{
650 ULONGEST n = 0;
651 ULONGEST prevn = 0;
652 ULONGEST un;
653
654 int i = 0;
655 int c;
656 int base = input_radix;
657 int unsigned_p = 0;
658 int long_p = 0;
659
660 /* We have found a "L" or "U" suffix. */
661 int found_suffix = 0;
662
663 ULONGEST high_bit;
664 struct type *signed_type;
665 struct type *unsigned_type;
666
667 if (parsed_float)
668 {
3ed9baed
IB
669 char *s, *sp;
670
671 /* Strip out all embedded '_' before passing to parse_float. */
672 s = (char *) alloca (len + 1);
673 sp = s;
674 while (len-- > 0)
675 {
676 if (*p != '_')
677 *sp++ = *p;
678 p++;
679 }
680 *sp = '\0';
681 len = strlen (s);
682
edd079d9 683 /* Check suffix for `i' , `fi' or `li' (idouble, ifloat or ireal). */
37194720 684 if (len >= 1 && c_tolower (s[len - 1]) == 'i')
3ed9baed 685 {
37194720 686 if (len >= 2 && c_tolower (s[len - 2]) == 'f')
3ed9baed
IB
687 {
688 putithere->typed_val_float.type
edd079d9
UW
689 = parse_d_type (ps)->builtin_ifloat;
690 len -= 2;
3ed9baed 691 }
37194720 692 else if (len >= 2 && c_tolower (s[len - 2]) == 'l')
3ed9baed
IB
693 {
694 putithere->typed_val_float.type
edd079d9
UW
695 = parse_d_type (ps)->builtin_ireal;
696 len -= 2;
3ed9baed 697 }
edd079d9 698 else
3ed9baed
IB
699 {
700 putithere->typed_val_float.type
701 = parse_d_type (ps)->builtin_idouble;
edd079d9 702 len -= 1;
3ed9baed 703 }
3ed9baed 704 }
edd079d9 705 /* Check suffix for `f' or `l'' (float or real). */
37194720 706 else if (len >= 1 && c_tolower (s[len - 1]) == 'f')
3ed9baed 707 {
edd079d9
UW
708 putithere->typed_val_float.type
709 = parse_d_type (ps)->builtin_float;
710 len -= 1;
711 }
37194720 712 else if (len >= 1 && c_tolower (s[len - 1]) == 'l')
edd079d9
UW
713 {
714 putithere->typed_val_float.type
715 = parse_d_type (ps)->builtin_real;
716 len -= 1;
3ed9baed 717 }
edd079d9 718 /* Default type if no suffix. */
3ed9baed 719 else
edd079d9
UW
720 {
721 putithere->typed_val_float.type
722 = parse_d_type (ps)->builtin_double;
723 }
724
725 if (!parse_float (s, len,
726 putithere->typed_val_float.type,
727 putithere->typed_val_float.val))
3ed9baed
IB
728 return ERROR;
729
730 return FLOAT_LITERAL;
731 }
732
733 /* Handle base-switching prefixes 0x, 0b, 0 */
734 if (p[0] == '0')
735 switch (p[1])
736 {
737 case 'x':
738 case 'X':
739 if (len >= 3)
740 {
741 p += 2;
742 base = 16;
743 len -= 2;
744 }
745 break;
746
747 case 'b':
748 case 'B':
749 if (len >= 3)
750 {
751 p += 2;
752 base = 2;
753 len -= 2;
754 }
755 break;
756
757 default:
758 base = 8;
759 break;
760 }
761
762 while (len-- > 0)
763 {
764 c = *p++;
765 if (c == '_')
766 continue; /* Ignore embedded '_'. */
767 if (c >= 'A' && c <= 'Z')
768 c += 'a' - 'A';
769 if (c != 'l' && c != 'u')
770 n *= base;
771 if (c >= '0' && c <= '9')
772 {
773 if (found_suffix)
774 return ERROR;
775 n += i = c - '0';
776 }
777 else
778 {
779 if (base > 10 && c >= 'a' && c <= 'f')
780 {
781 if (found_suffix)
dda83cd7 782 return ERROR;
3ed9baed
IB
783 n += i = c - 'a' + 10;
784 }
785 else if (c == 'l' && long_p == 0)
786 {
787 long_p = 1;
788 found_suffix = 1;
789 }
790 else if (c == 'u' && unsigned_p == 0)
791 {
792 unsigned_p = 1;
793 found_suffix = 1;
794 }
795 else
796 return ERROR; /* Char not a digit */
797 }
798 if (i >= base)
799 return ERROR; /* Invalid digit in this base. */
800 /* Portably test for integer overflow. */
801 if (c != 'l' && c != 'u')
802 {
803 ULONGEST n2 = prevn * base;
804 if ((n2 / base != prevn) || (n2 + i < prevn))
805 error (_("Numeric constant too large."));
806 }
807 prevn = n;
808 }
809
810 /* An integer constant is an int or a long. An L suffix forces it to
811 be long, and a U suffix forces it to be unsigned. To figure out
812 whether it fits, we shift it right and see whether anything remains.
813 Note that we can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or
814 more in one operation, because many compilers will warn about such a
815 shift (which always produces a zero result). To deal with the case
816 where it is we just always shift the value more than once, with fewer
817 bits each time. */
818 un = (ULONGEST) n >> 2;
819 if (long_p == 0 && (un >> 30) == 0)
820 {
821 high_bit = ((ULONGEST) 1) << 31;
822 signed_type = parse_d_type (ps)->builtin_int;
823 /* For decimal notation, keep the sign of the worked out type. */
824 if (base == 10 && !unsigned_p)
825 unsigned_type = parse_d_type (ps)->builtin_long;
826 else
827 unsigned_type = parse_d_type (ps)->builtin_uint;
828 }
829 else
830 {
831 int shift;
832 if (sizeof (ULONGEST) * HOST_CHAR_BIT < 64)
833 /* A long long does not fit in a LONGEST. */
834 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
835 else
836 shift = 63;
837 high_bit = (ULONGEST) 1 << shift;
838 signed_type = parse_d_type (ps)->builtin_long;
839 unsigned_type = parse_d_type (ps)->builtin_ulong;
840 }
841
842 putithere->typed_val_int.val = n;
843
844 /* If the high bit of the worked out type is set then this number
845 has to be unsigned_type. */
846 if (unsigned_p || (n & high_bit))
847 putithere->typed_val_int.type = unsigned_type;
848 else
849 putithere->typed_val_int.type = signed_type;
850
851 return INTEGER_LITERAL;
852}
853
854/* Temporary obstack used for holding strings. */
855static struct obstack tempbuf;
856static int tempbuf_init;
857
858/* Parse a string or character literal from TOKPTR. The string or
859 character may be wide or unicode. *OUTPTR is set to just after the
860 end of the literal in the input string. The resulting token is
861 stored in VALUE. This returns a token value, either STRING or
862 CHAR, depending on what was parsed. *HOST_CHARS is set to the
863 number of host characters in the literal. */
864
865static int
866parse_string_or_char (const char *tokptr, const char **outptr,
867 struct typed_stoken *value, int *host_chars)
868{
869 int quote;
870
871 /* Build the gdb internal form of the input string in tempbuf. Note
872 that the buffer is null byte terminated *only* for the
873 convenience of debugging gdb itself and printing the buffer
874 contents when the buffer contains no embedded nulls. Gdb does
875 not depend upon the buffer being null byte terminated, it uses
876 the length string instead. This allows gdb to handle C strings
877 (as well as strings in other languages) with embedded null
878 bytes */
879
880 if (!tempbuf_init)
881 tempbuf_init = 1;
882 else
883 obstack_free (&tempbuf, NULL);
884 obstack_init (&tempbuf);
885
886 /* Skip the quote. */
887 quote = *tokptr;
888 ++tokptr;
889
890 *host_chars = 0;
891
892 while (*tokptr)
893 {
894 char c = *tokptr;
895 if (c == '\\')
896 {
897 ++tokptr;
898 *host_chars += c_parse_escape (&tokptr, &tempbuf);
899 }
900 else if (c == quote)
901 break;
902 else
903 {
904 obstack_1grow (&tempbuf, c);
905 ++tokptr;
906 /* FIXME: this does the wrong thing with multi-byte host
907 characters. We could use mbrlen here, but that would
908 make "set host-charset" a bit less useful. */
909 ++*host_chars;
910 }
911 }
912
913 if (*tokptr != quote)
914 {
915 if (quote == '"' || quote == '`')
916 error (_("Unterminated string in expression."));
917 else
918 error (_("Unmatched single quote."));
919 }
920 ++tokptr;
921
922 /* FIXME: should instead use own language string_type enum
923 and handle D-specific string suffixes here. */
924 if (quote == '\'')
925 value->type = C_CHAR;
926 else
927 value->type = C_STRING;
928
79f33898 929 value->ptr = (char *) obstack_base (&tempbuf);
3ed9baed
IB
930 value->length = obstack_object_size (&tempbuf);
931
932 *outptr = tokptr;
933
934 return quote == '\'' ? CHARACTER_LITERAL : STRING_LITERAL;
935}
936
e72b937d 937struct d_token
3ed9baed 938{
a121b7c1 939 const char *oper;
3ed9baed
IB
940 int token;
941 enum exp_opcode opcode;
942};
943
e72b937d 944static const struct d_token tokentab3[] =
3ed9baed
IB
945 {
946 {"^^=", ASSIGN_MODIFY, BINOP_EXP},
947 {"<<=", ASSIGN_MODIFY, BINOP_LSH},
948 {">>=", ASSIGN_MODIFY, BINOP_RSH},
949 };
950
e72b937d 951static const struct d_token tokentab2[] =
3ed9baed
IB
952 {
953 {"+=", ASSIGN_MODIFY, BINOP_ADD},
954 {"-=", ASSIGN_MODIFY, BINOP_SUB},
955 {"*=", ASSIGN_MODIFY, BINOP_MUL},
956 {"/=", ASSIGN_MODIFY, BINOP_DIV},
957 {"%=", ASSIGN_MODIFY, BINOP_REM},
958 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
959 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
960 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
79ab486e
TT
961 {"++", INCREMENT, OP_NULL},
962 {"--", DECREMENT, OP_NULL},
963 {"&&", ANDAND, OP_NULL},
964 {"||", OROR, OP_NULL},
965 {"^^", HATHAT, OP_NULL},
966 {"<<", LSH, OP_NULL},
967 {">>", RSH, OP_NULL},
968 {"==", EQUAL, OP_NULL},
969 {"!=", NOTEQUAL, OP_NULL},
970 {"<=", LEQ, OP_NULL},
971 {">=", GEQ, OP_NULL},
972 {"..", DOTDOT, OP_NULL},
3ed9baed
IB
973 };
974
975/* Identifier-like tokens. */
e72b937d 976static const struct d_token ident_tokens[] =
3ed9baed 977 {
79ab486e
TT
978 {"is", IDENTITY, OP_NULL},
979 {"!is", NOTIDENTITY, OP_NULL},
3ed9baed
IB
980
981 {"cast", CAST_KEYWORD, OP_NULL},
982 {"const", CONST_KEYWORD, OP_NULL},
983 {"immutable", IMMUTABLE_KEYWORD, OP_NULL},
984 {"shared", SHARED_KEYWORD, OP_NULL},
985 {"super", SUPER_KEYWORD, OP_NULL},
986
987 {"null", NULL_KEYWORD, OP_NULL},
988 {"true", TRUE_KEYWORD, OP_NULL},
989 {"false", FALSE_KEYWORD, OP_NULL},
990
991 {"init", INIT_KEYWORD, OP_NULL},
992 {"sizeof", SIZEOF_KEYWORD, OP_NULL},
993 {"typeof", TYPEOF_KEYWORD, OP_NULL},
994 {"typeid", TYPEID_KEYWORD, OP_NULL},
995
996 {"delegate", DELEGATE_KEYWORD, OP_NULL},
997 {"function", FUNCTION_KEYWORD, OP_NULL},
998 {"struct", STRUCT_KEYWORD, OP_NULL},
999 {"union", UNION_KEYWORD, OP_NULL},
1000 {"class", CLASS_KEYWORD, OP_NULL},
1001 {"interface", INTERFACE_KEYWORD, OP_NULL},
1002 {"enum", ENUM_KEYWORD, OP_NULL},
1003 {"template", TEMPLATE_KEYWORD, OP_NULL},
1004 };
1005
3ed9baed
IB
1006/* This is set if a NAME token appeared at the very end of the input
1007 string, with no whitespace separating the name from the EOF. This
1008 is used only when parsing to do field name completion. */
1009static int saw_name_at_eof;
1010
1011/* This is set if the previously-returned token was a structure operator.
1012 This is used only when parsing to do field name completion. */
1013static int last_was_structop;
1014
28aaf3fd
TT
1015/* Depth of parentheses. */
1016static int paren_depth;
1017
3ed9baed
IB
1018/* Read one token, getting characters through lexptr. */
1019
1020static int
444c1ed8 1021lex_one_token (struct parser_state *par_state)
3ed9baed
IB
1022{
1023 int c;
1024 int namelen;
3ed9baed
IB
1025 const char *tokstart;
1026 int saw_structop = last_was_structop;
3ed9baed
IB
1027
1028 last_was_structop = 0;
1029
1030 retry:
1031
5776fca3 1032 pstate->prev_lexptr = pstate->lexptr;
3ed9baed 1033
5776fca3 1034 tokstart = pstate->lexptr;
3ed9baed 1035 /* See if it is a special token of length 3. */
696d6f4d
TT
1036 for (const auto &token : tokentab3)
1037 if (strncmp (tokstart, token.oper, 3) == 0)
3ed9baed 1038 {
5776fca3 1039 pstate->lexptr += 3;
696d6f4d
TT
1040 yylval.opcode = token.opcode;
1041 return token.token;
3ed9baed
IB
1042 }
1043
1044 /* See if it is a special token of length 2. */
696d6f4d
TT
1045 for (const auto &token : tokentab2)
1046 if (strncmp (tokstart, token.oper, 2) == 0)
3ed9baed 1047 {
5776fca3 1048 pstate->lexptr += 2;
696d6f4d
TT
1049 yylval.opcode = token.opcode;
1050 return token.token;
3ed9baed
IB
1051 }
1052
1053 switch (c = *tokstart)
1054 {
1055 case 0:
1056 /* If we're parsing for field name completion, and the previous
1057 token allows such completion, return a COMPLETE token.
1058 Otherwise, we were already scanning the original text, and
1059 we're really done. */
1060 if (saw_name_at_eof)
1061 {
1062 saw_name_at_eof = 0;
1063 return COMPLETE;
1064 }
1065 else if (saw_structop)
1066 return COMPLETE;
1067 else
dda83cd7 1068 return 0;
3ed9baed
IB
1069
1070 case ' ':
1071 case '\t':
1072 case '\n':
5776fca3 1073 pstate->lexptr++;
3ed9baed
IB
1074 goto retry;
1075
1076 case '[':
1077 case '(':
1078 paren_depth++;
5776fca3 1079 pstate->lexptr++;
3ed9baed
IB
1080 return c;
1081
1082 case ']':
1083 case ')':
1084 if (paren_depth == 0)
1085 return 0;
1086 paren_depth--;
5776fca3 1087 pstate->lexptr++;
3ed9baed
IB
1088 return c;
1089
1090 case ',':
8621b685 1091 if (pstate->comma_terminates && paren_depth == 0)
3ed9baed 1092 return 0;
5776fca3 1093 pstate->lexptr++;
3ed9baed
IB
1094 return c;
1095
1096 case '.':
1097 /* Might be a floating point number. */
5776fca3 1098 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
3ed9baed 1099 {
2a612529 1100 if (pstate->parse_completion)
3ed9baed
IB
1101 last_was_structop = 1;
1102 goto symbol; /* Nope, must be a symbol. */
1103 }
d182e398 1104 [[fallthrough]];
3ed9baed
IB
1105
1106 case '0':
1107 case '1':
1108 case '2':
1109 case '3':
1110 case '4':
1111 case '5':
1112 case '6':
1113 case '7':
1114 case '8':
1115 case '9':
1116 {
1117 /* It's a number. */
1118 int got_dot = 0, got_e = 0, toktype;
1119 const char *p = tokstart;
1120 int hex = input_radix > 10;
1121
1122 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1123 {
1124 p += 2;
1125 hex = 1;
1126 }
1127
1128 for (;; ++p)
1129 {
1130 /* Hex exponents start with 'p', because 'e' is a valid hex
1131 digit and thus does not indicate a floating point number
1132 when the radix is hex. */
37194720
TT
1133 if ((!hex && !got_e && c_tolower (p[0]) == 'e')
1134 || (hex && !got_e && c_tolower (p[0] == 'p')))
3ed9baed
IB
1135 got_dot = got_e = 1;
1136 /* A '.' always indicates a decimal floating point number
1137 regardless of the radix. If we have a '..' then its the
1138 end of the number and the beginning of a slice. */
1139 else if (!got_dot && (p[0] == '.' && p[1] != '.'))
1140 got_dot = 1;
1141 /* This is the sign of the exponent, not the end of the number. */
37194720
TT
1142 else if (got_e && (c_tolower (p[-1]) == 'e'
1143 || c_tolower (p[-1]) == 'p')
3ed9baed
IB
1144 && (*p == '-' || *p == '+'))
1145 continue;
1146 /* We will take any letters or digits, ignoring any embedded '_'.
1147 parse_number will complain if past the radix, or if L or U are
1148 not final. */
c0fe2ae7
IB
1149 else if ((*p < '0' || *p > '9') && (*p != '_')
1150 && ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z')))
3ed9baed
IB
1151 break;
1152 }
1153
444c1ed8 1154 toktype = parse_number (par_state, tokstart, p - tokstart,
3ed9baed
IB
1155 got_dot|got_e, &yylval);
1156 if (toktype == ERROR)
e6375bc8
TT
1157 error (_("Invalid number \"%.*s\"."), (int) (p - tokstart),
1158 tokstart);
5776fca3 1159 pstate->lexptr = p;
3ed9baed
IB
1160 return toktype;
1161 }
1162
1163 case '@':
1164 {
1165 const char *p = &tokstart[1];
1166 size_t len = strlen ("entry");
1167
37194720 1168 while (c_isspace (*p))
3ed9baed 1169 p++;
37194720 1170 if (strncmp (p, "entry", len) == 0 && !c_isalnum (p[len])
3ed9baed
IB
1171 && p[len] != '_')
1172 {
5776fca3 1173 pstate->lexptr = &p[len];
3ed9baed
IB
1174 return ENTRY;
1175 }
1176 }
d182e398 1177 [[fallthrough]];
3ed9baed
IB
1178 case '+':
1179 case '-':
1180 case '*':
1181 case '/':
1182 case '%':
1183 case '|':
1184 case '&':
1185 case '^':
1186 case '~':
1187 case '!':
1188 case '<':
1189 case '>':
1190 case '?':
1191 case ':':
1192 case '=':
1193 case '{':
1194 case '}':
1195 symbol:
5776fca3 1196 pstate->lexptr++;
3ed9baed
IB
1197 return c;
1198
1199 case '\'':
1200 case '"':
1201 case '`':
1202 {
1203 int host_len;
5776fca3
TT
1204 int result = parse_string_or_char (tokstart, &pstate->lexptr,
1205 &yylval.tsval, &host_len);
3ed9baed
IB
1206 if (result == CHARACTER_LITERAL)
1207 {
1208 if (host_len == 0)
1209 error (_("Empty character constant."));
1210 else if (host_len > 2 && c == '\'')
1211 {
1212 ++tokstart;
5776fca3 1213 namelen = pstate->lexptr - tokstart - 1;
3ed9baed
IB
1214 goto tryname;
1215 }
1216 else if (host_len > 1)
1217 error (_("Invalid character constant."));
1218 }
1219 return result;
1220 }
1221 }
1222
1223 if (!(c == '_' || c == '$'
1224 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1225 /* We must have come across a bad character (e.g. ';'). */
1226 error (_("Invalid character '%c' in expression"), c);
1227
1228 /* It's a name. See how long it is. */
1229 namelen = 0;
1230 for (c = tokstart[namelen];
1231 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1232 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
1233 c = tokstart[++namelen];
1234
1235 /* The token "if" terminates the expression and is NOT
1236 removed from the input stream. */
1237 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1238 return 0;
1239
1240 /* For the same reason (breakpoint conditions), "thread N"
1241 terminates the expression. "thread" could be an identifier, but
1242 an identifier is never followed by a number without intervening
1243 punctuation. "task" is similar. Handle abbreviations of these,
1244 similarly to breakpoint.c:find_condition_and_thread. */
1245 if (namelen >= 1
1246 && (strncmp (tokstart, "thread", namelen) == 0
1247 || strncmp (tokstart, "task", namelen) == 0)
1248 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
1249 {
1250 const char *p = tokstart + namelen + 1;
1251
1252 while (*p == ' ' || *p == '\t')
dda83cd7 1253 p++;
3ed9baed 1254 if (*p >= '0' && *p <= '9')
dda83cd7 1255 return 0;
3ed9baed
IB
1256 }
1257
5776fca3 1258 pstate->lexptr += namelen;
3ed9baed
IB
1259
1260 tryname:
1261
1262 yylval.sval.ptr = tokstart;
1263 yylval.sval.length = namelen;
1264
1265 /* Catch specific keywords. */
61f4b350 1266 std::string copy = copy_name (yylval.sval);
696d6f4d
TT
1267 for (const auto &token : ident_tokens)
1268 if (copy == token.oper)
3ed9baed
IB
1269 {
1270 /* It is ok to always set this, even though we don't always
1271 strictly need to. */
696d6f4d
TT
1272 yylval.opcode = token.opcode;
1273 return token.token;
3ed9baed
IB
1274 }
1275
1276 if (*tokstart == '$')
1277 return DOLLAR_VARIABLE;
1278
1279 yylval.tsym.type
73923d7e 1280 = language_lookup_primitive_type (par_state->language (),
61f4b350 1281 par_state->gdbarch (), copy.c_str ());
3ed9baed
IB
1282 if (yylval.tsym.type != NULL)
1283 return TYPENAME;
1284
1285 /* Input names that aren't symbols but ARE valid hex numbers,
1286 when the input radix permits them, can be names or numbers
1287 depending on the parse. Note we support radixes > 16 here. */
1288 if ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1289 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))
1290 {
1291 YYSTYPE newlval; /* Its value is ignored. */
444c1ed8 1292 int hextype = parse_number (par_state, tokstart, namelen, 0, &newlval);
3ed9baed
IB
1293 if (hextype == INTEGER_LITERAL)
1294 return NAME_OR_INT;
1295 }
1296
2a612529 1297 if (pstate->parse_completion && *pstate->lexptr == '\0')
3ed9baed
IB
1298 saw_name_at_eof = 1;
1299
1300 return IDENTIFIER;
1301}
1302
444c1ed8 1303/* An object of this type is pushed on a FIFO by the "outer" lexer. */
9972aac2 1304struct d_token_and_value
444c1ed8
IB
1305{
1306 int token;
1307 YYSTYPE value;
5fe3f3e4 1308};
444c1ed8 1309
444c1ed8
IB
1310
1311/* A FIFO of tokens that have been read but not yet returned to the
1312 parser. */
9972aac2 1313static std::vector<d_token_and_value> token_fifo;
444c1ed8
IB
1314
1315/* Non-zero if the lexer should return tokens from the FIFO. */
1316static int popping;
1317
1318/* Temporary storage for yylex; this holds symbol names as they are
1319 built up. */
8268c778 1320static auto_obstack name_obstack;
444c1ed8
IB
1321
1322/* Classify an IDENTIFIER token. The contents of the token are in `yylval'.
1323 Updates yylval and returns the new token type. BLOCK is the block
1324 in which lookups start; this can be NULL to mean the global scope. */
1325
1326static int
1327classify_name (struct parser_state *par_state, const struct block *block)
1328{
1329 struct block_symbol sym;
444c1ed8
IB
1330 struct field_of_this_result is_a_field_of_this;
1331
61f4b350 1332 std::string copy = copy_name (yylval.sval);
444c1ed8 1333
ccf41c24 1334 sym = lookup_symbol (copy.c_str (), block, SEARCH_VFT, &is_a_field_of_this);
4260abb7 1335 if (sym.symbol && sym.symbol->loc_class () == LOC_TYPEDEF)
444c1ed8 1336 {
5f9c5a63 1337 yylval.tsym.type = sym.symbol->type ();
444c1ed8
IB
1338 return TYPENAME;
1339 }
1340 else if (sym.symbol == NULL)
1341 {
1342 /* Look-up first for a module name, then a type. */
ccf41c24
TT
1343 sym = lookup_symbol (copy.c_str (), block, SEARCH_MODULE_DOMAIN,
1344 nullptr);
444c1ed8 1345 if (sym.symbol == NULL)
ccf41c24
TT
1346 sym = lookup_symbol (copy.c_str (), block, SEARCH_STRUCT_DOMAIN,
1347 nullptr);
444c1ed8
IB
1348
1349 if (sym.symbol != NULL)
1350 {
5f9c5a63 1351 yylval.tsym.type = sym.symbol->type ();
444c1ed8
IB
1352 return TYPENAME;
1353 }
1354
1355 return UNKNOWN_NAME;
1356 }
1357
1358 return IDENTIFIER;
1359}
1360
1361/* Like classify_name, but used by the inner loop of the lexer, when a
1362 name might have already been seen. CONTEXT is the context type, or
1363 NULL if this is the first component of a name. */
1364
1365static int
1366classify_inner_name (struct parser_state *par_state,
1367 const struct block *block, struct type *context)
1368{
1369 struct type *type;
444c1ed8
IB
1370
1371 if (context == NULL)
1372 return classify_name (par_state, block);
1373
1374 type = check_typedef (context);
7f3706eb
IB
1375 if (!type_aggregate_p (type))
1376 return ERROR;
444c1ed8 1377
61f4b350
TT
1378 std::string copy = copy_name (yylval.ssym.stoken);
1379 yylval.ssym.sym = d_lookup_nested_symbol (type, copy.c_str (), block);
444c1ed8
IB
1380
1381 if (yylval.ssym.sym.symbol == NULL)
1382 return ERROR;
1383
4260abb7 1384 if (yylval.ssym.sym.symbol->loc_class () == LOC_TYPEDEF)
444c1ed8 1385 {
5f9c5a63 1386 yylval.tsym.type = yylval.ssym.sym.symbol->type ();
444c1ed8
IB
1387 return TYPENAME;
1388 }
1389
1390 return IDENTIFIER;
1391}
1392
1393/* The outer level of a two-level lexer. This calls the inner lexer
1394 to return tokens. It then either returns these tokens, or
1395 aggregates them into a larger token. This lets us work around a
1396 problem in our parsing approach, where the parser could not
1397 distinguish between qualified names and qualified types at the
1398 right point. */
1399
1400static int
1401yylex (void)
1402{
9972aac2 1403 d_token_and_value current;
444c1ed8
IB
1404 int last_was_dot;
1405 struct type *context_type = NULL;
1406 int last_to_examine, next_to_examine, checkpoint;
1407 const struct block *search_block;
1408
5fe3f3e4 1409 if (popping && !token_fifo.empty ())
444c1ed8
IB
1410 goto do_pop;
1411 popping = 0;
1412
1413 /* Read the first token and decide what to do. */
1414 current.token = lex_one_token (pstate);
1415 if (current.token != IDENTIFIER && current.token != '.')
1416 return current.token;
1417
1418 /* Read any sequence of alternating "." and identifier tokens into
1419 the token FIFO. */
1420 current.value = yylval;
5fe3f3e4 1421 token_fifo.push_back (current);
444c1ed8
IB
1422 last_was_dot = current.token == '.';
1423
1424 while (1)
1425 {
1426 current.token = lex_one_token (pstate);
1427 current.value = yylval;
5fe3f3e4 1428 token_fifo.push_back (current);
444c1ed8
IB
1429
1430 if ((last_was_dot && current.token != IDENTIFIER)
1431 || (!last_was_dot && current.token != '.'))
1432 break;
1433
1434 last_was_dot = !last_was_dot;
1435 }
1436 popping = 1;
1437
1438 /* We always read one extra token, so compute the number of tokens
1439 to examine accordingly. */
5fe3f3e4 1440 last_to_examine = token_fifo.size () - 2;
444c1ed8
IB
1441 next_to_examine = 0;
1442
5fe3f3e4 1443 current = token_fifo[next_to_examine];
444c1ed8
IB
1444 ++next_to_examine;
1445
1446 /* If we are not dealing with a typename, now is the time to find out. */
1447 if (current.token == IDENTIFIER)
1448 {
1449 yylval = current.value;
1e58a4a4 1450 current.token = classify_name (pstate, pstate->expression_context_block);
444c1ed8
IB
1451 current.value = yylval;
1452 }
1453
1454 /* If the IDENTIFIER is not known, it could be a package symbol,
1455 first try building up a name until we find the qualified module. */
1456 if (current.token == UNKNOWN_NAME)
1457 {
8268c778 1458 name_obstack.clear ();
444c1ed8
IB
1459 obstack_grow (&name_obstack, current.value.sval.ptr,
1460 current.value.sval.length);
1461
1462 last_was_dot = 0;
1463
1464 while (next_to_examine <= last_to_examine)
1465 {
9972aac2 1466 d_token_and_value next;
444c1ed8 1467
5fe3f3e4 1468 next = token_fifo[next_to_examine];
444c1ed8
IB
1469 ++next_to_examine;
1470
5fe3f3e4 1471 if (next.token == IDENTIFIER && last_was_dot)
444c1ed8
IB
1472 {
1473 /* Update the partial name we are constructing. */
dda83cd7 1474 obstack_grow_str (&name_obstack, ".");
5fe3f3e4
TT
1475 obstack_grow (&name_obstack, next.value.sval.ptr,
1476 next.value.sval.length);
444c1ed8 1477
79f33898 1478 yylval.sval.ptr = (char *) obstack_base (&name_obstack);
444c1ed8
IB
1479 yylval.sval.length = obstack_object_size (&name_obstack);
1480
1e58a4a4
TT
1481 current.token = classify_name (pstate,
1482 pstate->expression_context_block);
444c1ed8
IB
1483 current.value = yylval;
1484
1485 /* We keep going until we find a TYPENAME. */
1486 if (current.token == TYPENAME)
1487 {
1488 /* Install it as the first token in the FIFO. */
5fe3f3e4
TT
1489 token_fifo[0] = current;
1490 token_fifo.erase (token_fifo.begin () + 1,
1491 token_fifo.begin () + next_to_examine);
444c1ed8
IB
1492 break;
1493 }
1494 }
5fe3f3e4 1495 else if (next.token == '.' && !last_was_dot)
444c1ed8
IB
1496 last_was_dot = 1;
1497 else
1498 {
1499 /* We've reached the end of the name. */
1500 break;
1501 }
1502 }
1503
1504 /* Reset our current token back to the start, if we found nothing
1505 this means that we will just jump to do pop. */
5fe3f3e4 1506 current = token_fifo[0];
444c1ed8
IB
1507 next_to_examine = 1;
1508 }
1509 if (current.token != TYPENAME && current.token != '.')
1510 goto do_pop;
1511
8268c778 1512 name_obstack.clear ();
444c1ed8
IB
1513 checkpoint = 0;
1514 if (current.token == '.')
1515 search_block = NULL;
1516 else
1517 {
1518 gdb_assert (current.token == TYPENAME);
1e58a4a4 1519 search_block = pstate->expression_context_block;
444c1ed8
IB
1520 obstack_grow (&name_obstack, current.value.sval.ptr,
1521 current.value.sval.length);
1522 context_type = current.value.tsym.type;
1523 checkpoint = 1;
1524 }
1525
1526 last_was_dot = current.token == '.';
1527
1528 while (next_to_examine <= last_to_examine)
1529 {
9972aac2 1530 d_token_and_value next;
444c1ed8 1531
5fe3f3e4 1532 next = token_fifo[next_to_examine];
444c1ed8
IB
1533 ++next_to_examine;
1534
5fe3f3e4 1535 if (next.token == IDENTIFIER && last_was_dot)
444c1ed8
IB
1536 {
1537 int classification;
1538
5fe3f3e4 1539 yylval = next.value;
444c1ed8
IB
1540 classification = classify_inner_name (pstate, search_block,
1541 context_type);
1542 /* We keep going until we either run out of names, or until
1543 we have a qualified name which is not a type. */
1544 if (classification != TYPENAME && classification != IDENTIFIER)
1545 break;
1546
1547 /* Accept up to this token. */
1548 checkpoint = next_to_examine;
1549
1550 /* Update the partial name we are constructing. */
1551 if (context_type != NULL)
1552 {
1553 /* We don't want to put a leading "." into the name. */
dda83cd7 1554 obstack_grow_str (&name_obstack, ".");
444c1ed8 1555 }
5fe3f3e4
TT
1556 obstack_grow (&name_obstack, next.value.sval.ptr,
1557 next.value.sval.length);
444c1ed8 1558
79f33898 1559 yylval.sval.ptr = (char *) obstack_base (&name_obstack);
444c1ed8
IB
1560 yylval.sval.length = obstack_object_size (&name_obstack);
1561 current.value = yylval;
1562 current.token = classification;
1563
1564 last_was_dot = 0;
1565
1566 if (classification == IDENTIFIER)
1567 break;
1568
1569 context_type = yylval.tsym.type;
1570 }
5fe3f3e4 1571 else if (next.token == '.' && !last_was_dot)
444c1ed8
IB
1572 last_was_dot = 1;
1573 else
1574 {
1575 /* We've reached the end of the name. */
1576 break;
1577 }
1578 }
1579
1580 /* If we have a replacement token, install it as the first token in
1581 the FIFO, and delete the other constituent tokens. */
1582 if (checkpoint > 0)
1583 {
5fe3f3e4 1584 token_fifo[0] = current;
444c1ed8 1585 if (checkpoint > 1)
5fe3f3e4
TT
1586 token_fifo.erase (token_fifo.begin () + 1,
1587 token_fifo.begin () + checkpoint);
444c1ed8
IB
1588 }
1589
1590 do_pop:
5fe3f3e4
TT
1591 current = token_fifo[0];
1592 token_fifo.erase (token_fifo.begin ());
444c1ed8
IB
1593 yylval = current.value;
1594 return current.token;
1595}
1596
3ed9baed
IB
1597int
1598d_parse (struct parser_state *par_state)
1599{
3ed9baed 1600 /* Setting up the parser state. */
eae49211 1601 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3ed9baed
IB
1602 gdb_assert (par_state != NULL);
1603 pstate = par_state;
1604
156d9eab 1605 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
e360af5a 1606 par_state->debug);
3ed9baed 1607
dac43e32
TT
1608 struct type_stack stack;
1609 scoped_restore restore_type_stack = make_scoped_restore (&type_stack,
1610 &stack);
1611
3ed9baed
IB
1612 /* Initialize some state used by the lexer. */
1613 last_was_structop = 0;
1614 saw_name_at_eof = 0;
28aaf3fd 1615 paren_depth = 0;
3ed9baed 1616
5fe3f3e4 1617 token_fifo.clear ();
444c1ed8 1618 popping = 0;
8268c778 1619 name_obstack.clear ();
444c1ed8 1620
9412fdcc
TT
1621 int result = yyparse ();
1622 if (!result)
1623 pstate->set_operation (pstate->pop ());
1624 return result;
3ed9baed
IB
1625}
1626
69d340c6 1627static void
a121b7c1 1628yyerror (const char *msg)
3ed9baed 1629{
e89496f4 1630 pstate->parse_error (msg);
3ed9baed
IB
1631}
1632