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