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