Commit | Line | Data |
---|---|---|
a766d390 DE |
1 | /* YACC parser for Go expressions, for GDB. |
2 | ||
e2882c85 | 3 | Copyright (C) 2012-2018 Free Software Foundation, Inc. |
a766d390 DE |
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, p-exp.y. */ | |
21 | ||
22 | /* Parse a Go 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 | /* Known bugs or limitations: | |
40 | ||
41 | - Unicode | |
42 | - &^ | |
43 | - '_' (blank identifier) | |
44 | - automatic deref of pointers | |
45 | - method expressions | |
46 | - interfaces, channels, etc. | |
47 | ||
48 | And lots of other things. | |
49 | I'm sure there's some cleanup to do. | |
50 | */ | |
51 | ||
52 | %{ | |
53 | ||
54 | #include "defs.h" | |
a766d390 DE |
55 | #include <ctype.h> |
56 | #include "expression.h" | |
57 | #include "value.h" | |
58 | #include "parser-defs.h" | |
59 | #include "language.h" | |
60 | #include "c-lang.h" | |
61 | #include "go-lang.h" | |
62 | #include "bfd.h" /* Required by objfiles.h. */ | |
63 | #include "symfile.h" /* Required by objfiles.h. */ | |
64 | #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */ | |
65 | #include "charset.h" | |
66 | #include "block.h" | |
67 | ||
410a0ff2 | 68 | #define parse_type(ps) builtin_type (parse_gdbarch (ps)) |
a766d390 | 69 | |
b3f11165 PA |
70 | /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, |
71 | etc). */ | |
72 | #define GDB_YY_REMAP_PREFIX go_ | |
73 | #include "yy-remap.h" | |
a766d390 | 74 | |
410a0ff2 SDJ |
75 | /* The state of the parser, used internally when we are parsing the |
76 | expression. */ | |
77 | ||
78 | static struct parser_state *pstate = NULL; | |
79 | ||
a766d390 DE |
80 | int yyparse (void); |
81 | ||
82 | static int yylex (void); | |
83 | ||
a121b7c1 | 84 | void yyerror (const char *); |
a766d390 DE |
85 | |
86 | %} | |
87 | ||
88 | /* Although the yacc "value" of an expression is not used, | |
89 | since the result is stored in the structure being created, | |
90 | other node types do have values. */ | |
91 | ||
92 | %union | |
93 | { | |
94 | LONGEST lval; | |
95 | struct { | |
96 | LONGEST val; | |
97 | struct type *type; | |
98 | } typed_val_int; | |
99 | struct { | |
edd079d9 | 100 | gdb_byte val[16]; |
a766d390 DE |
101 | struct type *type; |
102 | } typed_val_float; | |
103 | struct stoken sval; | |
104 | struct symtoken ssym; | |
105 | struct type *tval; | |
106 | struct typed_stoken tsval; | |
107 | struct ttype tsym; | |
108 | int voidval; | |
109 | enum exp_opcode opcode; | |
110 | struct internalvar *ivar; | |
111 | struct stoken_vector svec; | |
112 | } | |
113 | ||
114 | %{ | |
115 | /* YYSTYPE gets defined by %union. */ | |
410a0ff2 SDJ |
116 | static int parse_number (struct parser_state *, |
117 | const char *, int, int, YYSTYPE *); | |
a766d390 DE |
118 | %} |
119 | ||
120 | %type <voidval> exp exp1 type_exp start variable lcurly | |
121 | %type <lval> rcurly | |
122 | %type <tval> type | |
123 | ||
124 | %token <typed_val_int> INT | |
125 | %token <typed_val_float> FLOAT | |
126 | ||
127 | /* Both NAME and TYPENAME tokens represent symbols in the input, | |
128 | and both convey their data as strings. | |
129 | But a TYPENAME is a string that happens to be defined as a type | |
130 | or builtin type name (such as int or char) | |
131 | and a NAME is any other symbol. | |
132 | Contexts where this distinction is not important can use the | |
133 | nonterminal "name", which matches either NAME or TYPENAME. */ | |
134 | ||
135 | %token <tsval> RAW_STRING | |
136 | %token <tsval> STRING | |
137 | %token <tsval> CHAR | |
138 | %token <ssym> NAME | |
139 | %token <tsym> TYPENAME /* Not TYPE_NAME cus already taken. */ | |
140 | %token <voidval> COMPLETE | |
141 | /*%type <sval> name*/ | |
142 | %type <svec> string_exp | |
143 | %type <ssym> name_not_typename | |
144 | ||
145 | /* A NAME_OR_INT is a symbol which is not known in the symbol table, | |
146 | but which would parse as a valid number in the current input radix. | |
147 | E.g. "c" when input_radix==16. Depending on the parse, it will be | |
148 | turned into a name or into a number. */ | |
149 | %token <ssym> NAME_OR_INT | |
150 | ||
151 | %token <lval> TRUE_KEYWORD FALSE_KEYWORD | |
152 | %token STRUCT_KEYWORD INTERFACE_KEYWORD TYPE_KEYWORD CHAN_KEYWORD | |
153 | %token SIZEOF_KEYWORD | |
154 | %token LEN_KEYWORD CAP_KEYWORD | |
155 | %token NEW_KEYWORD | |
156 | %token IOTA_KEYWORD NIL_KEYWORD | |
157 | %token CONST_KEYWORD | |
158 | %token DOTDOTDOT | |
159 | %token ENTRY | |
160 | %token ERROR | |
161 | ||
162 | /* Special type cases. */ | |
163 | %token BYTE_KEYWORD /* An alias of uint8. */ | |
164 | ||
165 | %token <sval> DOLLAR_VARIABLE | |
166 | ||
167 | %token <opcode> ASSIGN_MODIFY | |
168 | ||
169 | %left ',' | |
170 | %left ABOVE_COMMA | |
171 | %right '=' ASSIGN_MODIFY | |
172 | %right '?' | |
173 | %left OROR | |
174 | %left ANDAND | |
175 | %left '|' | |
176 | %left '^' | |
177 | %left '&' | |
178 | %left ANDNOT | |
179 | %left EQUAL NOTEQUAL | |
180 | %left '<' '>' LEQ GEQ | |
181 | %left LSH RSH | |
182 | %left '@' | |
183 | %left '+' '-' | |
184 | %left '*' '/' '%' | |
185 | %right UNARY INCREMENT DECREMENT | |
186 | %right LEFT_ARROW '.' '[' '(' | |
187 | ||
188 | \f | |
189 | %% | |
190 | ||
191 | start : exp1 | |
192 | | type_exp | |
193 | ; | |
194 | ||
195 | type_exp: type | |
410a0ff2 SDJ |
196 | { write_exp_elt_opcode (pstate, OP_TYPE); |
197 | write_exp_elt_type (pstate, $1); | |
198 | write_exp_elt_opcode (pstate, OP_TYPE); } | |
a766d390 DE |
199 | ; |
200 | ||
201 | /* Expressions, including the comma operator. */ | |
202 | exp1 : exp | |
203 | | exp1 ',' exp | |
410a0ff2 | 204 | { write_exp_elt_opcode (pstate, BINOP_COMMA); } |
a766d390 DE |
205 | ; |
206 | ||
207 | /* Expressions, not including the comma operator. */ | |
208 | exp : '*' exp %prec UNARY | |
410a0ff2 | 209 | { write_exp_elt_opcode (pstate, UNOP_IND); } |
a766d390 DE |
210 | ; |
211 | ||
212 | exp : '&' exp %prec UNARY | |
410a0ff2 | 213 | { write_exp_elt_opcode (pstate, UNOP_ADDR); } |
a766d390 DE |
214 | ; |
215 | ||
216 | exp : '-' exp %prec UNARY | |
410a0ff2 | 217 | { write_exp_elt_opcode (pstate, UNOP_NEG); } |
a766d390 DE |
218 | ; |
219 | ||
220 | exp : '+' exp %prec UNARY | |
410a0ff2 | 221 | { write_exp_elt_opcode (pstate, UNOP_PLUS); } |
a766d390 DE |
222 | ; |
223 | ||
224 | exp : '!' exp %prec UNARY | |
410a0ff2 | 225 | { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); } |
a766d390 DE |
226 | ; |
227 | ||
228 | exp : '^' exp %prec UNARY | |
410a0ff2 | 229 | { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); } |
a766d390 DE |
230 | ; |
231 | ||
232 | exp : exp INCREMENT %prec UNARY | |
410a0ff2 | 233 | { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); } |
a766d390 DE |
234 | ; |
235 | ||
236 | exp : exp DECREMENT %prec UNARY | |
410a0ff2 | 237 | { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); } |
a766d390 DE |
238 | ; |
239 | ||
240 | /* foo->bar is not in Go. May want as a gdb extension. Later. */ | |
241 | ||
242 | exp : exp '.' name_not_typename | |
410a0ff2 SDJ |
243 | { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); |
244 | write_exp_string (pstate, $3.stoken); | |
245 | write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); } | |
a766d390 DE |
246 | ; |
247 | ||
248 | exp : exp '.' name_not_typename COMPLETE | |
410a0ff2 SDJ |
249 | { mark_struct_expression (pstate); |
250 | write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); | |
251 | write_exp_string (pstate, $3.stoken); | |
252 | write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); } | |
a766d390 DE |
253 | ; |
254 | ||
255 | exp : exp '.' COMPLETE | |
256 | { struct stoken s; | |
410a0ff2 SDJ |
257 | mark_struct_expression (pstate); |
258 | write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); | |
a766d390 DE |
259 | s.ptr = ""; |
260 | s.length = 0; | |
410a0ff2 SDJ |
261 | write_exp_string (pstate, s); |
262 | write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); } | |
a766d390 DE |
263 | ; |
264 | ||
265 | exp : exp '[' exp1 ']' | |
410a0ff2 | 266 | { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); } |
a766d390 DE |
267 | ; |
268 | ||
269 | exp : exp '(' | |
270 | /* This is to save the value of arglist_len | |
271 | being accumulated by an outer function call. */ | |
272 | { start_arglist (); } | |
273 | arglist ')' %prec LEFT_ARROW | |
410a0ff2 SDJ |
274 | { write_exp_elt_opcode (pstate, OP_FUNCALL); |
275 | write_exp_elt_longcst (pstate, | |
276 | (LONGEST) end_arglist ()); | |
277 | write_exp_elt_opcode (pstate, OP_FUNCALL); } | |
a766d390 DE |
278 | ; |
279 | ||
280 | lcurly : '{' | |
281 | { start_arglist (); } | |
282 | ; | |
283 | ||
284 | arglist : | |
285 | ; | |
286 | ||
287 | arglist : exp | |
288 | { arglist_len = 1; } | |
289 | ; | |
290 | ||
291 | arglist : arglist ',' exp %prec ABOVE_COMMA | |
292 | { arglist_len++; } | |
293 | ; | |
294 | ||
295 | rcurly : '}' | |
296 | { $$ = end_arglist () - 1; } | |
297 | ; | |
298 | ||
299 | exp : lcurly type rcurly exp %prec UNARY | |
410a0ff2 SDJ |
300 | { write_exp_elt_opcode (pstate, UNOP_MEMVAL); |
301 | write_exp_elt_type (pstate, $2); | |
302 | write_exp_elt_opcode (pstate, UNOP_MEMVAL); } | |
a766d390 DE |
303 | ; |
304 | ||
305 | exp : type '(' exp ')' %prec UNARY | |
410a0ff2 SDJ |
306 | { write_exp_elt_opcode (pstate, UNOP_CAST); |
307 | write_exp_elt_type (pstate, $1); | |
308 | write_exp_elt_opcode (pstate, UNOP_CAST); } | |
a766d390 DE |
309 | ; |
310 | ||
311 | exp : '(' exp1 ')' | |
312 | { } | |
313 | ; | |
314 | ||
315 | /* Binary operators in order of decreasing precedence. */ | |
316 | ||
317 | exp : exp '@' exp | |
410a0ff2 | 318 | { write_exp_elt_opcode (pstate, BINOP_REPEAT); } |
a766d390 DE |
319 | ; |
320 | ||
321 | exp : exp '*' exp | |
410a0ff2 | 322 | { write_exp_elt_opcode (pstate, BINOP_MUL); } |
a766d390 DE |
323 | ; |
324 | ||
325 | exp : exp '/' exp | |
410a0ff2 | 326 | { write_exp_elt_opcode (pstate, BINOP_DIV); } |
a766d390 DE |
327 | ; |
328 | ||
329 | exp : exp '%' exp | |
410a0ff2 | 330 | { write_exp_elt_opcode (pstate, BINOP_REM); } |
a766d390 DE |
331 | ; |
332 | ||
333 | exp : exp '+' exp | |
410a0ff2 | 334 | { write_exp_elt_opcode (pstate, BINOP_ADD); } |
a766d390 DE |
335 | ; |
336 | ||
337 | exp : exp '-' exp | |
410a0ff2 | 338 | { write_exp_elt_opcode (pstate, BINOP_SUB); } |
a766d390 DE |
339 | ; |
340 | ||
341 | exp : exp LSH exp | |
410a0ff2 | 342 | { write_exp_elt_opcode (pstate, BINOP_LSH); } |
a766d390 DE |
343 | ; |
344 | ||
345 | exp : exp RSH exp | |
410a0ff2 | 346 | { write_exp_elt_opcode (pstate, BINOP_RSH); } |
a766d390 DE |
347 | ; |
348 | ||
349 | exp : exp EQUAL exp | |
410a0ff2 | 350 | { write_exp_elt_opcode (pstate, BINOP_EQUAL); } |
a766d390 DE |
351 | ; |
352 | ||
353 | exp : exp NOTEQUAL exp | |
410a0ff2 | 354 | { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); } |
a766d390 DE |
355 | ; |
356 | ||
357 | exp : exp LEQ exp | |
410a0ff2 | 358 | { write_exp_elt_opcode (pstate, BINOP_LEQ); } |
a766d390 DE |
359 | ; |
360 | ||
361 | exp : exp GEQ exp | |
410a0ff2 | 362 | { write_exp_elt_opcode (pstate, BINOP_GEQ); } |
a766d390 DE |
363 | ; |
364 | ||
365 | exp : exp '<' exp | |
410a0ff2 | 366 | { write_exp_elt_opcode (pstate, BINOP_LESS); } |
a766d390 DE |
367 | ; |
368 | ||
369 | exp : exp '>' exp | |
410a0ff2 | 370 | { write_exp_elt_opcode (pstate, BINOP_GTR); } |
a766d390 DE |
371 | ; |
372 | ||
373 | exp : exp '&' exp | |
410a0ff2 | 374 | { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); } |
a766d390 DE |
375 | ; |
376 | ||
377 | exp : exp '^' exp | |
410a0ff2 | 378 | { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); } |
a766d390 DE |
379 | ; |
380 | ||
381 | exp : exp '|' exp | |
410a0ff2 | 382 | { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); } |
a766d390 DE |
383 | ; |
384 | ||
385 | exp : exp ANDAND exp | |
410a0ff2 | 386 | { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); } |
a766d390 DE |
387 | ; |
388 | ||
389 | exp : exp OROR exp | |
410a0ff2 | 390 | { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); } |
a766d390 DE |
391 | ; |
392 | ||
393 | exp : exp '?' exp ':' exp %prec '?' | |
410a0ff2 | 394 | { write_exp_elt_opcode (pstate, TERNOP_COND); } |
a766d390 DE |
395 | ; |
396 | ||
397 | exp : exp '=' exp | |
410a0ff2 | 398 | { write_exp_elt_opcode (pstate, BINOP_ASSIGN); } |
a766d390 DE |
399 | ; |
400 | ||
401 | exp : exp ASSIGN_MODIFY exp | |
410a0ff2 SDJ |
402 | { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); |
403 | write_exp_elt_opcode (pstate, $2); | |
404 | write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); } | |
a766d390 DE |
405 | ; |
406 | ||
407 | exp : INT | |
410a0ff2 SDJ |
408 | { write_exp_elt_opcode (pstate, OP_LONG); |
409 | write_exp_elt_type (pstate, $1.type); | |
410 | write_exp_elt_longcst (pstate, (LONGEST)($1.val)); | |
411 | write_exp_elt_opcode (pstate, OP_LONG); } | |
a766d390 DE |
412 | ; |
413 | ||
414 | exp : CHAR | |
415 | { | |
416 | struct stoken_vector vec; | |
417 | vec.len = 1; | |
418 | vec.tokens = &$1; | |
410a0ff2 | 419 | write_exp_string_vector (pstate, $1.type, &vec); |
a766d390 DE |
420 | } |
421 | ; | |
422 | ||
423 | exp : NAME_OR_INT | |
424 | { YYSTYPE val; | |
410a0ff2 SDJ |
425 | parse_number (pstate, $1.stoken.ptr, |
426 | $1.stoken.length, 0, &val); | |
427 | write_exp_elt_opcode (pstate, OP_LONG); | |
428 | write_exp_elt_type (pstate, val.typed_val_int.type); | |
429 | write_exp_elt_longcst (pstate, (LONGEST) | |
a766d390 | 430 | val.typed_val_int.val); |
410a0ff2 | 431 | write_exp_elt_opcode (pstate, OP_LONG); |
a766d390 DE |
432 | } |
433 | ; | |
434 | ||
435 | ||
436 | exp : FLOAT | |
edd079d9 | 437 | { write_exp_elt_opcode (pstate, OP_FLOAT); |
410a0ff2 | 438 | write_exp_elt_type (pstate, $1.type); |
edd079d9 UW |
439 | write_exp_elt_floatcst (pstate, $1.val); |
440 | write_exp_elt_opcode (pstate, OP_FLOAT); } | |
a766d390 DE |
441 | ; |
442 | ||
443 | exp : variable | |
444 | ; | |
445 | ||
446 | exp : DOLLAR_VARIABLE | |
447 | { | |
410a0ff2 | 448 | write_dollar_variable (pstate, $1); |
a766d390 DE |
449 | } |
450 | ; | |
451 | ||
452 | exp : SIZEOF_KEYWORD '(' type ')' %prec UNARY | |
453 | { | |
454 | /* TODO(dje): Go objects in structs. */ | |
410a0ff2 | 455 | write_exp_elt_opcode (pstate, OP_LONG); |
a766d390 | 456 | /* TODO(dje): What's the right type here? */ |
410a0ff2 SDJ |
457 | write_exp_elt_type |
458 | (pstate, | |
459 | parse_type (pstate)->builtin_unsigned_int); | |
f168693b | 460 | $3 = check_typedef ($3); |
410a0ff2 SDJ |
461 | write_exp_elt_longcst (pstate, |
462 | (LONGEST) TYPE_LENGTH ($3)); | |
463 | write_exp_elt_opcode (pstate, OP_LONG); | |
a766d390 DE |
464 | } |
465 | ; | |
466 | ||
467 | exp : SIZEOF_KEYWORD '(' exp ')' %prec UNARY | |
468 | { | |
469 | /* TODO(dje): Go objects in structs. */ | |
410a0ff2 | 470 | write_exp_elt_opcode (pstate, UNOP_SIZEOF); |
a766d390 DE |
471 | } |
472 | ||
473 | string_exp: | |
474 | STRING | |
475 | { | |
476 | /* We copy the string here, and not in the | |
477 | lexer, to guarantee that we do not leak a | |
478 | string. */ | |
479 | /* Note that we NUL-terminate here, but just | |
480 | for convenience. */ | |
481 | struct typed_stoken *vec = XNEW (struct typed_stoken); | |
482 | $$.len = 1; | |
483 | $$.tokens = vec; | |
484 | ||
485 | vec->type = $1.type; | |
486 | vec->length = $1.length; | |
224c3ddb | 487 | vec->ptr = (char *) malloc ($1.length + 1); |
a766d390 DE |
488 | memcpy (vec->ptr, $1.ptr, $1.length + 1); |
489 | } | |
490 | ||
491 | | string_exp '+' STRING | |
492 | { | |
493 | /* Note that we NUL-terminate here, but just | |
494 | for convenience. */ | |
495 | char *p; | |
496 | ++$$.len; | |
224c3ddb SM |
497 | $$.tokens = XRESIZEVEC (struct typed_stoken, |
498 | $$.tokens, $$.len); | |
a766d390 | 499 | |
224c3ddb | 500 | p = (char *) malloc ($3.length + 1); |
a766d390 DE |
501 | memcpy (p, $3.ptr, $3.length + 1); |
502 | ||
503 | $$.tokens[$$.len - 1].type = $3.type; | |
504 | $$.tokens[$$.len - 1].length = $3.length; | |
505 | $$.tokens[$$.len - 1].ptr = p; | |
506 | } | |
507 | ; | |
508 | ||
509 | exp : string_exp %prec ABOVE_COMMA | |
510 | { | |
511 | int i; | |
512 | ||
410a0ff2 SDJ |
513 | write_exp_string_vector (pstate, 0 /*always utf8*/, |
514 | &$1); | |
a766d390 DE |
515 | for (i = 0; i < $1.len; ++i) |
516 | free ($1.tokens[i].ptr); | |
517 | free ($1.tokens); | |
518 | } | |
519 | ; | |
520 | ||
521 | exp : TRUE_KEYWORD | |
410a0ff2 SDJ |
522 | { write_exp_elt_opcode (pstate, OP_BOOL); |
523 | write_exp_elt_longcst (pstate, (LONGEST) $1); | |
524 | write_exp_elt_opcode (pstate, OP_BOOL); } | |
a766d390 DE |
525 | ; |
526 | ||
527 | exp : FALSE_KEYWORD | |
410a0ff2 SDJ |
528 | { write_exp_elt_opcode (pstate, OP_BOOL); |
529 | write_exp_elt_longcst (pstate, (LONGEST) $1); | |
530 | write_exp_elt_opcode (pstate, OP_BOOL); } | |
a766d390 DE |
531 | ; |
532 | ||
533 | variable: name_not_typename ENTRY | |
d12307c1 | 534 | { struct symbol *sym = $1.sym.symbol; |
a766d390 DE |
535 | |
536 | if (sym == NULL | |
537 | || !SYMBOL_IS_ARGUMENT (sym) | |
538 | || !symbol_read_needs_frame (sym)) | |
539 | error (_("@entry can be used only for function " | |
540 | "parameters, not for \"%s\""), | |
541 | copy_name ($1.stoken)); | |
542 | ||
410a0ff2 SDJ |
543 | write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE); |
544 | write_exp_elt_sym (pstate, sym); | |
545 | write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE); | |
a766d390 DE |
546 | } |
547 | ; | |
548 | ||
549 | variable: name_not_typename | |
d12307c1 | 550 | { struct block_symbol sym = $1.sym; |
a766d390 | 551 | |
d12307c1 | 552 | if (sym.symbol) |
a766d390 | 553 | { |
d12307c1 | 554 | if (symbol_read_needs_frame (sym.symbol)) |
aee1fcdf | 555 | innermost_block.update (sym); |
a766d390 | 556 | |
410a0ff2 | 557 | write_exp_elt_opcode (pstate, OP_VAR_VALUE); |
63e43d3a | 558 | write_exp_elt_block (pstate, sym.block); |
d12307c1 | 559 | write_exp_elt_sym (pstate, sym.symbol); |
410a0ff2 | 560 | write_exp_elt_opcode (pstate, OP_VAR_VALUE); |
a766d390 DE |
561 | } |
562 | else if ($1.is_a_field_of_this) | |
563 | { | |
564 | /* TODO(dje): Can we get here? | |
565 | E.g., via a mix of c++ and go? */ | |
566 | gdb_assert_not_reached ("go with `this' field"); | |
567 | } | |
568 | else | |
569 | { | |
7c7b6655 | 570 | struct bound_minimal_symbol msymbol; |
a766d390 DE |
571 | char *arg = copy_name ($1.stoken); |
572 | ||
573 | msymbol = | |
7c7b6655 TT |
574 | lookup_bound_minimal_symbol (arg); |
575 | if (msymbol.minsym != NULL) | |
410a0ff2 | 576 | write_exp_msymbol (pstate, msymbol); |
a766d390 DE |
577 | else if (!have_full_symbols () |
578 | && !have_partial_symbols ()) | |
579 | error (_("No symbol table is loaded. " | |
580 | "Use the \"file\" command.")); | |
581 | else | |
582 | error (_("No symbol \"%s\" in current context."), | |
583 | copy_name ($1.stoken)); | |
584 | } | |
585 | } | |
586 | ; | |
587 | ||
588 | /* TODO | |
589 | method_exp: PACKAGENAME '.' name '.' name | |
590 | { | |
591 | } | |
592 | ; | |
593 | */ | |
594 | ||
595 | type /* Implements (approximately): [*] type-specifier */ | |
596 | : '*' type | |
597 | { $$ = lookup_pointer_type ($2); } | |
598 | | TYPENAME | |
599 | { $$ = $1.type; } | |
600 | /* | |
601 | | STRUCT_KEYWORD name | |
602 | { $$ = lookup_struct (copy_name ($2), | |
603 | expression_context_block); } | |
604 | */ | |
605 | | BYTE_KEYWORD | |
410a0ff2 | 606 | { $$ = builtin_go_type (parse_gdbarch (pstate)) |
a766d390 DE |
607 | ->builtin_uint8; } |
608 | ; | |
609 | ||
610 | /* TODO | |
611 | name : NAME { $$ = $1.stoken; } | |
612 | | TYPENAME { $$ = $1.stoken; } | |
613 | | NAME_OR_INT { $$ = $1.stoken; } | |
614 | ; | |
615 | */ | |
616 | ||
617 | name_not_typename | |
618 | : NAME | |
619 | /* These would be useful if name_not_typename was useful, but it is just | |
620 | a fake for "variable", so these cause reduce/reduce conflicts because | |
621 | the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable, | |
622 | =exp) or just an exp. If name_not_typename was ever used in an lvalue | |
623 | context where only a name could occur, this might be useful. | |
624 | | NAME_OR_INT | |
625 | */ | |
626 | ; | |
627 | ||
628 | %% | |
629 | ||
a766d390 DE |
630 | /* Take care of parsing a number (anything that starts with a digit). |
631 | Set yylval and return the token type; update lexptr. | |
632 | LEN is the number of characters in it. */ | |
633 | ||
634 | /* FIXME: Needs some error checking for the float case. */ | |
635 | /* FIXME(dje): IWBN to use c-exp.y's parse_number if we could. | |
636 | That will require moving the guts into a function that we both call | |
637 | as our YYSTYPE is different than c-exp.y's */ | |
638 | ||
639 | static int | |
410a0ff2 SDJ |
640 | parse_number (struct parser_state *par_state, |
641 | const char *p, int len, int parsed_float, YYSTYPE *putithere) | |
a766d390 DE |
642 | { |
643 | /* FIXME: Shouldn't these be unsigned? We don't deal with negative values | |
644 | here, and we do kind of silly things like cast to unsigned. */ | |
645 | LONGEST n = 0; | |
646 | LONGEST prevn = 0; | |
647 | ULONGEST un; | |
648 | ||
649 | int i = 0; | |
650 | int c; | |
651 | int base = input_radix; | |
652 | int unsigned_p = 0; | |
653 | ||
654 | /* Number of "L" suffixes encountered. */ | |
655 | int long_p = 0; | |
656 | ||
657 | /* We have found a "L" or "U" suffix. */ | |
658 | int found_suffix = 0; | |
659 | ||
660 | ULONGEST high_bit; | |
661 | struct type *signed_type; | |
662 | struct type *unsigned_type; | |
663 | ||
664 | if (parsed_float) | |
665 | { | |
edd079d9 UW |
666 | const struct builtin_go_type *builtin_go_types |
667 | = builtin_go_type (parse_gdbarch (par_state)); | |
668 | ||
669 | /* Handle suffixes: 'f' for float32, 'l' for long double. | |
670 | FIXME: This appears to be an extension -- do we want this? */ | |
671 | if (len >= 1 && tolower (p[len - 1]) == 'f') | |
672 | { | |
673 | putithere->typed_val_float.type | |
674 | = builtin_go_types->builtin_float32; | |
675 | len--; | |
676 | } | |
677 | else if (len >= 1 && tolower (p[len - 1]) == 'l') | |
678 | { | |
679 | putithere->typed_val_float.type | |
680 | = parse_type (par_state)->builtin_long_double; | |
681 | len--; | |
682 | } | |
683 | /* Default type for floating-point literals is float64. */ | |
684 | else | |
685 | { | |
686 | putithere->typed_val_float.type | |
687 | = builtin_go_types->builtin_float64; | |
688 | } | |
689 | ||
690 | if (!parse_float (p, len, | |
691 | putithere->typed_val_float.type, | |
692 | putithere->typed_val_float.val)) | |
693 | return ERROR; | |
a766d390 DE |
694 | return FLOAT; |
695 | } | |
696 | ||
697 | /* Handle base-switching prefixes 0x, 0t, 0d, 0. */ | |
698 | if (p[0] == '0') | |
699 | switch (p[1]) | |
700 | { | |
701 | case 'x': | |
702 | case 'X': | |
703 | if (len >= 3) | |
704 | { | |
705 | p += 2; | |
706 | base = 16; | |
707 | len -= 2; | |
708 | } | |
709 | break; | |
710 | ||
711 | case 'b': | |
712 | case 'B': | |
713 | if (len >= 3) | |
714 | { | |
715 | p += 2; | |
716 | base = 2; | |
717 | len -= 2; | |
718 | } | |
719 | break; | |
720 | ||
721 | case 't': | |
722 | case 'T': | |
723 | case 'd': | |
724 | case 'D': | |
725 | if (len >= 3) | |
726 | { | |
727 | p += 2; | |
728 | base = 10; | |
729 | len -= 2; | |
730 | } | |
731 | break; | |
732 | ||
733 | default: | |
734 | base = 8; | |
735 | break; | |
736 | } | |
737 | ||
738 | while (len-- > 0) | |
739 | { | |
740 | c = *p++; | |
741 | if (c >= 'A' && c <= 'Z') | |
742 | c += 'a' - 'A'; | |
743 | if (c != 'l' && c != 'u') | |
744 | n *= base; | |
745 | if (c >= '0' && c <= '9') | |
746 | { | |
747 | if (found_suffix) | |
748 | return ERROR; | |
749 | n += i = c - '0'; | |
750 | } | |
751 | else | |
752 | { | |
753 | if (base > 10 && c >= 'a' && c <= 'f') | |
754 | { | |
755 | if (found_suffix) | |
756 | return ERROR; | |
757 | n += i = c - 'a' + 10; | |
758 | } | |
759 | else if (c == 'l') | |
760 | { | |
761 | ++long_p; | |
762 | found_suffix = 1; | |
763 | } | |
764 | else if (c == 'u') | |
765 | { | |
766 | unsigned_p = 1; | |
767 | found_suffix = 1; | |
768 | } | |
769 | else | |
770 | return ERROR; /* Char not a digit */ | |
771 | } | |
772 | if (i >= base) | |
773 | return ERROR; /* Invalid digit in this base. */ | |
774 | ||
775 | /* Portably test for overflow (only works for nonzero values, so make | |
776 | a second check for zero). FIXME: Can't we just make n and prevn | |
777 | unsigned and avoid this? */ | |
778 | if (c != 'l' && c != 'u' && (prevn >= n) && n != 0) | |
779 | unsigned_p = 1; /* Try something unsigned. */ | |
780 | ||
781 | /* Portably test for unsigned overflow. | |
782 | FIXME: This check is wrong; for example it doesn't find overflow | |
783 | on 0x123456789 when LONGEST is 32 bits. */ | |
784 | if (c != 'l' && c != 'u' && n != 0) | |
785 | { | |
786 | if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n)) | |
787 | error (_("Numeric constant too large.")); | |
788 | } | |
789 | prevn = n; | |
790 | } | |
791 | ||
792 | /* An integer constant is an int, a long, or a long long. An L | |
793 | suffix forces it to be long; an LL suffix forces it to be long | |
794 | long. If not forced to a larger size, it gets the first type of | |
795 | the above that it fits in. To figure out whether it fits, we | |
796 | shift it right and see whether anything remains. Note that we | |
797 | can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one | |
798 | operation, because many compilers will warn about such a shift | |
799 | (which always produces a zero result). Sometimes gdbarch_int_bit | |
800 | or gdbarch_long_bit will be that big, sometimes not. To deal with | |
801 | the case where it is we just always shift the value more than | |
802 | once, with fewer bits each time. */ | |
803 | ||
804 | un = (ULONGEST)n >> 2; | |
805 | if (long_p == 0 | |
410a0ff2 | 806 | && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0) |
a766d390 | 807 | { |
410a0ff2 SDJ |
808 | high_bit |
809 | = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1); | |
a766d390 DE |
810 | |
811 | /* A large decimal (not hex or octal) constant (between INT_MAX | |
812 | and UINT_MAX) is a long or unsigned long, according to ANSI, | |
813 | never an unsigned int, but this code treats it as unsigned | |
814 | int. This probably should be fixed. GCC gives a warning on | |
815 | such constants. */ | |
816 | ||
410a0ff2 SDJ |
817 | unsigned_type = parse_type (par_state)->builtin_unsigned_int; |
818 | signed_type = parse_type (par_state)->builtin_int; | |
a766d390 DE |
819 | } |
820 | else if (long_p <= 1 | |
410a0ff2 | 821 | && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0) |
a766d390 | 822 | { |
410a0ff2 SDJ |
823 | high_bit |
824 | = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1); | |
825 | unsigned_type = parse_type (par_state)->builtin_unsigned_long; | |
826 | signed_type = parse_type (par_state)->builtin_long; | |
a766d390 DE |
827 | } |
828 | else | |
829 | { | |
830 | int shift; | |
831 | if (sizeof (ULONGEST) * HOST_CHAR_BIT | |
410a0ff2 | 832 | < gdbarch_long_long_bit (parse_gdbarch (par_state))) |
a766d390 DE |
833 | /* A long long does not fit in a LONGEST. */ |
834 | shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1); | |
835 | else | |
410a0ff2 | 836 | shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1); |
a766d390 | 837 | high_bit = (ULONGEST) 1 << shift; |
410a0ff2 SDJ |
838 | unsigned_type = parse_type (par_state)->builtin_unsigned_long_long; |
839 | signed_type = parse_type (par_state)->builtin_long_long; | |
a766d390 DE |
840 | } |
841 | ||
842 | putithere->typed_val_int.val = n; | |
843 | ||
844 | /* If the high bit of the worked out type is set then this number | |
845 | has to be unsigned. */ | |
846 | ||
847 | if (unsigned_p || (n & high_bit)) | |
848 | { | |
849 | putithere->typed_val_int.type = unsigned_type; | |
850 | } | |
851 | else | |
852 | { | |
853 | putithere->typed_val_int.type = signed_type; | |
854 | } | |
855 | ||
856 | return INT; | |
857 | } | |
858 | ||
859 | /* Temporary obstack used for holding strings. */ | |
860 | static struct obstack tempbuf; | |
861 | static int tempbuf_init; | |
862 | ||
863 | /* Parse a string or character literal from TOKPTR. The string or | |
864 | character may be wide or unicode. *OUTPTR is set to just after the | |
865 | end of the literal in the input string. The resulting token is | |
866 | stored in VALUE. This returns a token value, either STRING or | |
867 | CHAR, depending on what was parsed. *HOST_CHARS is set to the | |
868 | number of host characters in the literal. */ | |
869 | ||
870 | static int | |
d7561cbb KS |
871 | parse_string_or_char (const char *tokptr, const char **outptr, |
872 | struct typed_stoken *value, int *host_chars) | |
a766d390 DE |
873 | { |
874 | int quote; | |
875 | ||
876 | /* Build the gdb internal form of the input string in tempbuf. Note | |
877 | that the buffer is null byte terminated *only* for the | |
878 | convenience of debugging gdb itself and printing the buffer | |
879 | contents when the buffer contains no embedded nulls. Gdb does | |
880 | not depend upon the buffer being null byte terminated, it uses | |
881 | the length string instead. This allows gdb to handle C strings | |
882 | (as well as strings in other languages) with embedded null | |
883 | bytes */ | |
884 | ||
885 | if (!tempbuf_init) | |
886 | tempbuf_init = 1; | |
887 | else | |
888 | obstack_free (&tempbuf, NULL); | |
889 | obstack_init (&tempbuf); | |
890 | ||
891 | /* Skip the quote. */ | |
892 | quote = *tokptr; | |
893 | ++tokptr; | |
894 | ||
895 | *host_chars = 0; | |
896 | ||
897 | while (*tokptr) | |
898 | { | |
899 | char c = *tokptr; | |
900 | if (c == '\\') | |
901 | { | |
902 | ++tokptr; | |
903 | *host_chars += c_parse_escape (&tokptr, &tempbuf); | |
904 | } | |
905 | else if (c == quote) | |
906 | break; | |
907 | else | |
908 | { | |
909 | obstack_1grow (&tempbuf, c); | |
910 | ++tokptr; | |
911 | /* FIXME: this does the wrong thing with multi-byte host | |
912 | characters. We could use mbrlen here, but that would | |
913 | make "set host-charset" a bit less useful. */ | |
914 | ++*host_chars; | |
915 | } | |
916 | } | |
917 | ||
918 | if (*tokptr != quote) | |
919 | { | |
920 | if (quote == '"') | |
921 | error (_("Unterminated string in expression.")); | |
922 | else | |
923 | error (_("Unmatched single quote.")); | |
924 | } | |
925 | ++tokptr; | |
926 | ||
927 | value->type = C_STRING | (quote == '\'' ? C_CHAR : 0); /*FIXME*/ | |
79f33898 | 928 | value->ptr = (char *) obstack_base (&tempbuf); |
a766d390 DE |
929 | value->length = obstack_object_size (&tempbuf); |
930 | ||
931 | *outptr = tokptr; | |
932 | ||
933 | return quote == '\'' ? CHAR : STRING; | |
934 | } | |
935 | ||
936 | struct token | |
937 | { | |
a121b7c1 | 938 | const char *oper; |
a766d390 DE |
939 | int token; |
940 | enum exp_opcode opcode; | |
941 | }; | |
942 | ||
943 | static const struct token tokentab3[] = | |
944 | { | |
945 | {">>=", ASSIGN_MODIFY, BINOP_RSH}, | |
946 | {"<<=", ASSIGN_MODIFY, BINOP_LSH}, | |
947 | /*{"&^=", ASSIGN_MODIFY, BINOP_BITWISE_ANDNOT}, TODO */ | |
948 | {"...", DOTDOTDOT, OP_NULL}, | |
949 | }; | |
950 | ||
951 | static const struct token tokentab2[] = | |
952 | { | |
953 | {"+=", ASSIGN_MODIFY, BINOP_ADD}, | |
954 | {"-=", ASSIGN_MODIFY, BINOP_SUB}, | |
955 | {"*=", ASSIGN_MODIFY, BINOP_MUL}, | |
956 | {"/=", ASSIGN_MODIFY, BINOP_DIV}, | |
957 | {"%=", ASSIGN_MODIFY, BINOP_REM}, | |
958 | {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR}, | |
959 | {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND}, | |
960 | {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR}, | |
961 | {"++", INCREMENT, BINOP_END}, | |
962 | {"--", DECREMENT, BINOP_END}, | |
963 | /*{"->", RIGHT_ARROW, BINOP_END}, Doesn't exist in Go. */ | |
964 | {"<-", LEFT_ARROW, BINOP_END}, | |
965 | {"&&", ANDAND, BINOP_END}, | |
966 | {"||", OROR, BINOP_END}, | |
967 | {"<<", LSH, BINOP_END}, | |
968 | {">>", RSH, BINOP_END}, | |
969 | {"==", EQUAL, BINOP_END}, | |
970 | {"!=", NOTEQUAL, BINOP_END}, | |
971 | {"<=", LEQ, BINOP_END}, | |
972 | {">=", GEQ, BINOP_END}, | |
973 | /*{"&^", ANDNOT, BINOP_END}, TODO */ | |
974 | }; | |
975 | ||
976 | /* Identifier-like tokens. */ | |
977 | static const struct token ident_tokens[] = | |
978 | { | |
979 | {"true", TRUE_KEYWORD, OP_NULL}, | |
980 | {"false", FALSE_KEYWORD, OP_NULL}, | |
981 | {"nil", NIL_KEYWORD, OP_NULL}, | |
982 | {"const", CONST_KEYWORD, OP_NULL}, | |
983 | {"struct", STRUCT_KEYWORD, OP_NULL}, | |
984 | {"type", TYPE_KEYWORD, OP_NULL}, | |
985 | {"interface", INTERFACE_KEYWORD, OP_NULL}, | |
986 | {"chan", CHAN_KEYWORD, OP_NULL}, | |
987 | {"byte", BYTE_KEYWORD, OP_NULL}, /* An alias of uint8. */ | |
988 | {"len", LEN_KEYWORD, OP_NULL}, | |
989 | {"cap", CAP_KEYWORD, OP_NULL}, | |
990 | {"new", NEW_KEYWORD, OP_NULL}, | |
991 | {"iota", IOTA_KEYWORD, OP_NULL}, | |
992 | }; | |
993 | ||
994 | /* This is set if a NAME token appeared at the very end of the input | |
995 | string, with no whitespace separating the name from the EOF. This | |
996 | is used only when parsing to do field name completion. */ | |
997 | static int saw_name_at_eof; | |
998 | ||
999 | /* This is set if the previously-returned token was a structure | |
1000 | operator -- either '.' or ARROW. This is used only when parsing to | |
1001 | do field name completion. */ | |
1002 | static int last_was_structop; | |
1003 | ||
1004 | /* Read one token, getting characters through lexptr. */ | |
1005 | ||
1006 | static int | |
410a0ff2 | 1007 | lex_one_token (struct parser_state *par_state) |
a766d390 DE |
1008 | { |
1009 | int c; | |
1010 | int namelen; | |
1011 | unsigned int i; | |
d7561cbb | 1012 | const char *tokstart; |
a766d390 DE |
1013 | int saw_structop = last_was_structop; |
1014 | char *copy; | |
1015 | ||
1016 | last_was_structop = 0; | |
1017 | ||
1018 | retry: | |
1019 | ||
1020 | prev_lexptr = lexptr; | |
1021 | ||
1022 | tokstart = lexptr; | |
1023 | /* See if it is a special token of length 3. */ | |
1024 | for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++) | |
fe978cb0 | 1025 | if (strncmp (tokstart, tokentab3[i].oper, 3) == 0) |
a766d390 DE |
1026 | { |
1027 | lexptr += 3; | |
1028 | yylval.opcode = tokentab3[i].opcode; | |
1029 | return tokentab3[i].token; | |
1030 | } | |
1031 | ||
1032 | /* See if it is a special token of length 2. */ | |
1033 | for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++) | |
fe978cb0 | 1034 | if (strncmp (tokstart, tokentab2[i].oper, 2) == 0) |
a766d390 DE |
1035 | { |
1036 | lexptr += 2; | |
1037 | yylval.opcode = tokentab2[i].opcode; | |
1038 | /* NOTE: -> doesn't exist in Go, so we don't need to watch for | |
1039 | setting last_was_structop here. */ | |
1040 | return tokentab2[i].token; | |
1041 | } | |
1042 | ||
1043 | switch (c = *tokstart) | |
1044 | { | |
1045 | case 0: | |
1046 | if (saw_name_at_eof) | |
1047 | { | |
1048 | saw_name_at_eof = 0; | |
1049 | return COMPLETE; | |
1050 | } | |
1051 | else if (saw_structop) | |
1052 | return COMPLETE; | |
1053 | else | |
1054 | return 0; | |
1055 | ||
1056 | case ' ': | |
1057 | case '\t': | |
1058 | case '\n': | |
1059 | lexptr++; | |
1060 | goto retry; | |
1061 | ||
1062 | case '[': | |
1063 | case '(': | |
1064 | paren_depth++; | |
1065 | lexptr++; | |
1066 | return c; | |
1067 | ||
1068 | case ']': | |
1069 | case ')': | |
1070 | if (paren_depth == 0) | |
1071 | return 0; | |
1072 | paren_depth--; | |
1073 | lexptr++; | |
1074 | return c; | |
1075 | ||
1076 | case ',': | |
1077 | if (comma_terminates | |
1078 | && paren_depth == 0) | |
1079 | return 0; | |
1080 | lexptr++; | |
1081 | return c; | |
1082 | ||
1083 | case '.': | |
1084 | /* Might be a floating point number. */ | |
1085 | if (lexptr[1] < '0' || lexptr[1] > '9') | |
1086 | { | |
155da517 | 1087 | if (parse_completion) |
a766d390 DE |
1088 | last_was_structop = 1; |
1089 | goto symbol; /* Nope, must be a symbol. */ | |
1090 | } | |
1091 | /* FALL THRU into number case. */ | |
1092 | ||
1093 | case '0': | |
1094 | case '1': | |
1095 | case '2': | |
1096 | case '3': | |
1097 | case '4': | |
1098 | case '5': | |
1099 | case '6': | |
1100 | case '7': | |
1101 | case '8': | |
1102 | case '9': | |
1103 | { | |
1104 | /* It's a number. */ | |
1105 | int got_dot = 0, got_e = 0, toktype; | |
d7561cbb | 1106 | const char *p = tokstart; |
a766d390 DE |
1107 | int hex = input_radix > 10; |
1108 | ||
1109 | if (c == '0' && (p[1] == 'x' || p[1] == 'X')) | |
1110 | { | |
1111 | p += 2; | |
1112 | hex = 1; | |
1113 | } | |
1114 | ||
1115 | for (;; ++p) | |
1116 | { | |
1117 | /* This test includes !hex because 'e' is a valid hex digit | |
1118 | and thus does not indicate a floating point number when | |
1119 | the radix is hex. */ | |
1120 | if (!hex && !got_e && (*p == 'e' || *p == 'E')) | |
1121 | got_dot = got_e = 1; | |
1122 | /* This test does not include !hex, because a '.' always indicates | |
1123 | a decimal floating point number regardless of the radix. */ | |
1124 | else if (!got_dot && *p == '.') | |
1125 | got_dot = 1; | |
1126 | else if (got_e && (p[-1] == 'e' || p[-1] == 'E') | |
1127 | && (*p == '-' || *p == '+')) | |
1128 | /* This is the sign of the exponent, not the end of the | |
1129 | number. */ | |
1130 | continue; | |
1131 | /* We will take any letters or digits. parse_number will | |
1132 | complain if past the radix, or if L or U are not final. */ | |
1133 | else if ((*p < '0' || *p > '9') | |
1134 | && ((*p < 'a' || *p > 'z') | |
1135 | && (*p < 'A' || *p > 'Z'))) | |
1136 | break; | |
1137 | } | |
410a0ff2 SDJ |
1138 | toktype = parse_number (par_state, tokstart, p - tokstart, |
1139 | got_dot|got_e, &yylval); | |
a766d390 DE |
1140 | if (toktype == ERROR) |
1141 | { | |
1142 | char *err_copy = (char *) alloca (p - tokstart + 1); | |
1143 | ||
1144 | memcpy (err_copy, tokstart, p - tokstart); | |
1145 | err_copy[p - tokstart] = 0; | |
1146 | error (_("Invalid number \"%s\"."), err_copy); | |
1147 | } | |
1148 | lexptr = p; | |
1149 | return toktype; | |
1150 | } | |
1151 | ||
1152 | case '@': | |
1153 | { | |
d7561cbb | 1154 | const char *p = &tokstart[1]; |
a766d390 DE |
1155 | size_t len = strlen ("entry"); |
1156 | ||
1157 | while (isspace (*p)) | |
1158 | p++; | |
1159 | if (strncmp (p, "entry", len) == 0 && !isalnum (p[len]) | |
1160 | && p[len] != '_') | |
1161 | { | |
1162 | lexptr = &p[len]; | |
1163 | return ENTRY; | |
1164 | } | |
1165 | } | |
1166 | /* FALLTHRU */ | |
1167 | case '+': | |
1168 | case '-': | |
1169 | case '*': | |
1170 | case '/': | |
1171 | case '%': | |
1172 | case '|': | |
1173 | case '&': | |
1174 | case '^': | |
1175 | case '~': | |
1176 | case '!': | |
1177 | case '<': | |
1178 | case '>': | |
1179 | case '?': | |
1180 | case ':': | |
1181 | case '=': | |
1182 | case '{': | |
1183 | case '}': | |
1184 | symbol: | |
1185 | lexptr++; | |
1186 | return c; | |
1187 | ||
1188 | case '\'': | |
1189 | case '"': | |
1190 | case '`': | |
1191 | { | |
1192 | int host_len; | |
1193 | int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval, | |
1194 | &host_len); | |
1195 | if (result == CHAR) | |
1196 | { | |
1197 | if (host_len == 0) | |
1198 | error (_("Empty character constant.")); | |
1199 | else if (host_len > 2 && c == '\'') | |
1200 | { | |
1201 | ++tokstart; | |
1202 | namelen = lexptr - tokstart - 1; | |
1203 | goto tryname; | |
1204 | } | |
1205 | else if (host_len > 1) | |
1206 | error (_("Invalid character constant.")); | |
1207 | } | |
1208 | return result; | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | if (!(c == '_' || c == '$' | |
1213 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) | |
1214 | /* We must have come across a bad character (e.g. ';'). */ | |
1215 | error (_("Invalid character '%c' in expression."), c); | |
1216 | ||
1217 | /* It's a name. See how long it is. */ | |
1218 | namelen = 0; | |
1219 | for (c = tokstart[namelen]; | |
1220 | (c == '_' || c == '$' || (c >= '0' && c <= '9') | |
1221 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));) | |
1222 | { | |
1223 | c = tokstart[++namelen]; | |
1224 | } | |
1225 | ||
1226 | /* The token "if" terminates the expression and is NOT removed from | |
1227 | the input stream. It doesn't count if it appears in the | |
1228 | expansion of a macro. */ | |
1229 | if (namelen == 2 | |
1230 | && tokstart[0] == 'i' | |
1231 | && tokstart[1] == 'f') | |
1232 | { | |
1233 | return 0; | |
1234 | } | |
1235 | ||
1236 | /* For the same reason (breakpoint conditions), "thread N" | |
1237 | terminates the expression. "thread" could be an identifier, but | |
1238 | an identifier is never followed by a number without intervening | |
1239 | punctuation. | |
1240 | Handle abbreviations of these, similarly to | |
1241 | breakpoint.c:find_condition_and_thread. | |
1242 | TODO: Watch for "goroutine" here? */ | |
1243 | if (namelen >= 1 | |
1244 | && strncmp (tokstart, "thread", namelen) == 0 | |
1245 | && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')) | |
1246 | { | |
d7561cbb KS |
1247 | const char *p = tokstart + namelen + 1; |
1248 | ||
a766d390 DE |
1249 | while (*p == ' ' || *p == '\t') |
1250 | p++; | |
1251 | if (*p >= '0' && *p <= '9') | |
1252 | return 0; | |
1253 | } | |
1254 | ||
1255 | lexptr += namelen; | |
1256 | ||
1257 | tryname: | |
1258 | ||
1259 | yylval.sval.ptr = tokstart; | |
1260 | yylval.sval.length = namelen; | |
1261 | ||
1262 | /* Catch specific keywords. */ | |
1263 | copy = copy_name (yylval.sval); | |
1264 | for (i = 0; i < sizeof (ident_tokens) / sizeof (ident_tokens[0]); i++) | |
fe978cb0 | 1265 | if (strcmp (copy, ident_tokens[i].oper) == 0) |
a766d390 DE |
1266 | { |
1267 | /* It is ok to always set this, even though we don't always | |
1268 | strictly need to. */ | |
1269 | yylval.opcode = ident_tokens[i].opcode; | |
1270 | return ident_tokens[i].token; | |
1271 | } | |
1272 | ||
1273 | if (*tokstart == '$') | |
1274 | return DOLLAR_VARIABLE; | |
1275 | ||
155da517 | 1276 | if (parse_completion && *lexptr == '\0') |
a766d390 DE |
1277 | saw_name_at_eof = 1; |
1278 | return NAME; | |
1279 | } | |
1280 | ||
1281 | /* An object of this type is pushed on a FIFO by the "outer" lexer. */ | |
1282 | typedef struct | |
1283 | { | |
1284 | int token; | |
1285 | YYSTYPE value; | |
1286 | } token_and_value; | |
1287 | ||
1288 | DEF_VEC_O (token_and_value); | |
1289 | ||
1290 | /* A FIFO of tokens that have been read but not yet returned to the | |
1291 | parser. */ | |
1292 | static VEC (token_and_value) *token_fifo; | |
1293 | ||
1294 | /* Non-zero if the lexer should return tokens from the FIFO. */ | |
1295 | static int popping; | |
1296 | ||
1297 | /* Temporary storage for yylex; this holds symbol names as they are | |
1298 | built up. */ | |
8268c778 | 1299 | static auto_obstack name_obstack; |
a766d390 DE |
1300 | |
1301 | /* Build "package.name" in name_obstack. | |
1302 | For convenience of the caller, the name is NUL-terminated, | |
1303 | but the NUL is not included in the recorded length. */ | |
1304 | ||
1305 | static struct stoken | |
1306 | build_packaged_name (const char *package, int package_len, | |
1307 | const char *name, int name_len) | |
1308 | { | |
1309 | struct stoken result; | |
1310 | ||
8268c778 | 1311 | name_obstack.clear (); |
a766d390 DE |
1312 | obstack_grow (&name_obstack, package, package_len); |
1313 | obstack_grow_str (&name_obstack, "."); | |
1314 | obstack_grow (&name_obstack, name, name_len); | |
1315 | obstack_grow (&name_obstack, "", 1); | |
79f33898 | 1316 | result.ptr = (char *) obstack_base (&name_obstack); |
a766d390 DE |
1317 | result.length = obstack_object_size (&name_obstack) - 1; |
1318 | ||
1319 | return result; | |
1320 | } | |
1321 | ||
1322 | /* Return non-zero if NAME is a package name. | |
1323 | BLOCK is the scope in which to interpret NAME; this can be NULL | |
1324 | to mean the global scope. */ | |
1325 | ||
1326 | static int | |
270140bd | 1327 | package_name_p (const char *name, const struct block *block) |
a766d390 DE |
1328 | { |
1329 | struct symbol *sym; | |
1993b719 | 1330 | struct field_of_this_result is_a_field_of_this; |
a766d390 | 1331 | |
d12307c1 | 1332 | sym = lookup_symbol (name, block, STRUCT_DOMAIN, &is_a_field_of_this).symbol; |
a766d390 DE |
1333 | |
1334 | if (sym | |
1335 | && SYMBOL_CLASS (sym) == LOC_TYPEDEF | |
1336 | && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_MODULE) | |
1337 | return 1; | |
1338 | ||
1339 | return 0; | |
1340 | } | |
1341 | ||
1342 | /* Classify a (potential) function in the "unsafe" package. | |
1343 | We fold these into "keywords" to keep things simple, at least until | |
1344 | something more complex is warranted. */ | |
1345 | ||
1346 | static int | |
1347 | classify_unsafe_function (struct stoken function_name) | |
1348 | { | |
1349 | char *copy = copy_name (function_name); | |
1350 | ||
1351 | if (strcmp (copy, "Sizeof") == 0) | |
1352 | { | |
1353 | yylval.sval = function_name; | |
1354 | return SIZEOF_KEYWORD; | |
1355 | } | |
1356 | ||
1357 | error (_("Unknown function in `unsafe' package: %s"), copy); | |
1358 | } | |
1359 | ||
1360 | /* Classify token(s) "name1.name2" where name1 is known to be a package. | |
1361 | The contents of the token are in `yylval'. | |
1362 | Updates yylval and returns the new token type. | |
1363 | ||
1364 | The result is one of NAME, NAME_OR_INT, or TYPENAME. */ | |
1365 | ||
1366 | static int | |
270140bd | 1367 | classify_packaged_name (const struct block *block) |
a766d390 DE |
1368 | { |
1369 | char *copy; | |
d12307c1 | 1370 | struct block_symbol sym; |
1993b719 | 1371 | struct field_of_this_result is_a_field_of_this; |
a766d390 DE |
1372 | |
1373 | copy = copy_name (yylval.sval); | |
1374 | ||
1375 | sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this); | |
1376 | ||
d12307c1 | 1377 | if (sym.symbol) |
a766d390 DE |
1378 | { |
1379 | yylval.ssym.sym = sym; | |
1993b719 | 1380 | yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL; |
a766d390 DE |
1381 | } |
1382 | ||
1383 | return NAME; | |
1384 | } | |
1385 | ||
1386 | /* Classify a NAME token. | |
1387 | The contents of the token are in `yylval'. | |
1388 | Updates yylval and returns the new token type. | |
1389 | BLOCK is the block in which lookups start; this can be NULL | |
1390 | to mean the global scope. | |
1391 | ||
1392 | The result is one of NAME, NAME_OR_INT, or TYPENAME. */ | |
1393 | ||
1394 | static int | |
410a0ff2 | 1395 | classify_name (struct parser_state *par_state, const struct block *block) |
a766d390 DE |
1396 | { |
1397 | struct type *type; | |
d12307c1 | 1398 | struct block_symbol sym; |
a766d390 | 1399 | char *copy; |
1993b719 | 1400 | struct field_of_this_result is_a_field_of_this; |
a766d390 DE |
1401 | |
1402 | copy = copy_name (yylval.sval); | |
1403 | ||
1404 | /* Try primitive types first so they win over bad/weird debug info. */ | |
46b0da17 DE |
1405 | type = language_lookup_primitive_type (parse_language (par_state), |
1406 | parse_gdbarch (par_state), | |
1407 | copy); | |
a766d390 DE |
1408 | if (type != NULL) |
1409 | { | |
1410 | /* NOTE: We take advantage of the fact that yylval coming in was a | |
1411 | NAME, and that struct ttype is a compatible extension of struct | |
1412 | stoken, so yylval.tsym.stoken is already filled in. */ | |
1413 | yylval.tsym.type = type; | |
1414 | return TYPENAME; | |
1415 | } | |
1416 | ||
1417 | /* TODO: What about other types? */ | |
1418 | ||
1419 | sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this); | |
1420 | ||
d12307c1 | 1421 | if (sym.symbol) |
a766d390 DE |
1422 | { |
1423 | yylval.ssym.sym = sym; | |
1993b719 | 1424 | yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL; |
a766d390 DE |
1425 | return NAME; |
1426 | } | |
1427 | ||
1428 | /* If we didn't find a symbol, look again in the current package. | |
1429 | This is to, e.g., make "p global_var" work without having to specify | |
1430 | the package name. We intentionally only looks for objects in the | |
1431 | current package. */ | |
1432 | ||
1433 | { | |
1434 | char *current_package_name = go_block_package_name (block); | |
1435 | ||
1436 | if (current_package_name != NULL) | |
1437 | { | |
1438 | struct stoken sval = | |
1439 | build_packaged_name (current_package_name, | |
1440 | strlen (current_package_name), | |
1441 | copy, strlen (copy)); | |
1442 | ||
1443 | xfree (current_package_name); | |
1444 | sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN, | |
1445 | &is_a_field_of_this); | |
d12307c1 | 1446 | if (sym.symbol) |
a766d390 | 1447 | { |
3929b321 | 1448 | yylval.ssym.stoken = sval; |
a766d390 | 1449 | yylval.ssym.sym = sym; |
1993b719 | 1450 | yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL; |
a766d390 DE |
1451 | return NAME; |
1452 | } | |
1453 | } | |
1454 | } | |
1455 | ||
1456 | /* Input names that aren't symbols but ARE valid hex numbers, when | |
1457 | the input radix permits them, can be names or numbers depending | |
1458 | on the parse. Note we support radixes > 16 here. */ | |
1459 | if ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10) | |
1460 | || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)) | |
1461 | { | |
1462 | YYSTYPE newlval; /* Its value is ignored. */ | |
410a0ff2 SDJ |
1463 | int hextype = parse_number (par_state, copy, yylval.sval.length, |
1464 | 0, &newlval); | |
a766d390 | 1465 | if (hextype == INT) |
3929b321 | 1466 | { |
d12307c1 PMR |
1467 | yylval.ssym.sym.symbol = NULL; |
1468 | yylval.ssym.sym.block = NULL; | |
3929b321 DE |
1469 | yylval.ssym.is_a_field_of_this = 0; |
1470 | return NAME_OR_INT; | |
1471 | } | |
a766d390 DE |
1472 | } |
1473 | ||
d12307c1 PMR |
1474 | yylval.ssym.sym.symbol = NULL; |
1475 | yylval.ssym.sym.block = NULL; | |
3929b321 | 1476 | yylval.ssym.is_a_field_of_this = 0; |
a766d390 DE |
1477 | return NAME; |
1478 | } | |
1479 | ||
1480 | /* This is taken from c-exp.y mostly to get something working. | |
1481 | The basic structure has been kept because we may yet need some of it. */ | |
1482 | ||
1483 | static int | |
1484 | yylex (void) | |
1485 | { | |
1486 | token_and_value current, next; | |
1487 | ||
1488 | if (popping && !VEC_empty (token_and_value, token_fifo)) | |
1489 | { | |
1490 | token_and_value tv = *VEC_index (token_and_value, token_fifo, 0); | |
1491 | VEC_ordered_remove (token_and_value, token_fifo, 0); | |
1492 | yylval = tv.value; | |
1493 | /* There's no need to fall through to handle package.name | |
1494 | as that can never happen here. In theory. */ | |
1495 | return tv.token; | |
1496 | } | |
1497 | popping = 0; | |
1498 | ||
410a0ff2 | 1499 | current.token = lex_one_token (pstate); |
a766d390 DE |
1500 | |
1501 | /* TODO: Need a way to force specifying name1 as a package. | |
1502 | .name1.name2 ? */ | |
1503 | ||
1504 | if (current.token != NAME) | |
1505 | return current.token; | |
1506 | ||
1507 | /* See if we have "name1 . name2". */ | |
1508 | ||
1509 | current.value = yylval; | |
410a0ff2 | 1510 | next.token = lex_one_token (pstate); |
a766d390 DE |
1511 | next.value = yylval; |
1512 | ||
1513 | if (next.token == '.') | |
1514 | { | |
1515 | token_and_value name2; | |
1516 | ||
410a0ff2 | 1517 | name2.token = lex_one_token (pstate); |
a766d390 DE |
1518 | name2.value = yylval; |
1519 | ||
1520 | if (name2.token == NAME) | |
1521 | { | |
1522 | /* Ok, we have "name1 . name2". */ | |
a766d390 DE |
1523 | char *copy; |
1524 | ||
1525 | copy = copy_name (current.value.sval); | |
1526 | ||
1527 | if (strcmp (copy, "unsafe") == 0) | |
1528 | { | |
1529 | popping = 1; | |
1530 | return classify_unsafe_function (name2.value.sval); | |
1531 | } | |
1532 | ||
1533 | if (package_name_p (copy, expression_context_block)) | |
1534 | { | |
1535 | popping = 1; | |
1536 | yylval.sval = build_packaged_name (current.value.sval.ptr, | |
1537 | current.value.sval.length, | |
1538 | name2.value.sval.ptr, | |
1539 | name2.value.sval.length); | |
1540 | return classify_packaged_name (expression_context_block); | |
1541 | } | |
1542 | } | |
1543 | ||
1544 | VEC_safe_push (token_and_value, token_fifo, &next); | |
1545 | VEC_safe_push (token_and_value, token_fifo, &name2); | |
1546 | } | |
1547 | else | |
1548 | { | |
1549 | VEC_safe_push (token_and_value, token_fifo, &next); | |
1550 | } | |
1551 | ||
1552 | /* If we arrive here we don't have a package-qualified name. */ | |
1553 | ||
1554 | popping = 1; | |
1555 | yylval = current.value; | |
410a0ff2 | 1556 | return classify_name (pstate, expression_context_block); |
a766d390 DE |
1557 | } |
1558 | ||
1559 | int | |
410a0ff2 | 1560 | go_parse (struct parser_state *par_state) |
a766d390 | 1561 | { |
410a0ff2 | 1562 | /* Setting up the parser state. */ |
eae49211 | 1563 | scoped_restore pstate_restore = make_scoped_restore (&pstate); |
410a0ff2 SDJ |
1564 | gdb_assert (par_state != NULL); |
1565 | pstate = par_state; | |
1566 | ||
156d9eab TT |
1567 | scoped_restore restore_yydebug = make_scoped_restore (&yydebug, |
1568 | parser_debug); | |
a766d390 DE |
1569 | |
1570 | /* Initialize some state used by the lexer. */ | |
1571 | last_was_structop = 0; | |
1572 | saw_name_at_eof = 0; | |
1573 | ||
1574 | VEC_free (token_and_value, token_fifo); | |
1575 | popping = 0; | |
8268c778 | 1576 | name_obstack.clear (); |
a766d390 | 1577 | |
fef704bf | 1578 | return yyparse (); |
a766d390 DE |
1579 | } |
1580 | ||
1581 | void | |
a121b7c1 | 1582 | yyerror (const char *msg) |
a766d390 DE |
1583 | { |
1584 | if (prev_lexptr) | |
1585 | lexptr = prev_lexptr; | |
1586 | ||
1587 | error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr); | |
1588 | } |