go-exp.y 39 KB

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