]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/d-exp.y
Update copyright year range in all GDB files.
[thirdparty/binutils-gdb.git] / gdb / d-exp.y
CommitLineData
3ed9baed
IB
1/* YACC parser for D expressions, for GDB.
2
42a4f53d 3 Copyright (C) 2014-2019 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"
54
55#define parse_type(ps) builtin_type (parse_gdbarch (ps))
56#define parse_d_type(ps) builtin_d_type (parse_gdbarch (ps))
57
b3f11165
PA
58/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
59 etc). */
60#define GDB_YY_REMAP_PREFIX d_
61#include "yy-remap.h"
3ed9baed
IB
62
63/* The state of the parser, used internally when we are parsing the
64 expression. */
65
66static struct parser_state *pstate = NULL;
67
68int yyparse (void);
69
70static int yylex (void);
71
69d340c6 72static void yyerror (const char *);
3ed9baed 73
7f3706eb
IB
74static int type_aggregate_p (struct type *);
75
3ed9baed
IB
76%}
77
78/* Although the yacc "value" of an expression is not used,
79 since the result is stored in the structure being created,
80 other node types do have values. */
81
82%union
83 {
84 struct {
85 LONGEST val;
86 struct type *type;
87 } typed_val_int;
88 struct {
edd079d9 89 gdb_byte val[16];
3ed9baed
IB
90 struct type *type;
91 } typed_val_float;
92 struct symbol *sym;
93 struct type *tval;
94 struct typed_stoken tsval;
95 struct stoken sval;
96 struct ttype tsym;
97 struct symtoken ssym;
98 int ival;
444c1ed8 99 int voidval;
3ed9baed
IB
100 struct block *bval;
101 enum exp_opcode opcode;
102 struct stoken_vector svec;
103 }
104
105%{
106/* YYSTYPE gets defined by %union */
107static int parse_number (struct parser_state *, const char *,
108 int, int, YYSTYPE *);
3ed9baed
IB
109%}
110
444c1ed8 111%token <sval> IDENTIFIER UNKNOWN_NAME
3ed9baed
IB
112%token <tsym> TYPENAME
113%token <voidval> COMPLETE
114
115/* A NAME_OR_INT is a symbol which is not known in the symbol table,
116 but which would parse as a valid number in the current input radix.
117 E.g. "c" when input_radix==16. Depending on the parse, it will be
118 turned into a name or into a number. */
119
120%token <sval> NAME_OR_INT
121
122%token <typed_val_int> INTEGER_LITERAL
123%token <typed_val_float> FLOAT_LITERAL
124%token <tsval> CHARACTER_LITERAL
125%token <tsval> STRING_LITERAL
126
127%type <svec> StringExp
128%type <tval> BasicType TypeExp
129%type <sval> IdentifierExp
130%type <ival> ArrayLiteral
131
132%token ENTRY
133%token ERROR
134
135/* Keywords that have a constant value. */
136%token TRUE_KEYWORD FALSE_KEYWORD NULL_KEYWORD
137/* Class 'super' accessor. */
138%token SUPER_KEYWORD
139/* Properties. */
140%token CAST_KEYWORD SIZEOF_KEYWORD
141%token TYPEOF_KEYWORD TYPEID_KEYWORD
142%token INIT_KEYWORD
143/* Comparison keywords. */
144/* Type storage classes. */
145%token IMMUTABLE_KEYWORD CONST_KEYWORD SHARED_KEYWORD
146/* Non-scalar type keywords. */
147%token STRUCT_KEYWORD UNION_KEYWORD
148%token CLASS_KEYWORD INTERFACE_KEYWORD
149%token ENUM_KEYWORD TEMPLATE_KEYWORD
150%token DELEGATE_KEYWORD FUNCTION_KEYWORD
151
152%token <sval> DOLLAR_VARIABLE
153
154%token <opcode> ASSIGN_MODIFY
155
156%left ','
157%right '=' ASSIGN_MODIFY
158%right '?'
159%left OROR
160%left ANDAND
161%left '|'
162%left '^'
163%left '&'
164%left EQUAL NOTEQUAL '<' '>' LEQ GEQ
165%right LSH RSH
166%left '+' '-'
167%left '*' '/' '%'
168%right HATHAT
169%left IDENTITY NOTIDENTITY
170%right INCREMENT DECREMENT
171%right '.' '[' '('
172%token DOTDOT
173
174\f
175%%
176
177start :
178 Expression
179| TypeExp
180;
181
182/* Expressions, including the comma operator. */
183
184Expression:
185 CommaExpression
186;
187
188CommaExpression:
189 AssignExpression
190| AssignExpression ',' CommaExpression
191 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
192;
193
194AssignExpression:
195 ConditionalExpression
196| ConditionalExpression '=' AssignExpression
197 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
198| ConditionalExpression ASSIGN_MODIFY AssignExpression
199 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
200 write_exp_elt_opcode (pstate, $2);
201 write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
202;
203
204ConditionalExpression:
205 OrOrExpression
206| OrOrExpression '?' Expression ':' ConditionalExpression
207 { write_exp_elt_opcode (pstate, TERNOP_COND); }
208;
209
210OrOrExpression:
211 AndAndExpression
212| OrOrExpression OROR AndAndExpression
213 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
214;
215
216AndAndExpression:
217 OrExpression
218| AndAndExpression ANDAND OrExpression
219 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
220;
221
222OrExpression:
223 XorExpression
224| OrExpression '|' XorExpression
225 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
226;
227
228XorExpression:
229 AndExpression
230| XorExpression '^' AndExpression
231 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
232;
233
234AndExpression:
235 CmpExpression
236| AndExpression '&' CmpExpression
237 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
238;
239
240CmpExpression:
241 ShiftExpression
242| EqualExpression
243| IdentityExpression
244| RelExpression
245;
246
247EqualExpression:
248 ShiftExpression EQUAL ShiftExpression
249 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
250| ShiftExpression NOTEQUAL ShiftExpression
251 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
252;
253
254IdentityExpression:
255 ShiftExpression IDENTITY ShiftExpression
256 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
257| ShiftExpression NOTIDENTITY ShiftExpression
258 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
259;
260
261RelExpression:
262 ShiftExpression '<' ShiftExpression
263 { write_exp_elt_opcode (pstate, BINOP_LESS); }
264| ShiftExpression LEQ ShiftExpression
265 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
266| ShiftExpression '>' ShiftExpression
267 { write_exp_elt_opcode (pstate, BINOP_GTR); }
268| ShiftExpression GEQ ShiftExpression
269 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
270;
271
272ShiftExpression:
273 AddExpression
274| ShiftExpression LSH AddExpression
275 { write_exp_elt_opcode (pstate, BINOP_LSH); }
276| ShiftExpression RSH AddExpression
277 { write_exp_elt_opcode (pstate, BINOP_RSH); }
278;
279
280AddExpression:
281 MulExpression
282| AddExpression '+' MulExpression
283 { write_exp_elt_opcode (pstate, BINOP_ADD); }
284| AddExpression '-' MulExpression
285 { write_exp_elt_opcode (pstate, BINOP_SUB); }
286| AddExpression '~' MulExpression
287 { write_exp_elt_opcode (pstate, BINOP_CONCAT); }
288;
289
290MulExpression:
291 UnaryExpression
292| MulExpression '*' UnaryExpression
293 { write_exp_elt_opcode (pstate, BINOP_MUL); }
294| MulExpression '/' UnaryExpression
295 { write_exp_elt_opcode (pstate, BINOP_DIV); }
296| MulExpression '%' UnaryExpression
297 { write_exp_elt_opcode (pstate, BINOP_REM); }
298
299UnaryExpression:
300 '&' UnaryExpression
301 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
302| INCREMENT UnaryExpression
303 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
304| DECREMENT UnaryExpression
305 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
306| '*' UnaryExpression
307 { write_exp_elt_opcode (pstate, UNOP_IND); }
308| '-' UnaryExpression
309 { write_exp_elt_opcode (pstate, UNOP_NEG); }
310| '+' UnaryExpression
311 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
312| '!' UnaryExpression
313 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
314| '~' UnaryExpression
315 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
d5d8c4e1
IB
316| TypeExp '.' SIZEOF_KEYWORD
317 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
3ed9baed
IB
318| CastExpression
319| PowExpression
320;
321
322CastExpression:
323 CAST_KEYWORD '(' TypeExp ')' UnaryExpression
f5e6296e 324 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
3ed9baed
IB
325 /* C style cast is illegal D, but is still recognised in
326 the grammar, so we keep this around for convenience. */
327| '(' TypeExp ')' UnaryExpression
f5e6296e
IB
328 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
329
3ed9baed
IB
330;
331
332PowExpression:
333 PostfixExpression
334| PostfixExpression HATHAT UnaryExpression
335 { write_exp_elt_opcode (pstate, BINOP_EXP); }
336;
337
338PostfixExpression:
339 PrimaryExpression
444c1ed8
IB
340| PostfixExpression '.' COMPLETE
341 { struct stoken s;
342 mark_struct_expression (pstate);
343 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
344 s.ptr = "";
345 s.length = 0;
346 write_exp_string (pstate, s);
347 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
348| PostfixExpression '.' IDENTIFIER
349 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
350 write_exp_string (pstate, $3);
351 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
352| PostfixExpression '.' IDENTIFIER COMPLETE
353 { mark_struct_expression (pstate);
354 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
355 write_exp_string (pstate, $3);
356 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
d5d8c4e1
IB
357| PostfixExpression '.' SIZEOF_KEYWORD
358 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
3ed9baed
IB
359| PostfixExpression INCREMENT
360 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
361| PostfixExpression DECREMENT
362 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
363| CallExpression
364| IndexExpression
365| SliceExpression
366;
367
368ArgumentList:
369 AssignExpression
370 { arglist_len = 1; }
371| ArgumentList ',' AssignExpression
372 { arglist_len++; }
373;
374
375ArgumentList_opt:
376 /* EMPTY */
377 { arglist_len = 0; }
378| ArgumentList
379;
380
381CallExpression:
382 PostfixExpression '('
383 { start_arglist (); }
384 ArgumentList_opt ')'
385 { write_exp_elt_opcode (pstate, OP_FUNCALL);
386 write_exp_elt_longcst (pstate, (LONGEST) end_arglist ());
387 write_exp_elt_opcode (pstate, OP_FUNCALL); }
388;
389
390IndexExpression:
391 PostfixExpression '[' ArgumentList ']'
392 { if (arglist_len > 0)
393 {
394 write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
395 write_exp_elt_longcst (pstate, (LONGEST) arglist_len);
396 write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
397 }
398 else
399 write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT);
400 }
401;
402
403SliceExpression:
404 PostfixExpression '[' ']'
405 { /* Do nothing. */ }
406| PostfixExpression '[' AssignExpression DOTDOT AssignExpression ']'
407 { write_exp_elt_opcode (pstate, TERNOP_SLICE); }
408;
409
410PrimaryExpression:
411 '(' Expression ')'
412 { /* Do nothing. */ }
413| IdentifierExp
444c1ed8
IB
414 { struct bound_minimal_symbol msymbol;
415 char *copy = copy_name ($1);
416 struct field_of_this_result is_a_field_of_this;
417 struct block_symbol sym;
418
419 /* Handle VAR, which could be local or global. */
420 sym = lookup_symbol (copy, expression_context_block, VAR_DOMAIN,
421 &is_a_field_of_this);
422 if (sym.symbol && SYMBOL_CLASS (sym.symbol) != LOC_TYPEDEF)
423 {
424 if (symbol_read_needs_frame (sym.symbol))
aee1fcdf 425 innermost_block.update (sym);
444c1ed8 426 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
2d5a88dc 427 write_exp_elt_block (pstate, sym.block);
444c1ed8
IB
428 write_exp_elt_sym (pstate, sym.symbol);
429 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
430 }
431 else if (is_a_field_of_this.type != NULL)
432 {
433 /* It hangs off of `this'. Must not inadvertently convert from a
434 method call to data ref. */
aee1fcdf 435 innermost_block.update (sym);
444c1ed8
IB
436 write_exp_elt_opcode (pstate, OP_THIS);
437 write_exp_elt_opcode (pstate, OP_THIS);
438 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
439 write_exp_string (pstate, $1);
440 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
441 }
442 else
443 {
444 /* Lookup foreign name in global static symbols. */
445 msymbol = lookup_bound_minimal_symbol (copy);
446 if (msymbol.minsym != NULL)
447 write_exp_msymbol (pstate, msymbol);
448 else if (!have_full_symbols () && !have_partial_symbols ())
449 error (_("No symbol table is loaded. Use the \"file\" command"));
450 else
451 error (_("No symbol \"%s\" in current context."), copy);
452 }
453 }
454| TypeExp '.' IdentifierExp
455 { struct type *type = check_typedef ($1);
456
457 /* Check if the qualified name is in the global
458 context. However if the symbol has not already
459 been resolved, it's not likely to be found. */
460 if (TYPE_CODE (type) == TYPE_CODE_MODULE)
461 {
462 struct bound_minimal_symbol msymbol;
463 struct block_symbol sym;
b56ccc20
KS
464 const char *type_name = TYPE_SAFE_NAME (type);
465 int type_name_len = strlen (type_name);
5613c585
TT
466 std::string name
467 = string_printf ("%.*s.%.*s",
b56ccc20 468 type_name_len, type_name,
c0fe2ae7 469 $3.length, $3.ptr);
444c1ed8
IB
470
471 sym =
5613c585
TT
472 lookup_symbol (name.c_str (),
473 (const struct block *) NULL,
444c1ed8
IB
474 VAR_DOMAIN, NULL);
475 if (sym.symbol)
476 {
477 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
478 write_exp_elt_block (pstate, sym.block);
479 write_exp_elt_sym (pstate, sym.symbol);
480 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
481 break;
482 }
483
5613c585 484 msymbol = lookup_bound_minimal_symbol (name.c_str ());
444c1ed8
IB
485 if (msymbol.minsym != NULL)
486 write_exp_msymbol (pstate, msymbol);
487 else if (!have_full_symbols () && !have_partial_symbols ())
488 error (_("No symbol table is loaded. Use the \"file\" command."));
489 else
5613c585
TT
490 error (_("No symbol \"%s\" in current context."),
491 name.c_str ());
444c1ed8
IB
492 }
493
494 /* Check if the qualified name resolves as a member
495 of an aggregate or an enum type. */
7f3706eb 496 if (!type_aggregate_p (type))
444c1ed8
IB
497 error (_("`%s' is not defined as an aggregate type."),
498 TYPE_SAFE_NAME (type));
499
500 write_exp_elt_opcode (pstate, OP_SCOPE);
501 write_exp_elt_type (pstate, type);
502 write_exp_string (pstate, $3);
503 write_exp_elt_opcode (pstate, OP_SCOPE);
504 }
3ed9baed
IB
505| DOLLAR_VARIABLE
506 { write_dollar_variable (pstate, $1); }
507| NAME_OR_INT
508 { YYSTYPE val;
509 parse_number (pstate, $1.ptr, $1.length, 0, &val);
510 write_exp_elt_opcode (pstate, OP_LONG);
511 write_exp_elt_type (pstate, val.typed_val_int.type);
512 write_exp_elt_longcst (pstate,
513 (LONGEST) val.typed_val_int.val);
514 write_exp_elt_opcode (pstate, OP_LONG); }
515| NULL_KEYWORD
516 { struct type *type = parse_d_type (pstate)->builtin_void;
517 type = lookup_pointer_type (type);
518 write_exp_elt_opcode (pstate, OP_LONG);
519 write_exp_elt_type (pstate, type);
520 write_exp_elt_longcst (pstate, (LONGEST) 0);
521 write_exp_elt_opcode (pstate, OP_LONG); }
522| TRUE_KEYWORD
523 { write_exp_elt_opcode (pstate, OP_BOOL);
524 write_exp_elt_longcst (pstate, (LONGEST) 1);
525 write_exp_elt_opcode (pstate, OP_BOOL); }
526| FALSE_KEYWORD
527 { write_exp_elt_opcode (pstate, OP_BOOL);
528 write_exp_elt_longcst (pstate, (LONGEST) 0);
529 write_exp_elt_opcode (pstate, OP_BOOL); }
530| INTEGER_LITERAL
531 { write_exp_elt_opcode (pstate, OP_LONG);
532 write_exp_elt_type (pstate, $1.type);
533 write_exp_elt_longcst (pstate, (LONGEST)($1.val));
534 write_exp_elt_opcode (pstate, OP_LONG); }
535| FLOAT_LITERAL
edd079d9 536 { write_exp_elt_opcode (pstate, OP_FLOAT);
3ed9baed 537 write_exp_elt_type (pstate, $1.type);
edd079d9
UW
538 write_exp_elt_floatcst (pstate, $1.val);
539 write_exp_elt_opcode (pstate, OP_FLOAT); }
3ed9baed
IB
540| CHARACTER_LITERAL
541 { struct stoken_vector vec;
542 vec.len = 1;
543 vec.tokens = &$1;
544 write_exp_string_vector (pstate, $1.type, &vec); }
545| StringExp
546 { int i;
547 write_exp_string_vector (pstate, 0, &$1);
548 for (i = 0; i < $1.len; ++i)
549 free ($1.tokens[i].ptr);
550 free ($1.tokens); }
551| ArrayLiteral
552 { write_exp_elt_opcode (pstate, OP_ARRAY);
553 write_exp_elt_longcst (pstate, (LONGEST) 0);
554 write_exp_elt_longcst (pstate, (LONGEST) $1 - 1);
555 write_exp_elt_opcode (pstate, OP_ARRAY); }
d5d8c4e1
IB
556| TYPEOF_KEYWORD '(' Expression ')'
557 { write_exp_elt_opcode (pstate, OP_TYPEOF); }
3ed9baed
IB
558;
559
560ArrayLiteral:
561 '[' ArgumentList_opt ']'
562 { $$ = arglist_len; }
563;
564
565IdentifierExp:
566 IDENTIFIER
3ed9baed
IB
567;
568
569StringExp:
570 STRING_LITERAL
571 { /* We copy the string here, and not in the
572 lexer, to guarantee that we do not leak a
573 string. Note that we follow the
574 NUL-termination convention of the
575 lexer. */
576 struct typed_stoken *vec = XNEW (struct typed_stoken);
577 $$.len = 1;
578 $$.tokens = vec;
579
580 vec->type = $1.type;
581 vec->length = $1.length;
224c3ddb 582 vec->ptr = (char *) malloc ($1.length + 1);
3ed9baed
IB
583 memcpy (vec->ptr, $1.ptr, $1.length + 1);
584 }
585| StringExp STRING_LITERAL
586 { /* Note that we NUL-terminate here, but just
587 for convenience. */
588 char *p;
589 ++$$.len;
224c3ddb
SM
590 $$.tokens
591 = XRESIZEVEC (struct typed_stoken, $$.tokens, $$.len);
3ed9baed 592
224c3ddb 593 p = (char *) malloc ($2.length + 1);
3ed9baed
IB
594 memcpy (p, $2.ptr, $2.length + 1);
595
596 $$.tokens[$$.len - 1].type = $2.type;
597 $$.tokens[$$.len - 1].length = $2.length;
598 $$.tokens[$$.len - 1].ptr = p;
599 }
600;
601
602TypeExp:
444c1ed8
IB
603 '(' TypeExp ')'
604 { /* Do nothing. */ }
605| BasicType
3ed9baed
IB
606 { write_exp_elt_opcode (pstate, OP_TYPE);
607 write_exp_elt_type (pstate, $1);
608 write_exp_elt_opcode (pstate, OP_TYPE); }
609| BasicType BasicType2
610 { $$ = follow_types ($1);
611 write_exp_elt_opcode (pstate, OP_TYPE);
612 write_exp_elt_type (pstate, $$);
613 write_exp_elt_opcode (pstate, OP_TYPE);
614 }
615;
616
617BasicType2:
618 '*'
619 { push_type (tp_pointer); }
620| '*' BasicType2
621 { push_type (tp_pointer); }
622| '[' INTEGER_LITERAL ']'
623 { push_type_int ($2.val);
624 push_type (tp_array); }
625| '[' INTEGER_LITERAL ']' BasicType2
626 { push_type_int ($2.val);
627 push_type (tp_array); }
628;
629
630BasicType:
631 TYPENAME
632 { $$ = $1.type; }
3ed9baed
IB
633;
634
635%%
636
7f3706eb
IB
637/* Return true if the type is aggregate-like. */
638
639static int
640type_aggregate_p (struct type *type)
641{
642 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
643 || TYPE_CODE (type) == TYPE_CODE_UNION
662659a1 644 || TYPE_CODE (type) == TYPE_CODE_MODULE
7f3706eb
IB
645 || (TYPE_CODE (type) == TYPE_CODE_ENUM
646 && TYPE_DECLARED_CLASS (type)));
647}
648
3ed9baed
IB
649/* Take care of parsing a number (anything that starts with a digit).
650 Set yylval and return the token type; update lexptr.
651 LEN is the number of characters in it. */
652
653/*** Needs some error checking for the float case ***/
654
655static int
656parse_number (struct parser_state *ps, const char *p,
657 int len, int parsed_float, YYSTYPE *putithere)
658{
659 ULONGEST n = 0;
660 ULONGEST prevn = 0;
661 ULONGEST un;
662
663 int i = 0;
664 int c;
665 int base = input_radix;
666 int unsigned_p = 0;
667 int long_p = 0;
668
669 /* We have found a "L" or "U" suffix. */
670 int found_suffix = 0;
671
672 ULONGEST high_bit;
673 struct type *signed_type;
674 struct type *unsigned_type;
675
676 if (parsed_float)
677 {
3ed9baed
IB
678 char *s, *sp;
679
680 /* Strip out all embedded '_' before passing to parse_float. */
681 s = (char *) alloca (len + 1);
682 sp = s;
683 while (len-- > 0)
684 {
685 if (*p != '_')
686 *sp++ = *p;
687 p++;
688 }
689 *sp = '\0';
690 len = strlen (s);
691
edd079d9
UW
692 /* Check suffix for `i' , `fi' or `li' (idouble, ifloat or ireal). */
693 if (len >= 1 && tolower (s[len - 1]) == 'i')
3ed9baed 694 {
edd079d9 695 if (len >= 2 && tolower (s[len - 2]) == 'f')
3ed9baed
IB
696 {
697 putithere->typed_val_float.type
edd079d9
UW
698 = parse_d_type (ps)->builtin_ifloat;
699 len -= 2;
3ed9baed 700 }
edd079d9 701 else if (len >= 2 && tolower (s[len - 2]) == 'l')
3ed9baed
IB
702 {
703 putithere->typed_val_float.type
edd079d9
UW
704 = parse_d_type (ps)->builtin_ireal;
705 len -= 2;
3ed9baed 706 }
edd079d9 707 else
3ed9baed
IB
708 {
709 putithere->typed_val_float.type
710 = parse_d_type (ps)->builtin_idouble;
edd079d9 711 len -= 1;
3ed9baed 712 }
3ed9baed 713 }
edd079d9
UW
714 /* Check suffix for `f' or `l'' (float or real). */
715 else if (len >= 1 && tolower (s[len - 1]) == 'f')
3ed9baed 716 {
edd079d9
UW
717 putithere->typed_val_float.type
718 = parse_d_type (ps)->builtin_float;
719 len -= 1;
720 }
721 else if (len >= 1 && tolower (s[len - 1]) == 'l')
722 {
723 putithere->typed_val_float.type
724 = parse_d_type (ps)->builtin_real;
725 len -= 1;
3ed9baed 726 }
edd079d9 727 /* Default type if no suffix. */
3ed9baed 728 else
edd079d9
UW
729 {
730 putithere->typed_val_float.type
731 = parse_d_type (ps)->builtin_double;
732 }
733
734 if (!parse_float (s, len,
735 putithere->typed_val_float.type,
736 putithere->typed_val_float.val))
3ed9baed
IB
737 return ERROR;
738
739 return FLOAT_LITERAL;
740 }
741
742 /* Handle base-switching prefixes 0x, 0b, 0 */
743 if (p[0] == '0')
744 switch (p[1])
745 {
746 case 'x':
747 case 'X':
748 if (len >= 3)
749 {
750 p += 2;
751 base = 16;
752 len -= 2;
753 }
754 break;
755
756 case 'b':
757 case 'B':
758 if (len >= 3)
759 {
760 p += 2;
761 base = 2;
762 len -= 2;
763 }
764 break;
765
766 default:
767 base = 8;
768 break;
769 }
770
771 while (len-- > 0)
772 {
773 c = *p++;
774 if (c == '_')
775 continue; /* Ignore embedded '_'. */
776 if (c >= 'A' && c <= 'Z')
777 c += 'a' - 'A';
778 if (c != 'l' && c != 'u')
779 n *= base;
780 if (c >= '0' && c <= '9')
781 {
782 if (found_suffix)
783 return ERROR;
784 n += i = c - '0';
785 }
786 else
787 {
788 if (base > 10 && c >= 'a' && c <= 'f')
789 {
790 if (found_suffix)
791 return ERROR;
792 n += i = c - 'a' + 10;
793 }
794 else if (c == 'l' && long_p == 0)
795 {
796 long_p = 1;
797 found_suffix = 1;
798 }
799 else if (c == 'u' && unsigned_p == 0)
800 {
801 unsigned_p = 1;
802 found_suffix = 1;
803 }
804 else
805 return ERROR; /* Char not a digit */
806 }
807 if (i >= base)
808 return ERROR; /* Invalid digit in this base. */
809 /* Portably test for integer overflow. */
810 if (c != 'l' && c != 'u')
811 {
812 ULONGEST n2 = prevn * base;
813 if ((n2 / base != prevn) || (n2 + i < prevn))
814 error (_("Numeric constant too large."));
815 }
816 prevn = n;
817 }
818
819 /* An integer constant is an int or a long. An L suffix forces it to
820 be long, and a U suffix forces it to be unsigned. To figure out
821 whether it fits, we shift it right and see whether anything remains.
822 Note that we can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or
823 more in one operation, because many compilers will warn about such a
824 shift (which always produces a zero result). To deal with the case
825 where it is we just always shift the value more than once, with fewer
826 bits each time. */
827 un = (ULONGEST) n >> 2;
828 if (long_p == 0 && (un >> 30) == 0)
829 {
830 high_bit = ((ULONGEST) 1) << 31;
831 signed_type = parse_d_type (ps)->builtin_int;
832 /* For decimal notation, keep the sign of the worked out type. */
833 if (base == 10 && !unsigned_p)
834 unsigned_type = parse_d_type (ps)->builtin_long;
835 else
836 unsigned_type = parse_d_type (ps)->builtin_uint;
837 }
838 else
839 {
840 int shift;
841 if (sizeof (ULONGEST) * HOST_CHAR_BIT < 64)
842 /* A long long does not fit in a LONGEST. */
843 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
844 else
845 shift = 63;
846 high_bit = (ULONGEST) 1 << shift;
847 signed_type = parse_d_type (ps)->builtin_long;
848 unsigned_type = parse_d_type (ps)->builtin_ulong;
849 }
850
851 putithere->typed_val_int.val = n;
852
853 /* If the high bit of the worked out type is set then this number
854 has to be unsigned_type. */
855 if (unsigned_p || (n & high_bit))
856 putithere->typed_val_int.type = unsigned_type;
857 else
858 putithere->typed_val_int.type = signed_type;
859
860 return INTEGER_LITERAL;
861}
862
863/* Temporary obstack used for holding strings. */
864static struct obstack tempbuf;
865static int tempbuf_init;
866
867/* Parse a string or character literal from TOKPTR. The string or
868 character may be wide or unicode. *OUTPTR is set to just after the
869 end of the literal in the input string. The resulting token is
870 stored in VALUE. This returns a token value, either STRING or
871 CHAR, depending on what was parsed. *HOST_CHARS is set to the
872 number of host characters in the literal. */
873
874static int
875parse_string_or_char (const char *tokptr, const char **outptr,
876 struct typed_stoken *value, int *host_chars)
877{
878 int quote;
879
880 /* Build the gdb internal form of the input string in tempbuf. Note
881 that the buffer is null byte terminated *only* for the
882 convenience of debugging gdb itself and printing the buffer
883 contents when the buffer contains no embedded nulls. Gdb does
884 not depend upon the buffer being null byte terminated, it uses
885 the length string instead. This allows gdb to handle C strings
886 (as well as strings in other languages) with embedded null
887 bytes */
888
889 if (!tempbuf_init)
890 tempbuf_init = 1;
891 else
892 obstack_free (&tempbuf, NULL);
893 obstack_init (&tempbuf);
894
895 /* Skip the quote. */
896 quote = *tokptr;
897 ++tokptr;
898
899 *host_chars = 0;
900
901 while (*tokptr)
902 {
903 char c = *tokptr;
904 if (c == '\\')
905 {
906 ++tokptr;
907 *host_chars += c_parse_escape (&tokptr, &tempbuf);
908 }
909 else if (c == quote)
910 break;
911 else
912 {
913 obstack_1grow (&tempbuf, c);
914 ++tokptr;
915 /* FIXME: this does the wrong thing with multi-byte host
916 characters. We could use mbrlen here, but that would
917 make "set host-charset" a bit less useful. */
918 ++*host_chars;
919 }
920 }
921
922 if (*tokptr != quote)
923 {
924 if (quote == '"' || quote == '`')
925 error (_("Unterminated string in expression."));
926 else
927 error (_("Unmatched single quote."));
928 }
929 ++tokptr;
930
931 /* FIXME: should instead use own language string_type enum
932 and handle D-specific string suffixes here. */
933 if (quote == '\'')
934 value->type = C_CHAR;
935 else
936 value->type = C_STRING;
937
79f33898 938 value->ptr = (char *) obstack_base (&tempbuf);
3ed9baed
IB
939 value->length = obstack_object_size (&tempbuf);
940
941 *outptr = tokptr;
942
943 return quote == '\'' ? CHARACTER_LITERAL : STRING_LITERAL;
944}
945
946struct token
947{
a121b7c1 948 const char *oper;
3ed9baed
IB
949 int token;
950 enum exp_opcode opcode;
951};
952
953static const struct token tokentab3[] =
954 {
955 {"^^=", ASSIGN_MODIFY, BINOP_EXP},
956 {"<<=", ASSIGN_MODIFY, BINOP_LSH},
957 {">>=", ASSIGN_MODIFY, BINOP_RSH},
958 };
959
960static const struct token tokentab2[] =
961 {
962 {"+=", ASSIGN_MODIFY, BINOP_ADD},
963 {"-=", ASSIGN_MODIFY, BINOP_SUB},
964 {"*=", ASSIGN_MODIFY, BINOP_MUL},
965 {"/=", ASSIGN_MODIFY, BINOP_DIV},
966 {"%=", ASSIGN_MODIFY, BINOP_REM},
967 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
968 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
969 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
970 {"++", INCREMENT, BINOP_END},
971 {"--", DECREMENT, BINOP_END},
972 {"&&", ANDAND, BINOP_END},
973 {"||", OROR, BINOP_END},
974 {"^^", HATHAT, BINOP_END},
975 {"<<", LSH, BINOP_END},
976 {">>", RSH, BINOP_END},
977 {"==", EQUAL, BINOP_END},
978 {"!=", NOTEQUAL, BINOP_END},
979 {"<=", LEQ, BINOP_END},
980 {">=", GEQ, BINOP_END},
981 {"..", DOTDOT, BINOP_END},
982 };
983
984/* Identifier-like tokens. */
985static const struct token ident_tokens[] =
986 {
987 {"is", IDENTITY, BINOP_END},
988 {"!is", NOTIDENTITY, BINOP_END},
989
990 {"cast", CAST_KEYWORD, OP_NULL},
991 {"const", CONST_KEYWORD, OP_NULL},
992 {"immutable", IMMUTABLE_KEYWORD, OP_NULL},
993 {"shared", SHARED_KEYWORD, OP_NULL},
994 {"super", SUPER_KEYWORD, OP_NULL},
995
996 {"null", NULL_KEYWORD, OP_NULL},
997 {"true", TRUE_KEYWORD, OP_NULL},
998 {"false", FALSE_KEYWORD, OP_NULL},
999
1000 {"init", INIT_KEYWORD, OP_NULL},
1001 {"sizeof", SIZEOF_KEYWORD, OP_NULL},
1002 {"typeof", TYPEOF_KEYWORD, OP_NULL},
1003 {"typeid", TYPEID_KEYWORD, OP_NULL},
1004
1005 {"delegate", DELEGATE_KEYWORD, OP_NULL},
1006 {"function", FUNCTION_KEYWORD, OP_NULL},
1007 {"struct", STRUCT_KEYWORD, OP_NULL},
1008 {"union", UNION_KEYWORD, OP_NULL},
1009 {"class", CLASS_KEYWORD, OP_NULL},
1010 {"interface", INTERFACE_KEYWORD, OP_NULL},
1011 {"enum", ENUM_KEYWORD, OP_NULL},
1012 {"template", TEMPLATE_KEYWORD, OP_NULL},
1013 };
1014
3ed9baed
IB
1015/* This is set if a NAME token appeared at the very end of the input
1016 string, with no whitespace separating the name from the EOF. This
1017 is used only when parsing to do field name completion. */
1018static int saw_name_at_eof;
1019
1020/* This is set if the previously-returned token was a structure operator.
1021 This is used only when parsing to do field name completion. */
1022static int last_was_structop;
1023
1024/* Read one token, getting characters through lexptr. */
1025
1026static int
444c1ed8 1027lex_one_token (struct parser_state *par_state)
3ed9baed
IB
1028{
1029 int c;
1030 int namelen;
1031 unsigned int i;
1032 const char *tokstart;
1033 int saw_structop = last_was_structop;
1034 char *copy;
1035
1036 last_was_structop = 0;
1037
1038 retry:
1039
1040 prev_lexptr = lexptr;
1041
1042 tokstart = lexptr;
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
IB
1046 {
1047 lexptr += 3;
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
IB
1055 {
1056 lexptr += 2;
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
1076 return 0;
1077
1078 case ' ':
1079 case '\t':
1080 case '\n':
1081 lexptr++;
1082 goto retry;
1083
1084 case '[':
1085 case '(':
1086 paren_depth++;
1087 lexptr++;
1088 return c;
1089
1090 case ']':
1091 case ')':
1092 if (paren_depth == 0)
1093 return 0;
1094 paren_depth--;
1095 lexptr++;
1096 return c;
1097
1098 case ',':
1099 if (comma_terminates && paren_depth == 0)
1100 return 0;
1101 lexptr++;
1102 return c;
1103
1104 case '.':
1105 /* Might be a floating point number. */
1106 if (lexptr[1] < '0' || lexptr[1] > '9')
1107 {
1108 if (parse_completion)
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 }
1171 lexptr = p;
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 {
1185 lexptr = &p[len];
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:
1208 lexptr++;
1209 return c;
1210
1211 case '\'':
1212 case '"':
1213 case '`':
1214 {
1215 int host_len;
1216 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
1217 &host_len);
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;
1225 namelen = lexptr - tokstart - 1;
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')
1265 p++;
1266 if (*p >= '0' && *p <= '9')
1267 return 0;
1268 }
1269
1270 lexptr += namelen;
1271
1272 tryname:
1273
1274 yylval.sval.ptr = tokstart;
1275 yylval.sval.length = namelen;
1276
1277 /* Catch specific keywords. */
1278 copy = copy_name (yylval.sval);
1279 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
fe978cb0 1280 if (strcmp (copy, ident_tokens[i].oper) == 0)
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
444c1ed8
IB
1292 = language_lookup_primitive_type (parse_language (par_state),
1293 parse_gdbarch (par_state), copy);
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
1309 if (parse_completion && *lexptr == '\0')
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;
1342 char *copy;
1343 struct field_of_this_result is_a_field_of_this;
1344
1345 copy = copy_name (yylval.sval);
1346
1347 sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
1348 if (sym.symbol && SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF)
1349 {
1350 yylval.tsym.type = SYMBOL_TYPE (sym.symbol);
1351 return TYPENAME;
1352 }
1353 else if (sym.symbol == NULL)
1354 {
1355 /* Look-up first for a module name, then a type. */
1356 sym = lookup_symbol (copy, block, MODULE_DOMAIN, NULL);
1357 if (sym.symbol == NULL)
1358 sym = lookup_symbol (copy, block, STRUCT_DOMAIN, NULL);
1359
1360 if (sym.symbol != NULL)
1361 {
1362 yylval.tsym.type = SYMBOL_TYPE (sym.symbol);
1363 return TYPENAME;
1364 }
1365
1366 return UNKNOWN_NAME;
1367 }
1368
1369 return IDENTIFIER;
1370}
1371
1372/* Like classify_name, but used by the inner loop of the lexer, when a
1373 name might have already been seen. CONTEXT is the context type, or
1374 NULL if this is the first component of a name. */
1375
1376static int
1377classify_inner_name (struct parser_state *par_state,
1378 const struct block *block, struct type *context)
1379{
1380 struct type *type;
1381 char *copy;
1382
1383 if (context == NULL)
1384 return classify_name (par_state, block);
1385
1386 type = check_typedef (context);
7f3706eb
IB
1387 if (!type_aggregate_p (type))
1388 return ERROR;
444c1ed8
IB
1389
1390 copy = copy_name (yylval.ssym.stoken);
1391 yylval.ssym.sym = d_lookup_nested_symbol (type, copy, block);
1392
1393 if (yylval.ssym.sym.symbol == NULL)
1394 return ERROR;
1395
1396 if (SYMBOL_CLASS (yylval.ssym.sym.symbol) == LOC_TYPEDEF)
1397 {
1398 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
1399 return TYPENAME;
1400 }
1401
1402 return IDENTIFIER;
1403}
1404
1405/* The outer level of a two-level lexer. This calls the inner lexer
1406 to return tokens. It then either returns these tokens, or
1407 aggregates them into a larger token. This lets us work around a
1408 problem in our parsing approach, where the parser could not
1409 distinguish between qualified names and qualified types at the
1410 right point. */
1411
1412static int
1413yylex (void)
1414{
1415 token_and_value current;
1416 int last_was_dot;
1417 struct type *context_type = NULL;
1418 int last_to_examine, next_to_examine, checkpoint;
1419 const struct block *search_block;
1420
5fe3f3e4 1421 if (popping && !token_fifo.empty ())
444c1ed8
IB
1422 goto do_pop;
1423 popping = 0;
1424
1425 /* Read the first token and decide what to do. */
1426 current.token = lex_one_token (pstate);
1427 if (current.token != IDENTIFIER && current.token != '.')
1428 return current.token;
1429
1430 /* Read any sequence of alternating "." and identifier tokens into
1431 the token FIFO. */
1432 current.value = yylval;
5fe3f3e4 1433 token_fifo.push_back (current);
444c1ed8
IB
1434 last_was_dot = current.token == '.';
1435
1436 while (1)
1437 {
1438 current.token = lex_one_token (pstate);
1439 current.value = yylval;
5fe3f3e4 1440 token_fifo.push_back (current);
444c1ed8
IB
1441
1442 if ((last_was_dot && current.token != IDENTIFIER)
1443 || (!last_was_dot && current.token != '.'))
1444 break;
1445
1446 last_was_dot = !last_was_dot;
1447 }
1448 popping = 1;
1449
1450 /* We always read one extra token, so compute the number of tokens
1451 to examine accordingly. */
5fe3f3e4 1452 last_to_examine = token_fifo.size () - 2;
444c1ed8
IB
1453 next_to_examine = 0;
1454
5fe3f3e4 1455 current = token_fifo[next_to_examine];
444c1ed8
IB
1456 ++next_to_examine;
1457
1458 /* If we are not dealing with a typename, now is the time to find out. */
1459 if (current.token == IDENTIFIER)
1460 {
1461 yylval = current.value;
1462 current.token = classify_name (pstate, expression_context_block);
1463 current.value = yylval;
1464 }
1465
1466 /* If the IDENTIFIER is not known, it could be a package symbol,
1467 first try building up a name until we find the qualified module. */
1468 if (current.token == UNKNOWN_NAME)
1469 {
8268c778 1470 name_obstack.clear ();
444c1ed8
IB
1471 obstack_grow (&name_obstack, current.value.sval.ptr,
1472 current.value.sval.length);
1473
1474 last_was_dot = 0;
1475
1476 while (next_to_examine <= last_to_examine)
1477 {
5fe3f3e4 1478 token_and_value next;
444c1ed8 1479
5fe3f3e4 1480 next = token_fifo[next_to_examine];
444c1ed8
IB
1481 ++next_to_examine;
1482
5fe3f3e4 1483 if (next.token == IDENTIFIER && last_was_dot)
444c1ed8
IB
1484 {
1485 /* Update the partial name we are constructing. */
1486 obstack_grow_str (&name_obstack, ".");
5fe3f3e4
TT
1487 obstack_grow (&name_obstack, next.value.sval.ptr,
1488 next.value.sval.length);
444c1ed8 1489
79f33898 1490 yylval.sval.ptr = (char *) obstack_base (&name_obstack);
444c1ed8
IB
1491 yylval.sval.length = obstack_object_size (&name_obstack);
1492
1493 current.token = classify_name (pstate, expression_context_block);
1494 current.value = yylval;
1495
1496 /* We keep going until we find a TYPENAME. */
1497 if (current.token == TYPENAME)
1498 {
1499 /* Install it as the first token in the FIFO. */
5fe3f3e4
TT
1500 token_fifo[0] = current;
1501 token_fifo.erase (token_fifo.begin () + 1,
1502 token_fifo.begin () + next_to_examine);
444c1ed8
IB
1503 break;
1504 }
1505 }
5fe3f3e4 1506 else if (next.token == '.' && !last_was_dot)
444c1ed8
IB
1507 last_was_dot = 1;
1508 else
1509 {
1510 /* We've reached the end of the name. */
1511 break;
1512 }
1513 }
1514
1515 /* Reset our current token back to the start, if we found nothing
1516 this means that we will just jump to do pop. */
5fe3f3e4 1517 current = token_fifo[0];
444c1ed8
IB
1518 next_to_examine = 1;
1519 }
1520 if (current.token != TYPENAME && current.token != '.')
1521 goto do_pop;
1522
8268c778 1523 name_obstack.clear ();
444c1ed8
IB
1524 checkpoint = 0;
1525 if (current.token == '.')
1526 search_block = NULL;
1527 else
1528 {
1529 gdb_assert (current.token == TYPENAME);
1530 search_block = expression_context_block;
1531 obstack_grow (&name_obstack, current.value.sval.ptr,
1532 current.value.sval.length);
1533 context_type = current.value.tsym.type;
1534 checkpoint = 1;
1535 }
1536
1537 last_was_dot = current.token == '.';
1538
1539 while (next_to_examine <= last_to_examine)
1540 {
5fe3f3e4 1541 token_and_value next;
444c1ed8 1542
5fe3f3e4 1543 next = token_fifo[next_to_examine];
444c1ed8
IB
1544 ++next_to_examine;
1545
5fe3f3e4 1546 if (next.token == IDENTIFIER && last_was_dot)
444c1ed8
IB
1547 {
1548 int classification;
1549
5fe3f3e4 1550 yylval = next.value;
444c1ed8
IB
1551 classification = classify_inner_name (pstate, search_block,
1552 context_type);
1553 /* We keep going until we either run out of names, or until
1554 we have a qualified name which is not a type. */
1555 if (classification != TYPENAME && classification != IDENTIFIER)
1556 break;
1557
1558 /* Accept up to this token. */
1559 checkpoint = next_to_examine;
1560
1561 /* Update the partial name we are constructing. */
1562 if (context_type != NULL)
1563 {
1564 /* We don't want to put a leading "." into the name. */
1565 obstack_grow_str (&name_obstack, ".");
1566 }
5fe3f3e4
TT
1567 obstack_grow (&name_obstack, next.value.sval.ptr,
1568 next.value.sval.length);
444c1ed8 1569
79f33898 1570 yylval.sval.ptr = (char *) obstack_base (&name_obstack);
444c1ed8
IB
1571 yylval.sval.length = obstack_object_size (&name_obstack);
1572 current.value = yylval;
1573 current.token = classification;
1574
1575 last_was_dot = 0;
1576
1577 if (classification == IDENTIFIER)
1578 break;
1579
1580 context_type = yylval.tsym.type;
1581 }
5fe3f3e4 1582 else if (next.token == '.' && !last_was_dot)
444c1ed8
IB
1583 last_was_dot = 1;
1584 else
1585 {
1586 /* We've reached the end of the name. */
1587 break;
1588 }
1589 }
1590
1591 /* If we have a replacement token, install it as the first token in
1592 the FIFO, and delete the other constituent tokens. */
1593 if (checkpoint > 0)
1594 {
5fe3f3e4 1595 token_fifo[0] = current;
444c1ed8 1596 if (checkpoint > 1)
5fe3f3e4
TT
1597 token_fifo.erase (token_fifo.begin () + 1,
1598 token_fifo.begin () + checkpoint);
444c1ed8
IB
1599 }
1600
1601 do_pop:
5fe3f3e4
TT
1602 current = token_fifo[0];
1603 token_fifo.erase (token_fifo.begin ());
444c1ed8
IB
1604 yylval = current.value;
1605 return current.token;
1606}
1607
3ed9baed
IB
1608int
1609d_parse (struct parser_state *par_state)
1610{
3ed9baed 1611 /* Setting up the parser state. */
eae49211 1612 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3ed9baed
IB
1613 gdb_assert (par_state != NULL);
1614 pstate = par_state;
1615
156d9eab
TT
1616 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
1617 parser_debug);
3ed9baed
IB
1618
1619 /* Initialize some state used by the lexer. */
1620 last_was_structop = 0;
1621 saw_name_at_eof = 0;
1622
5fe3f3e4 1623 token_fifo.clear ();
444c1ed8 1624 popping = 0;
8268c778 1625 name_obstack.clear ();
444c1ed8 1626
5613c585 1627 return yyparse ();
3ed9baed
IB
1628}
1629
69d340c6 1630static void
a121b7c1 1631yyerror (const char *msg)
3ed9baed
IB
1632{
1633 if (prev_lexptr)
1634 lexptr = prev_lexptr;
1635
69d340c6 1636 error (_("A %s in expression, near `%s'."), msg, lexptr);
3ed9baed
IB
1637}
1638