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