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