gengtype-parse.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /* Process source files and output type information.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifdef HOST_GENERATOR_FILE
  16. #include "config.h"
  17. #define GENERATOR_FILE 1
  18. #else
  19. #include "bconfig.h"
  20. #endif
  21. #include "system.h"
  22. #include "gengtype.h"
  23. /* This is a simple recursive-descent parser which understands a subset of
  24. the C type grammar.
  25. Rule functions are suffixed _seq if they scan a sequence of items;
  26. _opt if they may consume zero tokens; _seqopt if both are true. The
  27. "consume_" prefix indicates that a sequence of tokens is parsed for
  28. syntactic correctness and then thrown away. */
  29. /* Simple one-token lookahead mechanism. */
  30. struct token
  31. {
  32. const char *value;
  33. int code;
  34. bool valid;
  35. };
  36. static struct token T;
  37. /* Retrieve the code of the current token; if there is no current token,
  38. get the next one from the lexer. */
  39. static inline int
  40. token (void)
  41. {
  42. if (!T.valid)
  43. {
  44. T.code = yylex (&T.value);
  45. T.valid = true;
  46. }
  47. return T.code;
  48. }
  49. /* Retrieve the value of the current token (if any) and mark it consumed.
  50. The next call to token() will get another token from the lexer. */
  51. static inline const char *
  52. advance (void)
  53. {
  54. T.valid = false;
  55. return T.value;
  56. }
  57. /* Diagnostics. */
  58. /* This array is indexed by the token code minus CHAR_TOKEN_OFFSET. */
  59. static const char *const token_names[] = {
  60. "GTY",
  61. "typedef",
  62. "extern",
  63. "static",
  64. "union",
  65. "struct",
  66. "enum",
  67. "...",
  68. "ptr_alias",
  69. "nested_ptr",
  70. "a param<N>_is option",
  71. "a number",
  72. "a scalar type",
  73. "an identifier",
  74. "a string constant",
  75. "a character constant",
  76. "an array declarator",
  77. "a C++ keyword to ignore"
  78. };
  79. /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
  80. static const char *const token_value_format[] = {
  81. "%s",
  82. "'%s'",
  83. "'%s'",
  84. "'%s'",
  85. "'\"%s\"'",
  86. "\"'%s'\"",
  87. "'[%s]'",
  88. "'%s'",
  89. };
  90. /* Produce a printable representation for a token defined by CODE and
  91. VALUE. This sometimes returns pointers into malloc memory and
  92. sometimes not, therefore it is unsafe to free the pointer it
  93. returns, so that memory is leaked. This does not matter, as this
  94. function is only used for diagnostics, and in a successful run of
  95. the program there will be none. */
  96. static const char *
  97. print_token (int code, const char *value)
  98. {
  99. if (code < CHAR_TOKEN_OFFSET)
  100. return xasprintf ("'%c'", code);
  101. else if (code < FIRST_TOKEN_WITH_VALUE)
  102. return xasprintf ("'%s'", token_names[code - CHAR_TOKEN_OFFSET]);
  103. else if (!value)
  104. return token_names[code - CHAR_TOKEN_OFFSET]; /* don't quote these */
  105. else
  106. return xasprintf (token_value_format[code - FIRST_TOKEN_WITH_VALUE],
  107. value);
  108. }
  109. /* Convenience wrapper around print_token which produces the printable
  110. representation of the current token. */
  111. static inline const char *
  112. print_cur_token (void)
  113. {
  114. return print_token (T.code, T.value);
  115. }
  116. /* Report a parse error on the current line, with diagnostic MSG.
  117. Behaves as standard printf with respect to additional arguments and
  118. format escapes. */
  119. static void ATTRIBUTE_PRINTF_1
  120. parse_error (const char *msg, ...)
  121. {
  122. va_list ap;
  123. fprintf (stderr, "%s:%d: parse error: ",
  124. get_input_file_name (lexer_line.file), lexer_line.line);
  125. va_start (ap, msg);
  126. vfprintf (stderr, msg, ap);
  127. va_end (ap);
  128. fputc ('\n', stderr);
  129. hit_error = true;
  130. }
  131. /* If the next token does not have code T, report a parse error; otherwise
  132. return the token's value. */
  133. static const char *
  134. require (int t)
  135. {
  136. int u = token ();
  137. const char *v = advance ();
  138. if (u != t)
  139. {
  140. parse_error ("expected %s, have %s",
  141. print_token (t, 0), print_token (u, v));
  142. return 0;
  143. }
  144. return v;
  145. }
  146. /* As per require, but do not advance. */
  147. static const char *
  148. require_without_advance (int t)
  149. {
  150. int u = token ();
  151. const char *v = T.value;
  152. if (u != t)
  153. {
  154. parse_error ("expected %s, have %s",
  155. print_token (t, 0), print_token (u, v));
  156. return 0;
  157. }
  158. return v;
  159. }
  160. /* If the next token does not have one of the codes T1 or T2, report a
  161. parse error; otherwise return the token's value. */
  162. static const char *
  163. require2 (int t1, int t2)
  164. {
  165. int u = token ();
  166. const char *v = advance ();
  167. if (u != t1 && u != t2)
  168. {
  169. parse_error ("expected %s or %s, have %s",
  170. print_token (t1, 0), print_token (t2, 0),
  171. print_token (u, v));
  172. return 0;
  173. }
  174. return v;
  175. }
  176. /* If the next token does not have one of the codes T1, T2, T3 or T4, report a
  177. parse error; otherwise return the token's value. */
  178. static const char *
  179. require4 (int t1, int t2, int t3, int t4)
  180. {
  181. int u = token ();
  182. const char *v = advance ();
  183. if (u != t1 && u != t2 && u != t3 && u != t4)
  184. {
  185. parse_error ("expected %s, %s, %s or %s, have %s",
  186. print_token (t1, 0), print_token (t2, 0),
  187. print_token (t3, 0), print_token (t4, 0),
  188. print_token (u, v));
  189. return 0;
  190. }
  191. return v;
  192. }
  193. /* Near-terminals. */
  194. /* C-style string constant concatenation: STRING+
  195. Bare STRING should appear nowhere else in this file. */
  196. static const char *
  197. string_seq (void)
  198. {
  199. const char *s1, *s2;
  200. size_t l1, l2;
  201. char *buf;
  202. s1 = require (STRING);
  203. if (s1 == 0)
  204. return "";
  205. while (token () == STRING)
  206. {
  207. s2 = advance ();
  208. l1 = strlen (s1);
  209. l2 = strlen (s2);
  210. buf = XRESIZEVEC (char, CONST_CAST (char *, s1), l1 + l2 + 1);
  211. memcpy (buf + l1, s2, l2 + 1);
  212. XDELETE (CONST_CAST (char *, s2));
  213. s1 = buf;
  214. }
  215. return s1;
  216. }
  217. /* The caller has detected a template declaration that starts
  218. with TMPL_NAME. Parse up to the closing '>'. This recognizes
  219. simple template declarations of the form ID<ID1,ID2,...,IDn>,
  220. potentially with a single level of indirection e.g.
  221. ID<ID1 *, ID2, ID3 *, ..., IDn>.
  222. It does not try to parse anything more sophisticated than that.
  223. Returns the template declaration string "ID<ID1,ID2,...,IDn>". */
  224. static const char *
  225. require_template_declaration (const char *tmpl_name)
  226. {
  227. char *str;
  228. int num_indirections = 0;
  229. /* Recognize the opening '<'. */
  230. require ('<');
  231. str = concat (tmpl_name, "<", (char *) 0);
  232. /* Read the comma-separated list of identifiers. */
  233. int depth = 1;
  234. while (depth > 0)
  235. {
  236. if (token () == ENUM)
  237. {
  238. advance ();
  239. str = concat (str, "enum ", (char *) 0);
  240. continue;
  241. }
  242. if (token () == NUM)
  243. {
  244. str = concat (str, advance (), (char *) 0);
  245. continue;
  246. }
  247. if (token () == ':')
  248. {
  249. advance ();
  250. str = concat (str, ":", (char *) 0);
  251. continue;
  252. }
  253. if (token () == '<')
  254. {
  255. advance ();
  256. str = concat (str, "<", (char *) 0);
  257. depth += 1;
  258. continue;
  259. }
  260. if (token () == '>')
  261. {
  262. advance ();
  263. str = concat (str, ">", (char *) 0);
  264. depth -= 1;
  265. continue;
  266. }
  267. const char *id = require4 (SCALAR, ID, '*', ',');
  268. if (id == NULL)
  269. {
  270. if (T.code == '*')
  271. {
  272. id = "*";
  273. if (num_indirections++)
  274. parse_error ("only one level of indirection is supported"
  275. " in template arguments");
  276. }
  277. else
  278. id = ",";
  279. }
  280. else
  281. num_indirections = 0;
  282. str = concat (str, id, (char *) 0);
  283. }
  284. return str;
  285. }
  286. /* typedef_name: either an ID, or a template type
  287. specification of the form ID<t1,t2,...,tn>. */
  288. static const char *
  289. typedef_name (void)
  290. {
  291. const char *id = require (ID);
  292. if (token () == '<')
  293. return require_template_declaration (id);
  294. else
  295. return id;
  296. }
  297. /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
  298. static void
  299. consume_balanced (int opener, int closer)
  300. {
  301. require (opener);
  302. for (;;)
  303. switch (token ())
  304. {
  305. default:
  306. advance ();
  307. break;
  308. case '(':
  309. consume_balanced ('(', ')');
  310. break;
  311. case '[':
  312. consume_balanced ('[', ']');
  313. break;
  314. case '{':
  315. consume_balanced ('{', '}');
  316. break;
  317. case '}':
  318. case ']':
  319. case ')':
  320. if (token () != closer)
  321. parse_error ("unbalanced delimiters - expected '%c', have '%c'",
  322. closer, token ());
  323. advance ();
  324. return;
  325. case EOF_TOKEN:
  326. parse_error ("unexpected end of file within %c%c-delimited construct",
  327. opener, closer);
  328. return;
  329. }
  330. }
  331. /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
  332. expressions, until we encounter an end-of-statement marker (a ';' or
  333. a '}') outside any such delimiters; absorb that too. */
  334. static void
  335. consume_until_eos (void)
  336. {
  337. for (;;)
  338. switch (token ())
  339. {
  340. case ';':
  341. advance ();
  342. return;
  343. case '{':
  344. consume_balanced ('{', '}');
  345. return;
  346. case '(':
  347. consume_balanced ('(', ')');
  348. break;
  349. case '[':
  350. consume_balanced ('[', ']');
  351. break;
  352. case '}':
  353. case ']':
  354. case ')':
  355. parse_error ("unmatched '%c' while scanning for ';'", token ());
  356. return;
  357. case EOF_TOKEN:
  358. parse_error ("unexpected end of file while scanning for ';'");
  359. return;
  360. default:
  361. advance ();
  362. break;
  363. }
  364. }
  365. /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
  366. expressions, until we encounter a comma or semicolon outside any
  367. such delimiters; absorb that too. Returns true if the loop ended
  368. with a comma. */
  369. static bool
  370. consume_until_comma_or_eos ()
  371. {
  372. for (;;)
  373. switch (token ())
  374. {
  375. case ',':
  376. advance ();
  377. return true;
  378. case ';':
  379. advance ();
  380. return false;
  381. case '{':
  382. consume_balanced ('{', '}');
  383. return false;
  384. case '(':
  385. consume_balanced ('(', ')');
  386. break;
  387. case '[':
  388. consume_balanced ('[', ']');
  389. break;
  390. case '}':
  391. case ']':
  392. case ')':
  393. parse_error ("unmatched '%s' while scanning for ',' or ';'",
  394. print_cur_token ());
  395. return false;
  396. case EOF_TOKEN:
  397. parse_error ("unexpected end of file while scanning for ',' or ';'");
  398. return false;
  399. default:
  400. advance ();
  401. break;
  402. }
  403. }
  404. /* GTY(()) option handling. */
  405. static type_p type (options_p *optsp, bool nested);
  406. /* Optional parenthesized string: ('(' string_seq ')')? */
  407. static options_p
  408. str_optvalue_opt (options_p prev)
  409. {
  410. const char *name = advance ();
  411. const char *value = "";
  412. if (token () == '(')
  413. {
  414. advance ();
  415. value = string_seq ();
  416. require (')');
  417. }
  418. return create_string_option (prev, name, value);
  419. }
  420. /* absdecl: type '*'*
  421. -- a vague approximation to what the C standard calls an abstract
  422. declarator. The only kinds that are actually used are those that
  423. are just a bare type and those that have trailing pointer-stars.
  424. Further kinds should be implemented if and when they become
  425. necessary. Used only within GTY(()) option values, therefore
  426. further GTY(()) tags within the type are invalid. Note that the
  427. return value has already been run through adjust_field_type. */
  428. static type_p
  429. absdecl (void)
  430. {
  431. type_p ty;
  432. options_p opts;
  433. ty = type (&opts, true);
  434. while (token () == '*')
  435. {
  436. ty = create_pointer (ty);
  437. advance ();
  438. }
  439. if (opts)
  440. parse_error ("nested GTY(()) options are invalid");
  441. return adjust_field_type (ty, 0);
  442. }
  443. /* Type-option: '(' absdecl ')' */
  444. static options_p
  445. type_optvalue (options_p prev, const char *name)
  446. {
  447. type_p ty;
  448. require ('(');
  449. ty = absdecl ();
  450. require (')');
  451. return create_type_option (prev, name, ty);
  452. }
  453. /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
  454. static options_p
  455. nestedptr_optvalue (options_p prev)
  456. {
  457. type_p ty;
  458. const char *from, *to;
  459. require ('(');
  460. ty = absdecl ();
  461. require (',');
  462. to = string_seq ();
  463. require (',');
  464. from = string_seq ();
  465. require (')');
  466. return create_nested_ptr_option (prev, ty, to, from);
  467. }
  468. /* One GTY(()) option:
  469. ID str_optvalue_opt
  470. | PTR_ALIAS type_optvalue
  471. | NESTED_PTR nestedptr_optvalue
  472. */
  473. static options_p
  474. option (options_p prev)
  475. {
  476. switch (token ())
  477. {
  478. case ID:
  479. return str_optvalue_opt (prev);
  480. case PTR_ALIAS:
  481. advance ();
  482. return type_optvalue (prev, "ptr_alias");
  483. case NESTED_PTR:
  484. advance ();
  485. return nestedptr_optvalue (prev);
  486. case USER_GTY:
  487. advance ();
  488. return create_string_option (prev, "user", "");
  489. default:
  490. parse_error ("expected an option keyword, have %s", print_cur_token ());
  491. advance ();
  492. return create_string_option (prev, "", "");
  493. }
  494. }
  495. /* One comma-separated list of options. */
  496. static options_p
  497. option_seq (void)
  498. {
  499. options_p o;
  500. o = option (0);
  501. while (token () == ',')
  502. {
  503. advance ();
  504. o = option (o);
  505. }
  506. return o;
  507. }
  508. /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
  509. static options_p
  510. gtymarker (void)
  511. {
  512. options_p result = 0;
  513. require (GTY_TOKEN);
  514. require ('(');
  515. require ('(');
  516. if (token () != ')')
  517. result = option_seq ();
  518. require (')');
  519. require (')');
  520. return result;
  521. }
  522. /* Optional GTY marker. */
  523. static options_p
  524. gtymarker_opt (void)
  525. {
  526. if (token () != GTY_TOKEN)
  527. return 0;
  528. return gtymarker ();
  529. }
  530. /* Declarators. The logic here is largely lifted from c-parser.c.
  531. Note that we do not have to process abstract declarators, which can
  532. appear only in parameter type lists or casts (but see absdecl,
  533. above). Also, type qualifiers are thrown out in gengtype-lex.l so
  534. we don't have to do it. */
  535. /* array_and_function_declarators_opt:
  536. \epsilon
  537. array_and_function_declarators_opt ARRAY
  538. array_and_function_declarators_opt '(' ... ')'
  539. where '...' indicates stuff we ignore except insofar as grouping
  540. symbols ()[]{} must balance.
  541. Subroutine of direct_declarator - do not use elsewhere. */
  542. static type_p
  543. array_and_function_declarators_opt (type_p ty)
  544. {
  545. if (token () == ARRAY)
  546. {
  547. const char *array = advance ();
  548. return create_array (array_and_function_declarators_opt (ty), array);
  549. }
  550. else if (token () == '(')
  551. {
  552. /* We don't need exact types for functions. */
  553. consume_balanced ('(', ')');
  554. array_and_function_declarators_opt (ty);
  555. return create_scalar_type ("function type");
  556. }
  557. else
  558. return ty;
  559. }
  560. static type_p inner_declarator (type_p, const char **, options_p *, bool);
  561. /* direct_declarator:
  562. '(' inner_declarator ')'
  563. '(' \epsilon ')' <-- C++ ctors/dtors
  564. gtymarker_opt ID array_and_function_declarators_opt
  565. Subroutine of declarator, mutually recursive with inner_declarator;
  566. do not use elsewhere.
  567. IN_STRUCT is true if we are called while parsing structures or classes. */
  568. static type_p
  569. direct_declarator (type_p ty, const char **namep, options_p *optsp,
  570. bool in_struct)
  571. {
  572. /* The first token in a direct-declarator must be an ID, a
  573. GTY marker, or an open parenthesis. */
  574. switch (token ())
  575. {
  576. case GTY_TOKEN:
  577. *optsp = gtymarker ();
  578. /* fall through */
  579. case ID:
  580. *namep = require (ID);
  581. /* If the next token is '(', we are parsing a function declaration.
  582. Functions are ignored by gengtype, so we return NULL. */
  583. if (token () == '(')
  584. return NULL;
  585. break;
  586. case '(':
  587. /* If the declarator starts with a '(', we have three options. We
  588. are either parsing 'TYPE (*ID)' (i.e., a function pointer)
  589. or 'TYPE(...)'.
  590. The latter will be a constructor iff we are inside a
  591. structure or class. Otherwise, it could be a typedef, but
  592. since we explicitly reject typedefs inside structures, we can
  593. assume that we found a ctor and return NULL. */
  594. advance ();
  595. if (in_struct && token () != '*')
  596. {
  597. /* Found a constructor. Find and consume the closing ')'. */
  598. while (token () != ')')
  599. advance ();
  600. advance ();
  601. /* Tell the caller to ignore this. */
  602. return NULL;
  603. }
  604. ty = inner_declarator (ty, namep, optsp, in_struct);
  605. require (')');
  606. break;
  607. case IGNORABLE_CXX_KEYWORD:
  608. /* Any C++ keyword like 'operator' means that we are not looking
  609. at a regular data declarator. */
  610. return NULL;
  611. default:
  612. parse_error ("expected '(', ')', 'GTY', or an identifier, have %s",
  613. print_cur_token ());
  614. /* Do _not_ advance if what we have is a close squiggle brace, as
  615. we will get much better error recovery that way. */
  616. if (token () != '}')
  617. advance ();
  618. return 0;
  619. }
  620. return array_and_function_declarators_opt (ty);
  621. }
  622. /* The difference between inner_declarator and declarator is in the
  623. handling of stars. Consider this declaration:
  624. char * (*pfc) (void)
  625. It declares a pointer to a function that takes no arguments and
  626. returns a char*. To construct the correct type for this
  627. declaration, the star outside the parentheses must be processed
  628. _before_ the function type, the star inside the parentheses must
  629. be processed _after_ the function type. To accomplish this,
  630. declarator() creates pointers before recursing (it is actually
  631. coded as a while loop), whereas inner_declarator() recurses before
  632. creating pointers. */
  633. /* inner_declarator:
  634. '*' inner_declarator
  635. direct_declarator
  636. Mutually recursive subroutine of direct_declarator; do not use
  637. elsewhere.
  638. IN_STRUCT is true if we are called while parsing structures or classes. */
  639. static type_p
  640. inner_declarator (type_p ty, const char **namep, options_p *optsp,
  641. bool in_struct)
  642. {
  643. if (token () == '*')
  644. {
  645. type_p inner;
  646. advance ();
  647. inner = inner_declarator (ty, namep, optsp, in_struct);
  648. if (inner == 0)
  649. return 0;
  650. else
  651. return create_pointer (ty);
  652. }
  653. else
  654. return direct_declarator (ty, namep, optsp, in_struct);
  655. }
  656. /* declarator: '*'+ direct_declarator
  657. This is the sole public interface to this part of the grammar.
  658. Arguments are the type known so far, a pointer to where the name
  659. may be stored, and a pointer to where GTY options may be stored.
  660. IN_STRUCT is true when we are called to parse declarators inside
  661. a structure or class.
  662. Returns the final type. */
  663. static type_p
  664. declarator (type_p ty, const char **namep, options_p *optsp,
  665. bool in_struct = false)
  666. {
  667. *namep = 0;
  668. *optsp = 0;
  669. while (token () == '*')
  670. {
  671. advance ();
  672. ty = create_pointer (ty);
  673. }
  674. return direct_declarator (ty, namep, optsp, in_struct);
  675. }
  676. /* Types and declarations. */
  677. /* Structure field(s) declaration:
  678. (
  679. type bitfield ';'
  680. | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
  681. )*
  682. Knows that such declarations must end with a close brace (or,
  683. erroneously, at EOF).
  684. */
  685. static pair_p
  686. struct_field_seq (void)
  687. {
  688. pair_p f = 0;
  689. type_p ty, dty;
  690. options_p opts, dopts;
  691. const char *name;
  692. bool another;
  693. while (token () != '}' && token () != EOF_TOKEN)
  694. {
  695. ty = type (&opts, true);
  696. /* Ignore access-control keywords ("public:" etc). */
  697. while (!ty && token () == IGNORABLE_CXX_KEYWORD)
  698. {
  699. const char *keyword = advance ();
  700. if (strcmp (keyword, "public:") != 0
  701. && strcmp (keyword, "private:") != 0
  702. && strcmp (keyword, "protected:") != 0)
  703. break;
  704. ty = type (&opts, true);
  705. }
  706. if (!ty || token () == ':')
  707. {
  708. consume_until_eos ();
  709. continue;
  710. }
  711. do
  712. {
  713. dty = declarator (ty, &name, &dopts, true);
  714. /* There could be any number of weird things after the declarator,
  715. notably bitfield declarations and __attribute__s. If this
  716. function returns true, the last thing was a comma, so we have
  717. more than one declarator paired with the current type. */
  718. another = consume_until_comma_or_eos ();
  719. if (!dty)
  720. continue;
  721. if (opts && dopts)
  722. parse_error ("two GTY(()) options for field %s", name);
  723. if (opts && !dopts)
  724. dopts = opts;
  725. f = create_field_at (f, dty, name, dopts, &lexer_line);
  726. }
  727. while (another);
  728. }
  729. return nreverse_pairs (f);
  730. }
  731. /* Return true if OPTS contain the option named STR. */
  732. bool
  733. opts_have (options_p opts, const char *str)
  734. {
  735. for (options_p opt = opts; opt; opt = opt->next)
  736. if (strcmp (opt->name, str) == 0)
  737. return true;
  738. return false;
  739. }
  740. /* This is called type(), but what it parses (sort of) is what C calls
  741. declaration-specifiers and specifier-qualifier-list:
  742. SCALAR
  743. | ID // typedef
  744. | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
  745. | ENUM ID ( '{' ... '}' )?
  746. Returns a partial type; under some conditions (notably
  747. "struct foo GTY((...)) thing;") it may write an options
  748. structure to *OPTSP.
  749. NESTED is true when parsing a declaration already known to have a
  750. GTY marker. In these cases, typedef and enum declarations are not
  751. allowed because gengtype only understands types at the global
  752. scope. */
  753. static type_p
  754. type (options_p *optsp, bool nested)
  755. {
  756. const char *s;
  757. *optsp = 0;
  758. switch (token ())
  759. {
  760. case SCALAR:
  761. s = advance ();
  762. return create_scalar_type (s);
  763. case ID:
  764. s = typedef_name ();
  765. return resolve_typedef (s, &lexer_line);
  766. case IGNORABLE_CXX_KEYWORD:
  767. /* By returning NULL here, we indicate to the caller that they
  768. should ignore everything following this keyword up to the
  769. next ';' or '}'. */
  770. return NULL;
  771. case STRUCT:
  772. case UNION:
  773. {
  774. type_p base_class = NULL;
  775. options_p opts = 0;
  776. /* GTY annotations follow attribute syntax
  777. GTY_BEFORE_ID is for union/struct declarations
  778. GTY_AFTER_ID is for variable declarations. */
  779. enum
  780. {
  781. NO_GTY,
  782. GTY_BEFORE_ID,
  783. GTY_AFTER_ID
  784. } is_gty = NO_GTY;
  785. enum typekind kind = (token () == UNION) ? TYPE_UNION : TYPE_STRUCT;
  786. advance ();
  787. /* Top-level structures that are not explicitly tagged GTY(())
  788. are treated as mere forward declarations. This is because
  789. there are a lot of structures that we don't need to know
  790. about, and some of those have C++ and macro constructs that
  791. we cannot handle. */
  792. if (nested || token () == GTY_TOKEN)
  793. {
  794. is_gty = GTY_BEFORE_ID;
  795. opts = gtymarker_opt ();
  796. }
  797. if (token () == ID)
  798. s = advance ();
  799. else
  800. s = xasprintf ("anonymous:%s:%d",
  801. get_input_file_name (lexer_line.file),
  802. lexer_line.line);
  803. /* Unfortunately above GTY_TOKEN check does not capture the
  804. typedef struct_type GTY case. */
  805. if (token () == GTY_TOKEN)
  806. {
  807. is_gty = GTY_AFTER_ID;
  808. opts = gtymarker_opt ();
  809. }
  810. bool is_user_gty = opts_have (opts, "user");
  811. if (token () == ':')
  812. {
  813. if (is_gty && !is_user_gty)
  814. {
  815. /* For GTY-marked types that are not "user", parse some C++
  816. inheritance specifications.
  817. We require single-inheritance from a non-template type. */
  818. advance ();
  819. const char *basename = require (ID);
  820. /* This may be either an access specifier, or the base name. */
  821. if (0 == strcmp (basename, "public")
  822. || 0 == strcmp (basename, "protected")
  823. || 0 == strcmp (basename, "private"))
  824. basename = require (ID);
  825. base_class = find_structure (basename, TYPE_STRUCT);
  826. if (!base_class)
  827. parse_error ("unrecognized base class: %s", basename);
  828. require_without_advance ('{');
  829. }
  830. else
  831. {
  832. /* For types lacking GTY-markings, skip over C++ inheritance
  833. specification (and thus avoid having to parse e.g. template
  834. types). */
  835. while (token () != '{')
  836. advance ();
  837. }
  838. }
  839. if (is_gty)
  840. {
  841. if (token () == '{')
  842. {
  843. pair_p fields;
  844. if (is_gty == GTY_AFTER_ID)
  845. parse_error ("GTY must be specified before identifier");
  846. if (!is_user_gty)
  847. {
  848. advance ();
  849. fields = struct_field_seq ();
  850. require ('}');
  851. }
  852. else
  853. {
  854. /* Do not look inside user defined structures. */
  855. fields = NULL;
  856. kind = TYPE_USER_STRUCT;
  857. consume_balanced ('{', '}');
  858. return create_user_defined_type (s, &lexer_line);
  859. }
  860. return new_structure (s, kind, &lexer_line, fields, opts,
  861. base_class);
  862. }
  863. }
  864. else if (token () == '{')
  865. consume_balanced ('{', '}');
  866. if (opts)
  867. *optsp = opts;
  868. return find_structure (s, kind);
  869. }
  870. case TYPEDEF:
  871. /* In C++, a typedef inside a struct/class/union defines a new
  872. type for that inner scope. We cannot support this in
  873. gengtype because we have no concept of scoping.
  874. We handle typedefs in the global scope separately (see
  875. parse_file), so if we find a 'typedef', we must be inside
  876. a struct. */
  877. gcc_assert (nested);
  878. parse_error ("typedefs not supported in structures marked with "
  879. "automatic GTY markers. Use GTY((user)) to mark "
  880. "this structure.");
  881. advance ();
  882. return NULL;
  883. case ENUM:
  884. advance ();
  885. if (token () == ID)
  886. s = advance ();
  887. else
  888. s = xasprintf ("anonymous:%s:%d",
  889. get_input_file_name (lexer_line.file),
  890. lexer_line.line);
  891. if (token () == '{')
  892. consume_balanced ('{', '}');
  893. /* If after parsing the enum we are at the end of the statement,
  894. and we are currently inside a structure, then this was an
  895. enum declaration inside this scope.
  896. We cannot support this for the same reason we cannot support
  897. 'typedef' inside structures (see the TYPEDEF handler above).
  898. If this happens, emit an error and return NULL. */
  899. if (nested && token () == ';')
  900. {
  901. parse_error ("enum definitions not supported in structures marked "
  902. "with automatic GTY markers. Use GTY((user)) to mark "
  903. "this structure.");
  904. advance ();
  905. return NULL;
  906. }
  907. return create_scalar_type (s);
  908. default:
  909. parse_error ("expected a type specifier, have %s", print_cur_token ());
  910. advance ();
  911. return create_scalar_type ("erroneous type");
  912. }
  913. }
  914. /* Top level constructs. */
  915. /* Dispatch declarations beginning with 'typedef'. */
  916. static void
  917. typedef_decl (void)
  918. {
  919. type_p ty, dty;
  920. const char *name;
  921. options_p opts;
  922. bool another;
  923. gcc_assert (token () == TYPEDEF);
  924. advance ();
  925. ty = type (&opts, false);
  926. if (!ty)
  927. return;
  928. if (opts)
  929. parse_error ("GTY((...)) cannot be applied to a typedef");
  930. do
  931. {
  932. dty = declarator (ty, &name, &opts);
  933. if (opts)
  934. parse_error ("GTY((...)) cannot be applied to a typedef");
  935. /* Yet another place where we could have junk (notably attributes)
  936. after the declarator. */
  937. another = consume_until_comma_or_eos ();
  938. if (dty)
  939. do_typedef (name, dty, &lexer_line);
  940. }
  941. while (another);
  942. }
  943. /* Structure definition: type() does all the work. */
  944. static void
  945. struct_or_union (void)
  946. {
  947. options_p dummy;
  948. type (&dummy, false);
  949. /* There may be junk after the type: notably, we cannot currently
  950. distinguish 'struct foo *function(prototype);' from 'struct foo;'
  951. ... we could call declarator(), but it's a waste of time at
  952. present. Instead, just eat whatever token is currently lookahead
  953. and go back to lexical skipping mode. */
  954. advance ();
  955. }
  956. /* GC root declaration:
  957. (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
  958. If the gtymarker is not present, we ignore the rest of the declaration. */
  959. static void
  960. extern_or_static (void)
  961. {
  962. options_p opts, opts2, dopts;
  963. type_p ty, dty;
  964. const char *name;
  965. require2 (EXTERN, STATIC);
  966. if (token () != GTY_TOKEN)
  967. {
  968. advance ();
  969. return;
  970. }
  971. opts = gtymarker ();
  972. ty = type (&opts2, true); /* if we get here, it's got a GTY(()) */
  973. dty = declarator (ty, &name, &dopts);
  974. if ((opts && dopts) || (opts && opts2) || (opts2 && dopts))
  975. parse_error ("GTY((...)) specified more than once for %s", name);
  976. else if (opts2)
  977. opts = opts2;
  978. else if (dopts)
  979. opts = dopts;
  980. if (dty)
  981. {
  982. note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line);
  983. require2 (';', '=');
  984. }
  985. }
  986. /* Parse the file FNAME for GC-relevant declarations and definitions.
  987. This is the only entry point to this file. */
  988. void
  989. parse_file (const char *fname)
  990. {
  991. yybegin (fname);
  992. for (;;)
  993. {
  994. switch (token ())
  995. {
  996. case EXTERN:
  997. case STATIC:
  998. extern_or_static ();
  999. break;
  1000. case STRUCT:
  1001. case UNION:
  1002. struct_or_union ();
  1003. break;
  1004. case TYPEDEF:
  1005. typedef_decl ();
  1006. break;
  1007. case EOF_TOKEN:
  1008. goto eof;
  1009. default:
  1010. parse_error ("unexpected top level token, %s", print_cur_token ());
  1011. goto eof;
  1012. }
  1013. lexer_toplevel_done = 1;
  1014. }
  1015. eof:
  1016. advance ();
  1017. yyend ();
  1018. }