gengtype-state.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620
  1. /* Gengtype persistent state serialization & de-serialization.
  2. Useful for gengtype in plugin mode.
  3. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>.
  16. Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr>
  17. and Basile Starynkevitch <basile@starynkevitch.net>
  18. */
  19. #ifdef HOST_GENERATOR_FILE
  20. #include "config.h"
  21. #define GENERATOR_FILE 1
  22. #else
  23. #include "bconfig.h"
  24. #endif
  25. #include "system.h"
  26. #include "errors.h" /* For fatal. */
  27. #include "hashtab.h"
  28. #include "version.h" /* For version_string & pkgversion_string. */
  29. #include "obstack.h"
  30. #include "gengtype.h"
  31. /* Gives the file location of a type, if any. */
  32. static inline struct fileloc*
  33. type_lineloc (const_type_p ty)
  34. {
  35. if (!ty)
  36. return NULL;
  37. switch (ty->kind)
  38. {
  39. case TYPE_NONE:
  40. gcc_unreachable ();
  41. case TYPE_STRUCT:
  42. case TYPE_UNION:
  43. case TYPE_LANG_STRUCT:
  44. case TYPE_USER_STRUCT:
  45. case TYPE_UNDEFINED:
  46. return CONST_CAST (struct fileloc*, &ty->u.s.line);
  47. case TYPE_SCALAR:
  48. case TYPE_STRING:
  49. case TYPE_POINTER:
  50. case TYPE_ARRAY:
  51. return NULL;
  52. default:
  53. gcc_unreachable ();
  54. }
  55. }
  56. /* The state file has simplistic lispy lexical tokens. Its lexer gives
  57. a linked list of struct state_token_st, through the peek_state_token
  58. function. Lexical tokens are consumed with next_state_tokens. */
  59. /* The lexical kind of each lispy token. */
  60. enum state_token_en
  61. {
  62. STOK_NONE, /* Never used. */
  63. STOK_INTEGER, /* Integer token. */
  64. STOK_STRING, /* String token. */
  65. STOK_LEFTPAR, /* Left opening parenthesis. */
  66. STOK_RIGHTPAR, /* Right closing parenthesis. */
  67. STOK_NAME /* hash-consed name or identifier. */
  68. };
  69. /* Structure and hash-table used to share identifiers or names. */
  70. struct state_ident_st
  71. {
  72. /* TODO: We could improve the parser by reserving identifiers for
  73. state keywords and adding a keyword number for them. That would
  74. mean adding another field in this state_ident_st struct. */
  75. char stid_name[1]; /* actually bigger & null terminated */
  76. };
  77. static htab_t state_ident_tab;
  78. /* The state_token_st structure is for lexical tokens in the read
  79. state file. The stok_kind field discriminates the union. Tokens
  80. are allocated by peek_state_token which calls read_a_state_token
  81. which allocate them. Tokens are freed by calls to
  82. next_state_tokens. Token are organized in a FIFO look-ahead queue
  83. filled by peek_state_token. */
  84. struct state_token_st
  85. {
  86. enum state_token_en stok_kind; /* the lexical kind
  87. discriminates the stok_un
  88. union */
  89. int stok_line; /* the line number */
  90. int stok_col; /* the column number */
  91. const char *stok_file; /* the file path */
  92. struct state_token_st *stok_next; /* the next token in the
  93. queue, when peeked */
  94. union /* discriminated by stok_kind! */
  95. {
  96. int stok_num; /* when STOK_INTEGER */
  97. char stok_string[1]; /* when STOK_STRING, actual size is
  98. bigger and null terminated */
  99. struct state_ident_st *stok_ident; /* when STOK_IDENT */
  100. void *stok_ptr; /* null otherwise */
  101. }
  102. stok_un;
  103. };
  104. #define NULL_STATE_TOKEN (struct state_token_st*)0
  105. /* the state_token pointer contains the leftmost current token. The
  106. tokens are organized in a linked queue, using stok_next, for token
  107. look-ahead. */
  108. struct state_token_st *state_token = NULL_STATE_TOKEN;
  109. /* Used by the reading lexer. */
  110. static FILE *state_file;
  111. static const char *state_path = NULL;
  112. static int state_line = 0;
  113. static long state_bol = 0; /* offset of beginning of line */
  114. /* A class for writing out s-expressions, keeping track of newlines and
  115. nested indentation. */
  116. class s_expr_writer
  117. {
  118. public:
  119. s_expr_writer ();
  120. void write_new_line ();
  121. void write_any_indent (int leading_spaces);
  122. void begin_s_expr (const char *tag);
  123. void end_s_expr ();
  124. private:
  125. int m_indent_amount;
  126. int m_had_recent_newline;
  127. }; // class s_expr_writer
  128. /* A class for writing out "gtype.state". */
  129. class state_writer : public s_expr_writer
  130. {
  131. public:
  132. state_writer ();
  133. private:
  134. void write_state_fileloc (struct fileloc *floc);
  135. void write_state_fields (pair_p fields);
  136. void write_state_a_string (const char *s);
  137. void write_state_string_option (options_p current);
  138. void write_state_type_option (options_p current);
  139. void write_state_nested_option (options_p current);
  140. void write_state_option (options_p current);
  141. void write_state_options (options_p opt);
  142. void write_state_lang_bitmap (lang_bitmap bitmap);
  143. void write_state_version (const char *version);
  144. void write_state_scalar_type (type_p current);
  145. void write_state_string_type (type_p current);
  146. void write_state_undefined_type (type_p current);
  147. void write_state_struct_union_type (type_p current, const char *kindstr);
  148. void write_state_struct_type (type_p current);
  149. void write_state_user_struct_type (type_p current);
  150. void write_state_union_type (type_p current);
  151. void write_state_lang_struct_type (type_p current);
  152. void write_state_pointer_type (type_p current);
  153. void write_state_array_type (type_p current);
  154. void write_state_gc_used (enum gc_used_enum gus);
  155. void write_state_common_type_content (type_p current);
  156. void write_state_type (type_p current);
  157. void write_state_pair (pair_p current);
  158. int write_state_pair_list (pair_p list);
  159. void write_state_typedefs (void);
  160. void write_state_structures (void);
  161. void write_state_variables (void);
  162. void write_state_srcdir (void);
  163. void write_state_files_list (void);
  164. void write_state_languages (void);
  165. friend void write_state (const char *state_path);
  166. private:
  167. /* Counter of written types. */
  168. int m_state_written_type_count;
  169. }; // class state_writer
  170. /* class s_expr_writer's trivial constructor. */
  171. s_expr_writer::s_expr_writer ()
  172. : m_indent_amount (0),
  173. m_had_recent_newline (0)
  174. {
  175. }
  176. /* Write a newline to the output file, merging adjacent newlines. */
  177. void
  178. s_expr_writer::write_new_line (void)
  179. {
  180. /* Don't add a newline if we've just had one. */
  181. if (!m_had_recent_newline)
  182. {
  183. fprintf (state_file, "\n");
  184. m_had_recent_newline = 1;
  185. }
  186. }
  187. /* If we've just had a newline, write the indentation amount, potentially
  188. omitting some spaces.
  189. LEADING_SPACES exists to support code that writes strings with leading
  190. spaces (e.g " foo") which might occur within a line, or could be the first
  191. thing on a line. By passing leading_spaces == 1, when such a string is the
  192. first thing on a line, write_any_indent () swallows the successive
  193. leading spaces into the indentation so that the "foo" begins at the expected
  194. column. */
  195. void
  196. s_expr_writer::write_any_indent (int leading_spaces)
  197. {
  198. int i;
  199. int amount = m_indent_amount - leading_spaces;
  200. if (m_had_recent_newline)
  201. for (i = 0; i < amount; i++)
  202. fprintf (state_file, " ");
  203. m_had_recent_newline = 0;
  204. }
  205. /* Write the beginning of a new s-expresion e.g. "(!foo "
  206. The writer automatically adds whitespace to show the hierarchical
  207. structure of the expressions, so each one starts on a new line,
  208. and any within it will be at an increased indentation level. */
  209. void
  210. s_expr_writer::begin_s_expr (const char *tag)
  211. {
  212. write_new_line ();
  213. write_any_indent (0);
  214. fprintf (state_file, "(!%s ", tag);
  215. m_indent_amount++;
  216. }
  217. /* Write out the end of an s-expression: any necssessary indentation,
  218. a closing parenthesis, and a new line. */
  219. void
  220. s_expr_writer::end_s_expr (void)
  221. {
  222. m_indent_amount--;
  223. write_any_indent (0);
  224. fprintf (state_file, ")");
  225. write_new_line ();
  226. }
  227. /* class state_writer's trivial constructor. */
  228. state_writer::state_writer ()
  229. : s_expr_writer (),
  230. m_state_written_type_count (0)
  231. {
  232. }
  233. /* Fatal error messages when reading the state. They are extremely
  234. unlikely, and only appear when this gengtype-state.c file is buggy,
  235. or when reading a gengtype state which was not generated by the
  236. same version of gengtype or GCC. */
  237. /* Fatal message while reading state. */
  238. static void
  239. fatal_reading_state (struct state_token_st* tok, const char*msg)
  240. {
  241. if (tok)
  242. fatal ("%s:%d:%d: Invalid state file; %s",
  243. tok->stok_file, tok->stok_line, tok->stok_col,
  244. msg);
  245. else
  246. fatal ("%s:%d: Invalid state file; %s",
  247. state_path, state_line, msg);
  248. }
  249. /* Fatal printf-like message while reading state. This can't be a
  250. function, because there is no way to pass a va_arg to a variant of
  251. fatal. */
  252. #define fatal_reading_state_printf(Tok,Fmt,...) do { \
  253. struct state_token_st* badtok = Tok; \
  254. if (badtok) \
  255. fatal ("%s:%d:%d: Invalid state file; " Fmt, \
  256. badtok->stok_file, \
  257. badtok->stok_line, \
  258. badtok->stok_col, __VA_ARGS__); \
  259. else \
  260. fatal ("%s:%d: Invalid state file; " Fmt, \
  261. state_path, state_line, __VA_ARGS__); \
  262. } while (0)
  263. /* Find or allocate an identifier in our name hash table. */
  264. static struct state_ident_st *
  265. state_ident_by_name (const char *name, enum insert_option optins)
  266. {
  267. PTR *slot = NULL;
  268. int namlen = 0;
  269. struct state_ident_st *stid = NULL;
  270. if (!name || !name[0])
  271. return NULL;
  272. slot = htab_find_slot (state_ident_tab, name, optins);
  273. if (!slot)
  274. return NULL;
  275. namlen = strlen (name);
  276. stid =
  277. (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
  278. namlen);
  279. memset (stid, 0, sizeof (struct state_ident_st) + namlen);
  280. strcpy (stid->stid_name, name);
  281. *slot = stid;
  282. return stid;
  283. }
  284. /* Our token lexer is heavily inspired by MELT's lexer, and share some
  285. code with the file gcc/melt-runtime.c of the GCC MELT branch! We
  286. really want the gengtype state to be easily parsable by MELT. This
  287. is a usual lispy lexing routine, dealing with spaces and comments,
  288. numbers, parenthesis, names, strings. */
  289. static struct state_token_st *
  290. read_a_state_token (void)
  291. {
  292. int c = 0;
  293. long curoff = 0;
  294. struct state_token_st *tk = NULL;
  295. again: /* Read again, e.g. after a comment or spaces. */
  296. c = getc (state_file);
  297. if (c == EOF)
  298. return NULL;
  299. /* Handle spaces, count lines. */
  300. if (c == '\n')
  301. {
  302. state_line++;
  303. state_bol = curoff = ftell (state_file);
  304. goto again;
  305. };
  306. if (ISSPACE (c))
  307. goto again;
  308. /* Skip comments starting with semi-colon. */
  309. if (c == ';')
  310. {
  311. do
  312. {
  313. c = getc (state_file);
  314. }
  315. while (c > 0 && c != '\n');
  316. if (c == '\n')
  317. {
  318. state_line++;
  319. state_bol = curoff = ftell (state_file);
  320. }
  321. goto again;
  322. };
  323. /* Read signed numbers. */
  324. if (ISDIGIT (c) || c == '-' || c == '+')
  325. { /* number */
  326. int n = 0;
  327. ungetc (c, state_file);
  328. curoff = ftell (state_file);
  329. if (fscanf (state_file, "%d", &n) <= 0)
  330. fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
  331. tk = XCNEW (struct state_token_st);
  332. tk->stok_kind = STOK_INTEGER;
  333. tk->stok_line = state_line;
  334. tk->stok_col = curoff - state_bol;
  335. tk->stok_file = state_path;
  336. tk->stok_next = NULL;
  337. tk->stok_un.stok_num = n;
  338. return tk;
  339. }
  340. /* Read an opening left parenthesis. */
  341. else if (c == '(')
  342. {
  343. curoff = ftell (state_file);
  344. tk = XCNEW (struct state_token_st);
  345. tk->stok_kind = STOK_LEFTPAR;
  346. tk->stok_line = state_line;
  347. tk->stok_col = curoff - state_bol;
  348. tk->stok_file = state_path;
  349. tk->stok_next = NULL;
  350. return tk;
  351. }
  352. /* Read an closing right parenthesis. */
  353. else if (c == ')')
  354. {
  355. curoff = ftell (state_file);
  356. tk = XCNEW (struct state_token_st);
  357. tk->stok_kind = STOK_RIGHTPAR;
  358. tk->stok_line = state_line;
  359. tk->stok_col = curoff - state_bol;
  360. tk->stok_file = state_path;
  361. tk->stok_next = NULL;
  362. return tk;
  363. }
  364. /* Read identifiers, using an obstack. */
  365. else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
  366. {
  367. struct obstack id_obstack;
  368. struct state_ident_st *sid = NULL;
  369. char *ids = NULL;
  370. obstack_init (&id_obstack);
  371. curoff = ftell (state_file);
  372. while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
  373. {
  374. obstack_1grow (&id_obstack, c);
  375. c = getc (state_file);
  376. if (c < 0)
  377. break;
  378. };
  379. if (c >= 0)
  380. ungetc (c, state_file);
  381. obstack_1grow (&id_obstack, (char) 0);
  382. ids = XOBFINISH (&id_obstack, char *);
  383. sid = state_ident_by_name (ids, INSERT);
  384. obstack_free (&id_obstack, NULL);
  385. ids = NULL;
  386. tk = XCNEW (struct state_token_st);
  387. tk->stok_kind = STOK_NAME;
  388. tk->stok_line = state_line;
  389. tk->stok_col = curoff - state_bol;
  390. tk->stok_file = state_path;
  391. tk->stok_next = NULL;
  392. tk->stok_un.stok_ident = sid;
  393. return tk;
  394. }
  395. /* Read a string, dealing with escape sequences a la C! */
  396. else if (c == '"')
  397. {
  398. char *cstr = NULL;
  399. int cslen = 0;
  400. struct obstack bstring_obstack;
  401. obstack_init (&bstring_obstack);
  402. curoff = ftell (state_file);
  403. while ((c = getc (state_file)) != '"' && c >= 0)
  404. {
  405. if (ISPRINT (c) && c != '\\')
  406. obstack_1grow (&bstring_obstack, (char) c);
  407. else if (ISSPACE (c) && c != '\n')
  408. obstack_1grow (&bstring_obstack, (char) c);
  409. else if (c == '\\')
  410. {
  411. c = getc (state_file);
  412. switch (c)
  413. {
  414. case 'a':
  415. obstack_1grow (&bstring_obstack, '\a');
  416. c = getc (state_file);
  417. break;
  418. case 'b':
  419. obstack_1grow (&bstring_obstack, '\b');
  420. c = getc (state_file);
  421. break;
  422. case 't':
  423. obstack_1grow (&bstring_obstack, '\t');
  424. c = getc (state_file);
  425. break;
  426. case 'n':
  427. obstack_1grow (&bstring_obstack, '\n');
  428. c = getc (state_file);
  429. break;
  430. case 'v':
  431. obstack_1grow (&bstring_obstack, '\v');
  432. c = getc (state_file);
  433. break;
  434. case 'f':
  435. obstack_1grow (&bstring_obstack, '\f');
  436. c = getc (state_file);
  437. break;
  438. case 'r':
  439. obstack_1grow (&bstring_obstack, '\r');
  440. c = getc (state_file);
  441. break;
  442. case '"':
  443. obstack_1grow (&bstring_obstack, '\"');
  444. c = getc (state_file);
  445. break;
  446. case '\\':
  447. obstack_1grow (&bstring_obstack, '\\');
  448. c = getc (state_file);
  449. break;
  450. case ' ':
  451. obstack_1grow (&bstring_obstack, ' ');
  452. c = getc (state_file);
  453. break;
  454. case 'x':
  455. {
  456. unsigned int cx = 0;
  457. if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
  458. obstack_1grow (&bstring_obstack, cx);
  459. else
  460. fatal_reading_state
  461. (NULL_STATE_TOKEN,
  462. "Lexical error in string hex escape");
  463. c = getc (state_file);
  464. break;
  465. }
  466. default:
  467. fatal_reading_state
  468. (NULL_STATE_TOKEN,
  469. "Lexical error - unknown string escape");
  470. }
  471. }
  472. else
  473. fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
  474. };
  475. if (c != '"')
  476. fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
  477. obstack_1grow (&bstring_obstack, '\0');
  478. cstr = XOBFINISH (&bstring_obstack, char *);
  479. cslen = strlen (cstr);
  480. tk = (struct state_token_st *)
  481. xcalloc (sizeof (struct state_token_st) + cslen, 1);
  482. tk->stok_kind = STOK_STRING;
  483. tk->stok_line = state_line;
  484. tk->stok_col = curoff - state_bol;
  485. tk->stok_file = state_path;
  486. tk->stok_next = NULL;
  487. strcpy (tk->stok_un.stok_string, cstr);
  488. obstack_free (&bstring_obstack, NULL);
  489. return tk;
  490. }
  491. /* Got an unexpected character. */
  492. fatal_reading_state_printf
  493. (NULL_STATE_TOKEN,
  494. "Lexical error at offset %ld - bad character \\%03o = '%c'",
  495. ftell (state_file), c, c);
  496. }
  497. /* Used for lexical look-ahead. Retrieves the lexical token of rank
  498. DEPTH, starting with 0 when reading the state file. Gives null on
  499. end of file. */
  500. static struct state_token_st *
  501. peek_state_token (int depth)
  502. {
  503. int remdepth = depth;
  504. struct state_token_st **ptoken = &state_token;
  505. struct state_token_st *tok = NULL;
  506. while (remdepth >= 0)
  507. {
  508. if (*ptoken == NULL)
  509. {
  510. *ptoken = tok = read_a_state_token ();
  511. if (tok == NULL)
  512. return NULL;
  513. }
  514. tok = *ptoken;
  515. ptoken = &((*ptoken)->stok_next);
  516. remdepth--;
  517. }
  518. return tok;
  519. }
  520. /* Consume the next DEPTH tokens and free them. */
  521. static void
  522. next_state_tokens (int depth)
  523. {
  524. struct state_token_st *n;
  525. while (depth > 0)
  526. {
  527. if (state_token != NULL)
  528. {
  529. n = state_token->stok_next;
  530. free (state_token);
  531. state_token = n;
  532. }
  533. else
  534. fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
  535. depth--;
  536. }
  537. }
  538. /* Safely retrieve the lexical kind of a token. */
  539. static inline enum state_token_en
  540. state_token_kind (struct state_token_st *p)
  541. {
  542. if (p == NULL)
  543. return STOK_NONE;
  544. else
  545. return p->stok_kind;
  546. }
  547. /* Test if a token is a given name i.e. an identifier. */
  548. static inline bool
  549. state_token_is_name (struct state_token_st *p, const char *name)
  550. {
  551. if (p == NULL)
  552. return false;
  553. if (p->stok_kind != STOK_NAME)
  554. return false;
  555. return !strcmp (p->stok_un.stok_ident->stid_name, name);
  556. }
  557. /* Following routines are useful for serializing datas.
  558. *
  559. * We want to serialize :
  560. * - typedefs list
  561. * - structures list
  562. * - variables list
  563. *
  564. * So, we have one routine for each kind of data. The main writing
  565. * routine is write_state. The main reading routine is
  566. * read_state. Most writing routines write_state_FOO have a
  567. * corresponding reading routine read_state_FOO. Reading is done in a
  568. * recursive descending way, and any read error is fatal.
  569. */
  570. /* When reading the state, we need to remember the previously seen
  571. types by their state_number, since GTY-ed types are usually
  572. shared. */
  573. static htab_t state_seen_types;
  574. /* Return the length of a linked list made of pairs. */
  575. static int pair_list_length (pair_p list);
  576. /* Compute the length of a list of pairs, starting from the first
  577. one. */
  578. static int
  579. pair_list_length (pair_p list)
  580. {
  581. int nbpair = 0;
  582. pair_p l = NULL;
  583. for (l = list; l; l = l->next)
  584. nbpair++;
  585. return nbpair;
  586. }
  587. /* Write a file location. Files relative to $(srcdir) are quite
  588. frequent and are handled specially. This ensures that two gengtype
  589. state file-s produced by gengtype on the same GCC source tree are
  590. very similar and can be reasonably compared with diff, even if the
  591. two GCC source trees have different absolute paths. */
  592. void
  593. state_writer::write_state_fileloc (struct fileloc *floc)
  594. {
  595. if (floc != NULL && floc->line > 0)
  596. {
  597. const char *srcrelpath = NULL;
  598. gcc_assert (floc->file != NULL);
  599. /* Most of the files are inside $(srcdir) so it is worth to
  600. handle them specially. */
  601. srcrelpath = get_file_srcdir_relative_path (floc->file);
  602. if (srcrelpath != NULL)
  603. {
  604. begin_s_expr ("srcfileloc");
  605. write_state_a_string (srcrelpath);
  606. }
  607. else
  608. {
  609. begin_s_expr ("fileloc");
  610. write_state_a_string (get_input_file_name (floc->file));
  611. }
  612. fprintf (state_file, " %d", floc->line);
  613. end_s_expr ();
  614. }
  615. else
  616. fprintf (state_file, "nil ");
  617. }
  618. /* Write a list of fields. */
  619. void
  620. state_writer::write_state_fields (pair_p fields)
  621. {
  622. int nbfields = pair_list_length (fields);
  623. int nbpairs = 0;
  624. begin_s_expr ("fields");
  625. fprintf (state_file, "%d ", nbfields);
  626. nbpairs = write_state_pair_list (fields);
  627. gcc_assert (nbpairs == nbfields);
  628. end_s_expr ();
  629. }
  630. /* Write a null-terminated string in our lexical convention, very
  631. similar to the convention of C. */
  632. void
  633. state_writer::write_state_a_string (const char *s)
  634. {
  635. char c;
  636. write_any_indent (1);
  637. fputs (" \"", state_file);
  638. for (; *s != 0; s++)
  639. {
  640. c = *s;
  641. switch (c)
  642. {
  643. case '\a':
  644. fputs ("\\a", state_file);
  645. break;
  646. case '\b':
  647. fputs ("\\b", state_file);
  648. break;
  649. case '\t':
  650. fputs ("\\t", state_file);
  651. break;
  652. case '\n':
  653. fputs ("\\n", state_file);
  654. break;
  655. case '\v':
  656. fputs ("\\v", state_file);
  657. break;
  658. case '\f':
  659. fputs ("\\f", state_file);
  660. break;
  661. case '\r':
  662. fputs ("\\r", state_file);
  663. break;
  664. case '\"':
  665. fputs ("\\\"", state_file);
  666. break;
  667. case '\\':
  668. fputs ("\\\\", state_file);
  669. break;
  670. default:
  671. if (ISPRINT (c))
  672. putc (c, state_file);
  673. else
  674. fprintf (state_file, "\\x%02x", (unsigned) c);
  675. }
  676. }
  677. fputs ("\"", state_file);
  678. }
  679. /* Our option-s have three kinds, each with its writer. */
  680. void
  681. state_writer::write_state_string_option (options_p current)
  682. {
  683. write_any_indent (0);
  684. fprintf (state_file, "string ");
  685. if (current->info.string != NULL)
  686. write_state_a_string (current->info.string);
  687. else
  688. fprintf (state_file, " nil ");
  689. }
  690. void
  691. state_writer::write_state_type_option (options_p current)
  692. {
  693. write_any_indent (0);
  694. fprintf (state_file, "type ");
  695. write_state_type (current->info.type);
  696. }
  697. void
  698. state_writer::write_state_nested_option (options_p current)
  699. {
  700. write_any_indent (0);
  701. fprintf (state_file, "nested ");
  702. write_state_type (current->info.nested->type);
  703. if (current->info.nested->convert_from != NULL)
  704. write_state_a_string (current->info.nested->convert_from);
  705. else
  706. {
  707. write_any_indent (1);
  708. fprintf (state_file, " nil ");
  709. }
  710. if (current->info.nested->convert_to != NULL)
  711. write_state_a_string (current->info.nested->convert_to);
  712. else
  713. {
  714. write_any_indent (1);
  715. fprintf (state_file, " nil ");
  716. }
  717. }
  718. void
  719. state_writer::write_state_option (options_p current)
  720. {
  721. begin_s_expr ("option");
  722. write_any_indent (0);
  723. if (current->name != NULL)
  724. fprintf (state_file, "%s ", current->name);
  725. else
  726. fprintf (state_file, "nil ");
  727. switch (current->kind)
  728. {
  729. case OPTION_STRING:
  730. write_state_string_option (current);
  731. break;
  732. case OPTION_TYPE:
  733. write_state_type_option (current);
  734. break;
  735. case OPTION_NESTED:
  736. write_state_nested_option (current);
  737. break;
  738. default:
  739. fatal ("Option tag unknown");
  740. }
  741. /* Terminate the "option" s-expression. */
  742. end_s_expr ();
  743. }
  744. /* Write a list of GTY options. */
  745. void
  746. state_writer::write_state_options (options_p opt)
  747. {
  748. options_p current;
  749. if (opt == NULL)
  750. {
  751. write_any_indent (0);
  752. fprintf (state_file, "nil ");
  753. return;
  754. }
  755. begin_s_expr ("options");
  756. for (current = opt; current != NULL; current = current->next)
  757. write_state_option (current);
  758. end_s_expr ();
  759. }
  760. /* Write a bitmap representing a set of GCC front-end languages. */
  761. void
  762. state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
  763. {
  764. write_any_indent (0);
  765. fprintf (state_file, "%d ", (int) bitmap);
  766. }
  767. /* Write version information. */
  768. void
  769. state_writer::write_state_version (const char *version)
  770. {
  771. begin_s_expr ("version");
  772. write_state_a_string (version);
  773. end_s_expr ();
  774. }
  775. /* Write a scalar type. We have only two of these. */
  776. void
  777. state_writer::write_state_scalar_type (type_p current)
  778. {
  779. write_any_indent (0);
  780. if (current == &scalar_nonchar)
  781. fprintf (state_file, "scalar_nonchar ");
  782. else if (current == &scalar_char)
  783. fprintf (state_file, "scalar_char ");
  784. else
  785. fatal ("Unexpected type in write_state_scalar_type");
  786. write_state_common_type_content (current);
  787. }
  788. /* Write the string type. There is only one such thing! */
  789. void
  790. state_writer::write_state_string_type (type_p current)
  791. {
  792. if (current == &string_type)
  793. {
  794. write_any_indent (0);
  795. fprintf (state_file, "string ");
  796. write_state_common_type_content (current);
  797. }
  798. else
  799. fatal ("Unexpected type in write_state_string_type");
  800. }
  801. /* Write an undefined type. */
  802. void
  803. state_writer::write_state_undefined_type (type_p current)
  804. {
  805. DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
  806. current->state_number, current->u.s.tag);
  807. write_any_indent (0);
  808. fprintf (state_file, "undefined ");
  809. gcc_assert (current->gc_used == GC_UNUSED);
  810. write_state_common_type_content (current);
  811. if (current->u.s.tag != NULL)
  812. write_state_a_string (current->u.s.tag);
  813. else
  814. {
  815. write_any_indent (0);
  816. fprintf (state_file, "nil");
  817. }
  818. write_state_fileloc (type_lineloc (current));
  819. }
  820. /* Common code to write structure like types. */
  821. void
  822. state_writer::write_state_struct_union_type (type_p current,
  823. const char *kindstr)
  824. {
  825. DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
  826. current->state_number, current->u.s.tag);
  827. write_any_indent (0);
  828. fprintf (state_file, "%s ", kindstr);
  829. write_state_common_type_content (current);
  830. if (current->u.s.tag != NULL)
  831. write_state_a_string (current->u.s.tag);
  832. else
  833. {
  834. write_any_indent (0);
  835. fprintf (state_file, "nil");
  836. }
  837. write_state_fileloc (type_lineloc (current));
  838. write_state_fields (current->u.s.fields);
  839. write_state_options (current->u.s.opt);
  840. write_state_lang_bitmap (current->u.s.bitmap);
  841. }
  842. /* Write a GTY struct type. */
  843. void
  844. state_writer::write_state_struct_type (type_p current)
  845. {
  846. write_state_struct_union_type (current, "struct");
  847. write_state_type (current->u.s.lang_struct);
  848. write_state_type (current->u.s.base_class);
  849. }
  850. /* Write a GTY user-defined struct type. */
  851. void
  852. state_writer::write_state_user_struct_type (type_p current)
  853. {
  854. DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
  855. current->state_number, current->u.s.tag);
  856. write_any_indent (0);
  857. fprintf (state_file, "user_struct ");
  858. write_state_common_type_content (current);
  859. if (current->u.s.tag != NULL)
  860. write_state_a_string (current->u.s.tag);
  861. else
  862. {
  863. write_any_indent (0);
  864. fprintf (state_file, "nil");
  865. }
  866. write_state_fileloc (type_lineloc (current));
  867. write_state_fields (current->u.s.fields);
  868. }
  869. /* write a GTY union type. */
  870. void
  871. state_writer::write_state_union_type (type_p current)
  872. {
  873. write_state_struct_union_type (current, "union");
  874. write_state_type (current->u.s.lang_struct);
  875. }
  876. /* Write a lang_struct type. This is tricky and was painful to debug,
  877. we deal with the next field specifically within their lang_struct
  878. subfield, which points to a linked list of homonumous types.
  879. Change this function with extreme care, see also
  880. read_state_lang_struct_type. */
  881. void
  882. state_writer::write_state_lang_struct_type (type_p current)
  883. {
  884. int nbhomontype = 0;
  885. type_p hty = NULL;
  886. const char *homoname = 0;
  887. write_state_struct_union_type (current, "lang_struct");
  888. /* lang_struct-ures are particularly tricky, since their
  889. u.s.lang_struct field gives a list of homonymous struct-s or
  890. union-s! */
  891. DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
  892. for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
  893. {
  894. nbhomontype++;
  895. DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
  896. (void *) hty, hty->state_number, hty->u.s.tag);
  897. /* Every member of the homonymous list should have the same tag. */
  898. gcc_assert (union_or_struct_p (hty));
  899. gcc_assert (hty->u.s.lang_struct == current);
  900. if (!homoname)
  901. homoname = hty->u.s.tag;
  902. gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
  903. }
  904. begin_s_expr ("homotypes");
  905. fprintf (state_file, "%d", nbhomontype);
  906. for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
  907. write_state_type (hty);
  908. end_s_expr ();
  909. }
  910. /* Write a pointer type. */
  911. void
  912. state_writer::write_state_pointer_type (type_p current)
  913. {
  914. write_any_indent (0);
  915. fprintf (state_file, "pointer ");
  916. write_state_common_type_content (current);
  917. write_state_type (current->u.p);
  918. }
  919. /* Write an array type. */
  920. void
  921. state_writer::write_state_array_type (type_p current)
  922. {
  923. write_any_indent (0);
  924. fprintf (state_file, "array ");
  925. write_state_common_type_content (current);
  926. if (current->u.a.len != NULL)
  927. write_state_a_string (current->u.a.len);
  928. else
  929. {
  930. write_any_indent (1);
  931. fprintf (state_file, " nil");
  932. }
  933. write_any_indent (1);
  934. fprintf (state_file, " ");
  935. write_state_type (current->u.a.p);
  936. }
  937. /* Write the gc_used information. */
  938. void
  939. state_writer::write_state_gc_used (enum gc_used_enum gus)
  940. {
  941. write_any_indent (1);
  942. switch (gus)
  943. {
  944. case GC_UNUSED:
  945. fprintf (state_file, " gc_unused");
  946. break;
  947. case GC_USED:
  948. fprintf (state_file, " gc_used");
  949. break;
  950. case GC_MAYBE_POINTED_TO:
  951. fprintf (state_file, " gc_maybe_pointed_to");
  952. break;
  953. case GC_POINTED_TO:
  954. fprintf (state_file, " gc_pointed_to");
  955. break;
  956. default:
  957. gcc_unreachable ();
  958. }
  959. }
  960. /* Utility routine to write the common content of all types. Notice
  961. that the next field is *not* written on purpose. */
  962. void
  963. state_writer::write_state_common_type_content (type_p current)
  964. {
  965. write_any_indent (0);
  966. fprintf (state_file, "%d ", current->state_number);
  967. /* We do not write the next type, because list of types are
  968. explicitly written. However, lang_struct are special in that
  969. respect. See function write_state_lang_struct_type for more. */
  970. write_state_type (current->pointer_to);
  971. write_state_gc_used (current->gc_used);
  972. }
  973. /* The important and recursive routine writing GTY types as understood
  974. by gengtype. Types which have a positive state_number have already
  975. been seen and written. */
  976. void
  977. state_writer::write_state_type (type_p current)
  978. {
  979. write_any_indent (0);
  980. if (current == NULL)
  981. {
  982. fprintf (state_file, "nil ");
  983. return;
  984. }
  985. begin_s_expr ("type");
  986. if (current->state_number > 0)
  987. {
  988. write_any_indent (0);
  989. fprintf (state_file, "already_seen %d", current->state_number);
  990. }
  991. else
  992. {
  993. m_state_written_type_count++;
  994. DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count,
  995. (void *) current, current->state_number);
  996. current->state_number = m_state_written_type_count;
  997. switch (current->kind)
  998. {
  999. case TYPE_NONE:
  1000. gcc_unreachable ();
  1001. case TYPE_UNDEFINED:
  1002. write_state_undefined_type (current);
  1003. break;
  1004. case TYPE_STRUCT:
  1005. write_state_struct_type (current);
  1006. break;
  1007. case TYPE_USER_STRUCT:
  1008. write_state_user_struct_type (current);
  1009. break;
  1010. case TYPE_UNION:
  1011. write_state_union_type (current);
  1012. break;
  1013. case TYPE_POINTER:
  1014. write_state_pointer_type (current);
  1015. break;
  1016. case TYPE_ARRAY:
  1017. write_state_array_type (current);
  1018. break;
  1019. case TYPE_LANG_STRUCT:
  1020. write_state_lang_struct_type (current);
  1021. break;
  1022. case TYPE_SCALAR:
  1023. write_state_scalar_type (current);
  1024. break;
  1025. case TYPE_STRING:
  1026. write_state_string_type (current);
  1027. break;
  1028. }
  1029. }
  1030. /* Terminate the "type" s-expression. */
  1031. end_s_expr ();
  1032. }
  1033. /* Write a pair. */
  1034. void
  1035. state_writer::write_state_pair (pair_p current)
  1036. {
  1037. if (current == NULL)
  1038. {
  1039. write_any_indent (0);
  1040. fprintf (state_file, "nil)");
  1041. return;
  1042. }
  1043. begin_s_expr ("pair");
  1044. if (current->name != NULL)
  1045. write_state_a_string (current->name);
  1046. else
  1047. write_state_a_string ("nil");
  1048. write_state_type (current->type);
  1049. write_state_fileloc (&(current->line));
  1050. write_state_options (current->opt);
  1051. /* Terminate the "pair" s-expression. */
  1052. end_s_expr ();
  1053. }
  1054. /* Write a pair list and return the number of pairs written. */
  1055. int
  1056. state_writer::write_state_pair_list (pair_p list)
  1057. {
  1058. int nbpair = 0;
  1059. pair_p current;
  1060. for (current = list; current != NULL; current = current->next)
  1061. {
  1062. write_state_pair (current);
  1063. nbpair++;
  1064. }
  1065. return nbpair;
  1066. }
  1067. /* When writing imported linked lists, like typedefs, structures, ... we count
  1068. their length first and write it. This eases the reading, and enables an
  1069. extra verification on the number of actually read items. */
  1070. /* Write our typedefs. */
  1071. void
  1072. state_writer::write_state_typedefs (void)
  1073. {
  1074. int nbtypedefs = pair_list_length (typedefs);
  1075. int nbpairs = 0;
  1076. begin_s_expr ("typedefs");
  1077. fprintf (state_file, "%d", nbtypedefs);
  1078. nbpairs = write_state_pair_list (typedefs);
  1079. gcc_assert (nbpairs == nbtypedefs);
  1080. end_s_expr ();
  1081. if (verbosity_level >= 2)
  1082. printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
  1083. }
  1084. /* Write our structures. */
  1085. void
  1086. state_writer::write_state_structures (void)
  1087. {
  1088. int nbstruct = 0;
  1089. type_p current;
  1090. for (current = structures; current != NULL; current = current->next)
  1091. nbstruct++;
  1092. begin_s_expr ("structures");
  1093. fprintf (state_file, "%d", nbstruct);
  1094. for (current = structures; current != NULL; current = current->next)
  1095. {
  1096. write_new_line ();
  1097. write_state_type (current);
  1098. }
  1099. /* Terminate the "structures" s-expression. */
  1100. end_s_expr ();
  1101. if (verbosity_level >= 2)
  1102. printf ("%s wrote %d structures in state\n", progname, nbstruct);
  1103. }
  1104. /* Write our variables. */
  1105. void
  1106. state_writer::write_state_variables (void)
  1107. {
  1108. int nbvars = pair_list_length (variables);
  1109. int nbpairs = 0;
  1110. begin_s_expr ("variables");
  1111. fprintf (state_file, "%d", nbvars);
  1112. nbpairs = write_state_pair_list (variables);
  1113. gcc_assert (nbpairs == nbvars);
  1114. end_s_expr ();
  1115. if (verbosity_level >= 2)
  1116. printf ("%s wrote %d variables.\n", progname, nbvars);
  1117. }
  1118. /* Write the source directory. File locations within the source
  1119. directory have been written specifically. */
  1120. void
  1121. state_writer::write_state_srcdir (void)
  1122. {
  1123. begin_s_expr ("srcdir");
  1124. write_state_a_string (srcdir);
  1125. end_s_expr ();
  1126. }
  1127. /* Count and write the list of our files. */
  1128. void
  1129. state_writer::write_state_files_list (void)
  1130. {
  1131. int i = 0;
  1132. /* Write the list of files with their lang_bitmap. */
  1133. begin_s_expr ("fileslist");
  1134. fprintf (state_file, "%d", (int) num_gt_files);
  1135. for (i = 0; i < (int) num_gt_files; i++)
  1136. {
  1137. const char *cursrcrelpath = NULL;
  1138. const input_file *curfil = gt_files[i];
  1139. /* Most of the files are inside $(srcdir) so it is worth to
  1140. handle them specially. */
  1141. cursrcrelpath = get_file_srcdir_relative_path (curfil);
  1142. if (cursrcrelpath)
  1143. {
  1144. begin_s_expr ("srcfile");
  1145. fprintf (state_file, "%d ", get_lang_bitmap (curfil));
  1146. write_state_a_string (cursrcrelpath);
  1147. }
  1148. else
  1149. {
  1150. begin_s_expr ("file");
  1151. fprintf (state_file, "%d ", get_lang_bitmap (curfil));
  1152. write_state_a_string (get_input_file_name (curfil));
  1153. }
  1154. /* Terminate the inner s-expression (either "srcfile" or "file"). */
  1155. end_s_expr ();
  1156. }
  1157. /* Terminate the "fileslist" s-expression. */
  1158. end_s_expr ();
  1159. }
  1160. /* Write the list of GCC front-end languages. */
  1161. void
  1162. state_writer::write_state_languages (void)
  1163. {
  1164. int i = 0;
  1165. begin_s_expr ("languages");
  1166. fprintf (state_file, "%d", (int) num_lang_dirs);
  1167. for (i = 0; i < (int) num_lang_dirs; i++)
  1168. {
  1169. /* Languages names are identifiers, we expect only letters or
  1170. underscores or digits in them. In particular, C++ is not a
  1171. valid language name, but cp is valid. */
  1172. fprintf (state_file, " %s", lang_dir_names[i]);
  1173. }
  1174. end_s_expr ();
  1175. }
  1176. /* Write the trailer. */
  1177. static void
  1178. write_state_trailer (void)
  1179. {
  1180. /* This test should probably catch IO errors like disk full... */
  1181. if (fputs ("\n(!endfile)\n", state_file) == EOF)
  1182. fatal ("failed to write state trailer [%s]", xstrerror (errno));
  1183. }
  1184. /* The write_state routine is the only writing routine called by main
  1185. in gengtype.c. To avoid messing the state if gengtype is
  1186. interrupted or aborted, we write a temporary file and rename it
  1187. after having written it in totality. */
  1188. void
  1189. write_state (const char *state_path)
  1190. {
  1191. long statelen = 0;
  1192. time_t now = 0;
  1193. char *temp_state_path = NULL;
  1194. char tempsuffix[40];
  1195. time (&now);
  1196. /* We write a unique temporary file which is renamed when complete
  1197. * only. So even if gengtype is interrupted, the written state file
  1198. * won't be partially written, since the temporary file is not yet
  1199. * renamed in that case. */
  1200. memset (tempsuffix, 0, sizeof (tempsuffix));
  1201. snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
  1202. (int) getpid ());
  1203. temp_state_path = concat (state_path, tempsuffix, NULL);
  1204. state_file = fopen (temp_state_path, "w");
  1205. if (state_file == NULL)
  1206. fatal ("Failed to open file %s for writing state: %s",
  1207. temp_state_path, xstrerror (errno));
  1208. if (verbosity_level >= 3)
  1209. printf ("%s writing state file %s temporarily in %s\n",
  1210. progname, state_path, temp_state_path);
  1211. /* This is the first line of the state. Perhaps the file utility
  1212. could know about that, so don't change it often. */
  1213. fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
  1214. /* Output a few comments for humans. */
  1215. fprintf (state_file,
  1216. ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
  1217. fprintf (state_file,
  1218. ";;; The format of this file is tied to a particular version of GCC.\n");
  1219. fprintf (state_file,
  1220. ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
  1221. fprintf (state_file,
  1222. ";;; This file should be parsed by the same %s which wrote it.\n",
  1223. progname);
  1224. state_writer sw;
  1225. /* The first non-comment significant line gives the version string. */
  1226. sw.write_state_version (version_string);
  1227. sw.write_state_srcdir ();
  1228. sw.write_state_languages ();
  1229. sw.write_state_files_list ();
  1230. sw.write_state_structures ();
  1231. sw.write_state_typedefs ();
  1232. sw.write_state_variables ();
  1233. write_state_trailer ();
  1234. statelen = ftell (state_file);
  1235. if (ferror (state_file))
  1236. fatal ("output error when writing state file %s [%s]",
  1237. temp_state_path, xstrerror (errno));
  1238. if (fclose (state_file))
  1239. fatal ("failed to close state file %s [%s]",
  1240. temp_state_path, xstrerror (errno));
  1241. if (rename (temp_state_path, state_path))
  1242. fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
  1243. state_path, xstrerror (errno));
  1244. free (temp_state_path);
  1245. if (verbosity_level >= 1)
  1246. printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
  1247. progname, state_path, statelen, sw.m_state_written_type_count);
  1248. }
  1249. /** End of writing routines! The corresponding reading routines follow. **/
  1250. /* Forward declarations, since some read_state_* functions are
  1251. recursive! */
  1252. static void read_state_fileloc (struct fileloc *line);
  1253. static void read_state_options (options_p *opt);
  1254. static void read_state_type (type_p *current);
  1255. static void read_state_pair (pair_p *pair);
  1256. /* Return the number of pairs actually read. */
  1257. static int read_state_pair_list (pair_p *list);
  1258. static void read_state_fields (pair_p *fields);
  1259. static void read_state_common_type_content (type_p current);
  1260. /* Record into the state_seen_types hash-table a type which we are
  1261. reading, to enable recursive or circular references to it. */
  1262. static void
  1263. record_type (type_p type)
  1264. {
  1265. PTR *slot;
  1266. slot = htab_find_slot (state_seen_types, type, INSERT);
  1267. gcc_assert (slot);
  1268. *slot = type;
  1269. }
  1270. /* Read an already seen type. */
  1271. static void
  1272. read_state_already_seen_type (type_p *type)
  1273. {
  1274. struct state_token_st *t0 = peek_state_token (0);
  1275. if (state_token_kind (t0) == STOK_INTEGER)
  1276. {
  1277. PTR *slot = NULL;
  1278. struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
  1279. loctype.state_number = t0->stok_un.stok_num;
  1280. slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
  1281. if (slot == NULL)
  1282. {
  1283. fatal_reading_state (t0, "Unknown type");
  1284. }
  1285. next_state_tokens (1);
  1286. *type = (type_p) *slot;
  1287. }
  1288. else
  1289. {
  1290. fatal_reading_state (t0, "Bad seen type");
  1291. }
  1292. }
  1293. /* Read the scalar_nonchar type. */
  1294. static void
  1295. read_state_scalar_nonchar_type (type_p *type)
  1296. {
  1297. *type = &scalar_nonchar;
  1298. read_state_common_type_content (*type);
  1299. }
  1300. /* Read the scalar_char type. */
  1301. static void
  1302. read_state_scalar_char_type (type_p *type)
  1303. {
  1304. *type = &scalar_char;
  1305. read_state_common_type_content (*type);
  1306. }
  1307. /* Read the string_type. */
  1308. static void
  1309. read_state_string_type (type_p *type)
  1310. {
  1311. *type = &string_type;
  1312. read_state_common_type_content (*type);
  1313. }
  1314. /* Read a lang_bitmap representing a set of GCC front-end languages. */
  1315. static void
  1316. read_state_lang_bitmap (lang_bitmap *bitmap)
  1317. {
  1318. struct state_token_st *t;
  1319. t = peek_state_token (0);
  1320. if (state_token_kind (t) == STOK_INTEGER)
  1321. {
  1322. *bitmap = t->stok_un.stok_num;
  1323. next_state_tokens (1);
  1324. }
  1325. else
  1326. {
  1327. fatal_reading_state (t, "Bad syntax for bitmap");
  1328. }
  1329. }
  1330. /* Read an undefined type. */
  1331. static void
  1332. read_state_undefined_type (type_p type)
  1333. {
  1334. struct state_token_st *t0;
  1335. type->kind = TYPE_UNDEFINED;
  1336. read_state_common_type_content (type);
  1337. t0 = peek_state_token (0);
  1338. if (state_token_kind (t0) == STOK_STRING)
  1339. {
  1340. if (state_token_is_name (t0, "nil"))
  1341. {
  1342. type->u.s.tag = NULL;
  1343. DBGPRINTF ("read anonymous undefined type @%p #%d",
  1344. (void *) type, type->state_number);
  1345. }
  1346. else
  1347. {
  1348. type->u.s.tag = xstrdup (t0->stok_un.stok_string);
  1349. DBGPRINTF ("read undefined type @%p #%d '%s'",
  1350. (void *) type, type->state_number, type->u.s.tag);
  1351. }
  1352. next_state_tokens (1);
  1353. read_state_fileloc (&(type->u.s.line));
  1354. }
  1355. else
  1356. {
  1357. fatal_reading_state (t0, "Bad tag in undefined type");
  1358. }
  1359. }
  1360. /* Read a GTY-ed struct type. */
  1361. static void
  1362. read_state_struct_type (type_p type)
  1363. {
  1364. struct state_token_st *t0;
  1365. type->kind = TYPE_STRUCT;
  1366. read_state_common_type_content (type);
  1367. t0 = peek_state_token (0);
  1368. if (state_token_kind (t0) == STOK_STRING)
  1369. {
  1370. if (state_token_is_name (t0, "nil"))
  1371. {
  1372. type->u.s.tag = NULL;
  1373. DBGPRINTF ("read anonymous struct type @%p #%d",
  1374. (void *) type, type->state_number);
  1375. }
  1376. else
  1377. {
  1378. type->u.s.tag = xstrdup (t0->stok_un.stok_string);
  1379. DBGPRINTF ("read struct type @%p #%d '%s'",
  1380. (void *) type, type->state_number, type->u.s.tag);
  1381. }
  1382. next_state_tokens (1);
  1383. read_state_fileloc (&(type->u.s.line));
  1384. read_state_fields (&(type->u.s.fields));
  1385. read_state_options (&(type->u.s.opt));
  1386. read_state_lang_bitmap (&(type->u.s.bitmap));
  1387. read_state_type (&(type->u.s.lang_struct));
  1388. read_state_type (&(type->u.s.base_class));
  1389. if (type->u.s.base_class)
  1390. add_subclass (type->u.s.base_class, type);
  1391. }
  1392. else
  1393. {
  1394. fatal_reading_state (t0, "Bad tag in struct type");
  1395. }
  1396. }
  1397. /* Read a GTY-ed user-provided struct TYPE. */
  1398. static void
  1399. read_state_user_struct_type (type_p type)
  1400. {
  1401. struct state_token_st *t0;
  1402. type->kind = TYPE_USER_STRUCT;
  1403. read_state_common_type_content (type);
  1404. t0 = peek_state_token (0);
  1405. if (state_token_kind (t0) == STOK_STRING)
  1406. {
  1407. if (state_token_is_name (t0, "nil"))
  1408. {
  1409. type->u.s.tag = NULL;
  1410. DBGPRINTF ("read anonymous struct type @%p #%d",
  1411. (void *) type, type->state_number);
  1412. }
  1413. else
  1414. {
  1415. type->u.s.tag = xstrdup (t0->stok_un.stok_string);
  1416. DBGPRINTF ("read struct type @%p #%d '%s'",
  1417. (void *) type, type->state_number, type->u.s.tag);
  1418. }
  1419. next_state_tokens (1);
  1420. read_state_fileloc (&(type->u.s.line));
  1421. read_state_fields (&(type->u.s.fields));
  1422. }
  1423. else
  1424. {
  1425. fatal_reading_state (t0, "Bad tag in user-struct type");
  1426. }
  1427. }
  1428. /* Read a GTY-ed union type. */
  1429. static void
  1430. read_state_union_type (type_p type)
  1431. {
  1432. struct state_token_st *t0;
  1433. type->kind = TYPE_UNION;
  1434. read_state_common_type_content (type);
  1435. t0 = peek_state_token (0);
  1436. if (state_token_kind (t0) == STOK_STRING)
  1437. {
  1438. if (state_token_is_name (t0, "nil"))
  1439. {
  1440. type->u.s.tag = NULL;
  1441. DBGPRINTF ("read anonymous union type @%p #%d",
  1442. (void *) type, type->state_number);
  1443. }
  1444. else
  1445. {
  1446. type->u.s.tag = xstrdup (t0->stok_un.stok_string);
  1447. DBGPRINTF ("read union type @%p #%d '%s'",
  1448. (void *) type, type->state_number, type->u.s.tag);
  1449. }
  1450. next_state_tokens (1);
  1451. read_state_fileloc (&(type->u.s.line));
  1452. read_state_fields (&(type->u.s.fields));
  1453. read_state_options (&(type->u.s.opt));
  1454. read_state_lang_bitmap (&(type->u.s.bitmap));
  1455. read_state_type (&(type->u.s.lang_struct));
  1456. }
  1457. else
  1458. fatal_reading_state (t0, "Bad tag in union type");
  1459. }
  1460. /* Read a GTY-ed pointer type. */
  1461. static void
  1462. read_state_pointer_type (type_p type)
  1463. {
  1464. type->kind = TYPE_POINTER;
  1465. read_state_common_type_content (type);
  1466. DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
  1467. read_state_type (&(type->u.p));
  1468. }
  1469. /* Read a GTY-ed array type. */
  1470. static void
  1471. read_state_array_type (type_p type)
  1472. {
  1473. struct state_token_st *t0;
  1474. type->kind = TYPE_ARRAY;
  1475. read_state_common_type_content (type);
  1476. t0 = peek_state_token (0);
  1477. if (state_token_kind (t0) == STOK_STRING)
  1478. {
  1479. type->u.a.len = xstrdup (t0->stok_un.stok_string);
  1480. DBGPRINTF ("read array type @%p #%d length '%s'",
  1481. (void *) type, type->state_number, type->u.a.len);
  1482. next_state_tokens (1);
  1483. }
  1484. else if (state_token_is_name (t0, "nil"))
  1485. {
  1486. type->u.a.len = NULL;
  1487. DBGPRINTF ("read array type @%p #%d without length",
  1488. (void *) type, type->state_number);
  1489. next_state_tokens (1);
  1490. }
  1491. else
  1492. fatal_reading_state (t0, "Bad array name type");
  1493. read_state_type (&(type->u.a.p));
  1494. }
  1495. /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
  1496. front-end languages. This is a tricky function and it was painful
  1497. to debug. Change it with extreme care. See also
  1498. write_state_lang_struct_type. */
  1499. static void
  1500. read_state_lang_struct_type (type_p type)
  1501. {
  1502. struct state_token_st *t0 = NULL;
  1503. struct state_token_st *t1 = NULL;
  1504. struct state_token_st *t2 = NULL;
  1505. type->kind = TYPE_LANG_STRUCT;
  1506. read_state_common_type_content (type);
  1507. t0 = peek_state_token (0);
  1508. if (state_token_kind (t0) == STOK_STRING)
  1509. {
  1510. if (state_token_is_name (t0, "nil"))
  1511. {
  1512. DBGPRINTF ("read anonymous lang_struct type @%p #%d",
  1513. (void *) type, type->state_number);
  1514. type->u.s.tag = NULL;
  1515. }
  1516. else
  1517. {
  1518. type->u.s.tag = xstrdup (t0->stok_un.stok_string);
  1519. DBGPRINTF ("read lang_struct type @%p #%d '%s'",
  1520. (void *) type, type->state_number, type->u.s.tag);
  1521. }
  1522. next_state_tokens (1);
  1523. }
  1524. else
  1525. fatal_reading_state (t0, "Bad tag in lang struct type");
  1526. read_state_fileloc (&(type->u.s.line));
  1527. read_state_fields (&(type->u.s.fields));
  1528. read_state_options (&(type->u.s.opt));
  1529. read_state_lang_bitmap (&(type->u.s.bitmap));
  1530. /* Within lang_struct-ures, the lang_struct field is a linked list
  1531. of homonymous types! */
  1532. t0 = peek_state_token (0);
  1533. t1 = peek_state_token (1);
  1534. t2 = peek_state_token (2);
  1535. /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
  1536. if (state_token_kind (t0) == STOK_LEFTPAR
  1537. && state_token_is_name (t1, "!homotypes")
  1538. && state_token_kind (t2) == STOK_INTEGER)
  1539. {
  1540. type_p *prevty = &type->u.s.lang_struct;
  1541. int nbhomotype = t2->stok_un.stok_num;
  1542. int i = 0;
  1543. t0 = t1 = t2 = NULL;
  1544. next_state_tokens (3);
  1545. for (i = 0; i < nbhomotype; i++)
  1546. {
  1547. read_state_type (prevty);
  1548. t0 = peek_state_token (0);
  1549. if (*prevty)
  1550. prevty = &(*prevty)->next;
  1551. else
  1552. fatal_reading_state (t0,
  1553. "expecting type in homotype list for lang_struct");
  1554. };
  1555. if (state_token_kind (t0) != STOK_RIGHTPAR)
  1556. fatal_reading_state (t0,
  1557. "expecting ) in homotype list for lang_struct");
  1558. next_state_tokens (1);
  1559. }
  1560. else
  1561. fatal_reading_state (t0, "expecting !homotypes for lang_struct");
  1562. }
  1563. /* Read the gc used information. */
  1564. static void
  1565. read_state_gc_used (enum gc_used_enum *pgus)
  1566. {
  1567. struct state_token_st *t0 = peek_state_token (0);
  1568. if (state_token_is_name (t0, "gc_unused"))
  1569. *pgus = GC_UNUSED;
  1570. else if (state_token_is_name (t0, "gc_used"))
  1571. *pgus = GC_USED;
  1572. else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
  1573. *pgus = GC_MAYBE_POINTED_TO;
  1574. else if (state_token_is_name (t0, "gc_pointed_to"))
  1575. *pgus = GC_POINTED_TO;
  1576. else
  1577. fatal_reading_state (t0, "invalid gc_used information");
  1578. next_state_tokens (1);
  1579. }
  1580. /* Utility function to read the common content of types. */
  1581. static void
  1582. read_state_common_type_content (type_p current)
  1583. {
  1584. struct state_token_st *t0 = peek_state_token (0);
  1585. if (state_token_kind (t0) == STOK_INTEGER)
  1586. {
  1587. current->state_number = t0->stok_un.stok_num;
  1588. next_state_tokens (1);
  1589. record_type (current);
  1590. }
  1591. else
  1592. fatal_reading_state_printf (t0,
  1593. "Expected integer for state_number line %d",
  1594. state_line);
  1595. /* We don't read the next field of the type. */
  1596. read_state_type (&current->pointer_to);
  1597. read_state_gc_used (&current->gc_used);
  1598. }
  1599. /* Read a GTY-ed type. */
  1600. void
  1601. read_state_type (type_p *current)
  1602. {
  1603. struct state_token_st *t0 = peek_state_token (0);
  1604. struct state_token_st *t1 = peek_state_token (1);
  1605. if (state_token_kind (t0) == STOK_LEFTPAR &&
  1606. state_token_is_name (t1, "!type"))
  1607. {
  1608. next_state_tokens (2);
  1609. t0 = peek_state_token (0);
  1610. if (state_token_is_name (t0, "already_seen"))
  1611. {
  1612. next_state_tokens (1);
  1613. read_state_already_seen_type (current);
  1614. }
  1615. else
  1616. {
  1617. t0 = peek_state_token (0);
  1618. if (state_token_is_name (t0, "scalar_nonchar"))
  1619. {
  1620. next_state_tokens (1);
  1621. read_state_scalar_nonchar_type (current);
  1622. }
  1623. else if (state_token_is_name (t0, "scalar_char"))
  1624. {
  1625. next_state_tokens (1);
  1626. read_state_scalar_char_type (current);
  1627. }
  1628. else if (state_token_is_name (t0, "string"))
  1629. {
  1630. next_state_tokens (1);
  1631. read_state_string_type (current);
  1632. }
  1633. else if (state_token_is_name (t0, "undefined"))
  1634. {
  1635. *current = XCNEW (struct type);
  1636. next_state_tokens (1);
  1637. read_state_undefined_type (*current);
  1638. }
  1639. else if (state_token_is_name (t0, "struct"))
  1640. {
  1641. *current = XCNEW (struct type);
  1642. next_state_tokens (1);
  1643. read_state_struct_type (*current);
  1644. }
  1645. else if (state_token_is_name (t0, "union"))
  1646. {
  1647. *current = XCNEW (struct type);
  1648. next_state_tokens (1);
  1649. read_state_union_type (*current);
  1650. }
  1651. else if (state_token_is_name (t0, "lang_struct"))
  1652. {
  1653. *current = XCNEW (struct type);
  1654. next_state_tokens (1);
  1655. read_state_lang_struct_type (*current);
  1656. }
  1657. else if (state_token_is_name (t0, "pointer"))
  1658. {
  1659. *current = XCNEW (struct type);
  1660. next_state_tokens (1);
  1661. read_state_pointer_type (*current);
  1662. }
  1663. else if (state_token_is_name (t0, "array"))
  1664. {
  1665. *current = XCNEW (struct type);
  1666. next_state_tokens (1);
  1667. read_state_array_type (*current);
  1668. }
  1669. else if (state_token_is_name (t0, "user_struct"))
  1670. {
  1671. *current = XCNEW (struct type);
  1672. next_state_tokens (1);
  1673. read_state_user_struct_type (*current);
  1674. }
  1675. else
  1676. fatal_reading_state (t0, "bad type in (!type");
  1677. }
  1678. t0 = peek_state_token (0);
  1679. if (state_token_kind (t0) != STOK_RIGHTPAR)
  1680. fatal_reading_state (t0, "missing ) in type");
  1681. next_state_tokens (1);
  1682. }
  1683. else if (state_token_is_name (t0, "nil"))
  1684. {
  1685. next_state_tokens (1);
  1686. *current = NULL;
  1687. }
  1688. else
  1689. fatal_reading_state (t0, "bad type syntax");
  1690. }
  1691. /* Read a file location. Files within the source directory are dealt
  1692. with specifically. */
  1693. void
  1694. read_state_fileloc (struct fileloc *floc)
  1695. {
  1696. bool issrcfile = false;
  1697. struct state_token_st *t0 = peek_state_token (0);
  1698. struct state_token_st *t1 = peek_state_token (1);
  1699. gcc_assert (floc != NULL);
  1700. gcc_assert (srcdir != NULL);
  1701. if (state_token_kind (t0) == STOK_LEFTPAR &&
  1702. (state_token_is_name (t1, "!fileloc")
  1703. || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
  1704. {
  1705. next_state_tokens (2);
  1706. t0 = peek_state_token (0);
  1707. t1 = peek_state_token (1);
  1708. if (state_token_kind (t0) == STOK_STRING &&
  1709. state_token_kind (t1) == STOK_INTEGER)
  1710. {
  1711. char *path = t0->stok_un.stok_string;
  1712. if (issrcfile)
  1713. {
  1714. static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
  1715. char *fullpath = concat (srcdir, dirsepstr, path, NULL);
  1716. floc->file = input_file_by_name (fullpath);
  1717. free (fullpath);
  1718. }
  1719. else
  1720. floc->file = input_file_by_name (path);
  1721. floc->line = t1->stok_un.stok_num;
  1722. next_state_tokens (2);
  1723. }
  1724. else
  1725. fatal_reading_state (t0,
  1726. "Bad fileloc syntax, expected path string and line");
  1727. t0 = peek_state_token (0);
  1728. if (state_token_kind (t0) != STOK_RIGHTPAR)
  1729. fatal_reading_state (t0, "Bad fileloc syntax, expected )");
  1730. next_state_tokens (1);
  1731. }
  1732. else if (state_token_is_name (t0, "nil"))
  1733. {
  1734. next_state_tokens (1);
  1735. floc->file = NULL;
  1736. floc->line = 0;
  1737. }
  1738. else
  1739. fatal_reading_state (t0, "Bad fileloc syntax");
  1740. }
  1741. /* Read the fields of a GTY-ed type. */
  1742. void
  1743. read_state_fields (pair_p *fields)
  1744. {
  1745. pair_p tmp = NULL;
  1746. struct state_token_st *t0 = peek_state_token (0);
  1747. struct state_token_st *t1 = peek_state_token (1);
  1748. struct state_token_st *t2 = peek_state_token (2);
  1749. if (state_token_kind (t0) == STOK_LEFTPAR
  1750. && state_token_is_name (t1, "!fields")
  1751. && state_token_kind (t2) == STOK_INTEGER)
  1752. {
  1753. int nbfields = t2->stok_un.stok_num;
  1754. int nbpairs = 0;
  1755. next_state_tokens (3);
  1756. nbpairs = read_state_pair_list (&tmp);
  1757. t0 = peek_state_token (0);
  1758. if (nbpairs != nbfields)
  1759. fatal_reading_state_printf
  1760. (t0,
  1761. "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
  1762. if (state_token_kind (t0) == STOK_RIGHTPAR)
  1763. next_state_tokens (1);
  1764. else
  1765. fatal_reading_state (t0, "Bad fields expecting )");
  1766. }
  1767. *fields = tmp;
  1768. }
  1769. /* Read a string option. */
  1770. static void
  1771. read_state_string_option (options_p opt)
  1772. {
  1773. struct state_token_st *t0 = peek_state_token (0);
  1774. opt->kind = OPTION_STRING;
  1775. if (state_token_kind (t0) == STOK_STRING)
  1776. {
  1777. opt->info.string = xstrdup (t0->stok_un.stok_string);
  1778. next_state_tokens (1);
  1779. }
  1780. else if (state_token_is_name (t0, "nil"))
  1781. {
  1782. opt->info.string = NULL;
  1783. next_state_tokens (1);
  1784. }
  1785. else
  1786. fatal_reading_state (t0, "Missing name in string option");
  1787. }
  1788. /* Read a type option. */
  1789. static void
  1790. read_state_type_option (options_p opt)
  1791. {
  1792. opt->kind = OPTION_TYPE;
  1793. read_state_type (&(opt->info.type));
  1794. }
  1795. /* Read a nested option. */
  1796. static void
  1797. read_state_nested_option (options_p opt)
  1798. {
  1799. struct state_token_st *t0;
  1800. opt->info.nested = XCNEW (struct nested_ptr_data);
  1801. opt->kind = OPTION_NESTED;
  1802. read_state_type (&(opt->info.nested->type));
  1803. t0 = peek_state_token (0);
  1804. if (state_token_kind (t0) == STOK_STRING)
  1805. {
  1806. opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
  1807. next_state_tokens (1);
  1808. }
  1809. else if (state_token_is_name (t0, "nil"))
  1810. {
  1811. opt->info.nested->convert_from = NULL;
  1812. next_state_tokens (1);
  1813. }
  1814. else
  1815. fatal_reading_state (t0, "Bad nested convert_from option");
  1816. t0 = peek_state_token (0);
  1817. if (state_token_kind (t0) == STOK_STRING)
  1818. {
  1819. opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
  1820. next_state_tokens (1);
  1821. }
  1822. else if (state_token_is_name (t0, "nil"))
  1823. {
  1824. opt->info.nested->convert_to = NULL;
  1825. next_state_tokens (1);
  1826. }
  1827. else
  1828. fatal_reading_state (t0, "Bad nested convert_from option");
  1829. }
  1830. /* Read an GTY option. */
  1831. static void
  1832. read_state_option (options_p *opt)
  1833. {
  1834. struct state_token_st *t0 = peek_state_token (0);
  1835. struct state_token_st *t1 = peek_state_token (1);
  1836. if (state_token_kind (t0) == STOK_LEFTPAR &&
  1837. state_token_is_name (t1, "!option"))
  1838. {
  1839. next_state_tokens (2);
  1840. t0 = peek_state_token (0);
  1841. if (state_token_kind (t0) == STOK_NAME)
  1842. {
  1843. *opt = XCNEW (struct options);
  1844. if (state_token_is_name (t0, "nil"))
  1845. (*opt)->name = NULL;
  1846. else
  1847. (*opt)->name = t0->stok_un.stok_ident->stid_name;
  1848. next_state_tokens (1);
  1849. t0 = peek_state_token (0);
  1850. if (state_token_kind (t0) == STOK_NAME)
  1851. {
  1852. if (state_token_is_name (t0, "string"))
  1853. {
  1854. next_state_tokens (1);
  1855. read_state_string_option (*opt);
  1856. }
  1857. else if (state_token_is_name (t0, "type"))
  1858. {
  1859. next_state_tokens (1);
  1860. read_state_type_option (*opt);
  1861. }
  1862. else if (state_token_is_name (t0, "nested"))
  1863. {
  1864. next_state_tokens (1);
  1865. read_state_nested_option (*opt);
  1866. }
  1867. else
  1868. fatal_reading_state (t0, "Bad option type");
  1869. t0 = peek_state_token (0);
  1870. if (state_token_kind (t0) != STOK_RIGHTPAR)
  1871. fatal_reading_state (t0, "Bad syntax in option, expecting )");
  1872. next_state_tokens (1);
  1873. }
  1874. else
  1875. fatal_reading_state (t0, "Missing option type");
  1876. }
  1877. else
  1878. fatal_reading_state (t0, "Bad name for option");
  1879. }
  1880. else
  1881. fatal_reading_state (t0, "Bad option, waiting for )");
  1882. }
  1883. /* Read a list of options. */
  1884. void
  1885. read_state_options (options_p *opt)
  1886. {
  1887. options_p head = NULL;
  1888. options_p previous = NULL;
  1889. options_p current_option = NULL;
  1890. struct state_token_st *t0 = peek_state_token (0);
  1891. struct state_token_st *t1 = peek_state_token (1);
  1892. if (state_token_kind (t0) == STOK_LEFTPAR &&
  1893. state_token_is_name (t1, "!options"))
  1894. {
  1895. next_state_tokens (2);
  1896. t0 = peek_state_token (0);
  1897. while (state_token_kind (t0) != STOK_RIGHTPAR)
  1898. {
  1899. read_state_option (&current_option);
  1900. if (head == NULL)
  1901. {
  1902. head = current_option;
  1903. previous = head;
  1904. }
  1905. else
  1906. {
  1907. previous->next = current_option;
  1908. previous = current_option;
  1909. }
  1910. t0 = peek_state_token (0);
  1911. }
  1912. next_state_tokens (1);
  1913. }
  1914. else if (state_token_is_name (t0, "nil"))
  1915. {
  1916. next_state_tokens (1);
  1917. }
  1918. else
  1919. fatal_reading_state (t0, "Bad options syntax");
  1920. *opt = head;
  1921. }
  1922. /* Read a version, and check against the version of the gengtype. */
  1923. static void
  1924. read_state_version (const char *version_string)
  1925. {
  1926. struct state_token_st *t0 = peek_state_token (0);
  1927. struct state_token_st *t1 = peek_state_token (1);
  1928. if (state_token_kind (t0) == STOK_LEFTPAR &&
  1929. state_token_is_name (t1, "!version"))
  1930. {
  1931. next_state_tokens (2);
  1932. t0 = peek_state_token (0);
  1933. t1 = peek_state_token (1);
  1934. if (state_token_kind (t0) == STOK_STRING &&
  1935. state_token_kind (t1) == STOK_RIGHTPAR)
  1936. {
  1937. /* Check that the read version string is the same as current
  1938. version. */
  1939. if (strcmp (version_string, t0->stok_un.stok_string))
  1940. fatal_reading_state_printf (t0,
  1941. "version string mismatch; expecting %s but got %s",
  1942. version_string,
  1943. t0->stok_un.stok_string);
  1944. next_state_tokens (2);
  1945. }
  1946. else
  1947. fatal_reading_state (t0, "Missing version or right parenthesis");
  1948. }
  1949. else
  1950. fatal_reading_state (t0, "Bad version syntax");
  1951. }
  1952. /* Read a pair. */
  1953. void
  1954. read_state_pair (pair_p *current)
  1955. {
  1956. struct state_token_st *t0 = peek_state_token (0);
  1957. struct state_token_st *t1 = peek_state_token (1);
  1958. if (state_token_kind (t0) == STOK_LEFTPAR &&
  1959. state_token_is_name (t1, "!pair"))
  1960. {
  1961. *current = XCNEW (struct pair);
  1962. next_state_tokens (2);
  1963. t0 = peek_state_token (0);
  1964. if (state_token_kind (t0) == STOK_STRING)
  1965. {
  1966. if (strcmp (t0->stok_un.stok_string, "nil") == 0)
  1967. {
  1968. (*current)->name = NULL;
  1969. }
  1970. else
  1971. {
  1972. (*current)->name = xstrdup (t0->stok_un.stok_string);
  1973. }
  1974. next_state_tokens (1);
  1975. read_state_type (&((*current)->type));
  1976. read_state_fileloc (&((*current)->line));
  1977. read_state_options (&((*current)->opt));;
  1978. t0 = peek_state_token (0);
  1979. if (state_token_kind (t0) == STOK_RIGHTPAR)
  1980. {
  1981. next_state_tokens (1);
  1982. }
  1983. else
  1984. {
  1985. fatal_reading_state (t0, "Bad syntax for pair, )");
  1986. }
  1987. }
  1988. else
  1989. {
  1990. fatal_reading_state (t0, "Bad name for pair");
  1991. }
  1992. }
  1993. else if (state_token_kind (t0) == STOK_NAME &&
  1994. state_token_is_name (t0, "nil"))
  1995. {
  1996. next_state_tokens (1);
  1997. *current = NULL;
  1998. }
  1999. else
  2000. fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
  2001. state_token->stok_kind);
  2002. }
  2003. /* Return the number of pairs actually read. */
  2004. int
  2005. read_state_pair_list (pair_p *list)
  2006. {
  2007. int nbpair = 0;
  2008. pair_p head = NULL;
  2009. pair_p previous = NULL;
  2010. pair_p tmp = NULL;
  2011. struct state_token_st *t0 = peek_state_token (0);
  2012. while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
  2013. {
  2014. read_state_pair (&tmp);
  2015. if (head == NULL)
  2016. {
  2017. head = tmp;
  2018. previous = head;
  2019. }
  2020. else
  2021. {
  2022. previous->next = tmp;
  2023. previous = tmp;
  2024. }
  2025. t0 = peek_state_token (0);
  2026. nbpair++;
  2027. }
  2028. /* don't consume the ); the caller will eat it. */
  2029. *list = head;
  2030. return nbpair;
  2031. }
  2032. /* Read the typedefs. */
  2033. static void
  2034. read_state_typedefs (pair_p *typedefs)
  2035. {
  2036. int nbtypedefs = 0;
  2037. pair_p list = NULL;
  2038. struct state_token_st *t0 = peek_state_token (0);
  2039. struct state_token_st *t1 = peek_state_token (1);
  2040. struct state_token_st *t2 = peek_state_token (2);
  2041. if (state_token_kind (t0) == STOK_LEFTPAR
  2042. && state_token_is_name (t1, "!typedefs")
  2043. && state_token_kind (t2) == STOK_INTEGER)
  2044. {
  2045. int nbpairs = 0;
  2046. nbtypedefs = t2->stok_un.stok_num;
  2047. next_state_tokens (3);
  2048. nbpairs = read_state_pair_list (&list);
  2049. t0 = peek_state_token (0);
  2050. if (nbpairs != nbtypedefs)
  2051. fatal_reading_state_printf
  2052. (t0,
  2053. "invalid number of typedefs, expected %d but got %d",
  2054. nbtypedefs, nbpairs);
  2055. if (state_token_kind (t0) == STOK_RIGHTPAR)
  2056. next_state_tokens (1);
  2057. else
  2058. fatal_reading_state (t0, "Bad typedefs syntax )");
  2059. }
  2060. else
  2061. fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
  2062. if (verbosity_level >= 2)
  2063. printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
  2064. *typedefs = list;
  2065. }
  2066. /* Read the structures. */
  2067. static void
  2068. read_state_structures (type_p *structures)
  2069. {
  2070. type_p head = NULL;
  2071. type_p previous = NULL;
  2072. type_p tmp;
  2073. int nbstruct = 0, countstruct = 0;
  2074. struct state_token_st *t0 = peek_state_token (0);
  2075. struct state_token_st *t1 = peek_state_token (1);
  2076. struct state_token_st *t2 = peek_state_token (2);
  2077. if (state_token_kind (t0) == STOK_LEFTPAR
  2078. && state_token_is_name (t1, "!structures")
  2079. && state_token_kind (t2) == STOK_INTEGER)
  2080. {
  2081. nbstruct = t2->stok_un.stok_num;
  2082. next_state_tokens (3);
  2083. t0 = peek_state_token (0);
  2084. while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
  2085. {
  2086. tmp = NULL;
  2087. read_state_type (&tmp);
  2088. countstruct++;
  2089. if (head == NULL)
  2090. {
  2091. head = tmp;
  2092. previous = head;
  2093. }
  2094. else
  2095. {
  2096. previous->next = tmp;
  2097. previous = tmp;
  2098. }
  2099. t0 = peek_state_token (0);
  2100. }
  2101. next_state_tokens (1);
  2102. }
  2103. else
  2104. fatal_reading_state (t0, "Bad structures syntax");
  2105. if (countstruct != nbstruct)
  2106. fatal_reading_state_printf (NULL_STATE_TOKEN,
  2107. "expected %d structures but got %d",
  2108. nbstruct, countstruct);
  2109. if (verbosity_level >= 2)
  2110. printf ("%s read %d structures from state\n", progname, nbstruct);
  2111. *structures = head;
  2112. }
  2113. /* Read the variables. */
  2114. static void
  2115. read_state_variables (pair_p *variables)
  2116. {
  2117. pair_p list = NULL;
  2118. int nbvars = 0;
  2119. struct state_token_st *t0 = peek_state_token (0);
  2120. struct state_token_st *t1 = peek_state_token (1);
  2121. struct state_token_st *t2 = peek_state_token (2);
  2122. if (state_token_kind (t0) == STOK_LEFTPAR
  2123. && state_token_is_name (t1, "!variables")
  2124. && state_token_kind (t2) == STOK_INTEGER)
  2125. {
  2126. int nbpairs = 0;
  2127. nbvars = t2->stok_un.stok_num;
  2128. next_state_tokens (3);
  2129. nbpairs = read_state_pair_list (&list);
  2130. t0 = peek_state_token (0);
  2131. if (nbpairs != nbvars)
  2132. fatal_reading_state_printf
  2133. (t0, "Invalid number of variables, expected %d but got %d",
  2134. nbvars, nbpairs);
  2135. if (state_token_kind (t0) == STOK_RIGHTPAR)
  2136. next_state_tokens (1);
  2137. else
  2138. fatal_reading_state (t0, "Waiting for ) in variables");
  2139. }
  2140. else
  2141. fatal_reading_state (t0, "Bad variables syntax");
  2142. *variables = list;
  2143. if (verbosity_level >= 2)
  2144. printf ("%s read %d variables from state\n", progname, nbvars);
  2145. }
  2146. /* Read the source directory. */
  2147. static void
  2148. read_state_srcdir (void)
  2149. {
  2150. struct state_token_st *t0 = peek_state_token (0);
  2151. struct state_token_st *t1 = peek_state_token (1);
  2152. if (state_token_kind (t0) == STOK_LEFTPAR &&
  2153. state_token_is_name (t1, "!srcdir"))
  2154. {
  2155. next_state_tokens (2);
  2156. t0 = peek_state_token (0);
  2157. t1 = peek_state_token (1);
  2158. if (state_token_kind (t0) == STOK_STRING &&
  2159. state_token_kind (t1) == STOK_RIGHTPAR)
  2160. {
  2161. srcdir = xstrdup (t0->stok_un.stok_string);
  2162. srcdir_len = strlen (srcdir);
  2163. next_state_tokens (2);
  2164. return;
  2165. }
  2166. }
  2167. fatal_reading_state (t0, "Bad srcdir in state_file");
  2168. }
  2169. /* Read the sequence of GCC front-end languages. */
  2170. static void
  2171. read_state_languages (void)
  2172. {
  2173. struct state_token_st *t0 = peek_state_token (0);
  2174. struct state_token_st *t1 = peek_state_token (1);
  2175. struct state_token_st *t2 = peek_state_token (2);
  2176. if (state_token_kind (t0) == STOK_LEFTPAR
  2177. && state_token_is_name (t1, "!languages")
  2178. && state_token_kind (t2) == STOK_INTEGER)
  2179. {
  2180. int i = 0;
  2181. num_lang_dirs = t2->stok_un.stok_num;
  2182. lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
  2183. next_state_tokens (3);
  2184. t0 = t1 = t2 = NULL;
  2185. for (i = 0; i < (int) num_lang_dirs; i++)
  2186. {
  2187. t0 = peek_state_token (0);
  2188. if (state_token_kind (t0) != STOK_NAME)
  2189. fatal_reading_state (t0, "expecting language name in state file");
  2190. lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
  2191. next_state_tokens (1);
  2192. }
  2193. t0 = peek_state_token (0);
  2194. if (state_token_kind (t0) != STOK_RIGHTPAR)
  2195. fatal_reading_state (t0, "missing ) in languages list of state file");
  2196. next_state_tokens (1);
  2197. }
  2198. else
  2199. fatal_reading_state (t0, "expecting languages list in state file");
  2200. }
  2201. /* Read the sequence of files. */
  2202. static void
  2203. read_state_files_list (void)
  2204. {
  2205. struct state_token_st *t0 = peek_state_token (0);
  2206. struct state_token_st *t1 = peek_state_token (1);
  2207. struct state_token_st *t2 = peek_state_token (2);
  2208. if (state_token_kind (t0) == STOK_LEFTPAR
  2209. && state_token_is_name (t1, "!fileslist")
  2210. && state_token_kind (t2) == STOK_INTEGER)
  2211. {
  2212. int i = 0;
  2213. num_gt_files = t2->stok_un.stok_num;
  2214. next_state_tokens (3);
  2215. t0 = t1 = t2 = NULL;
  2216. gt_files = XCNEWVEC (const input_file *, num_gt_files);
  2217. for (i = 0; i < (int) num_gt_files; i++)
  2218. {
  2219. bool issrcfile = FALSE;
  2220. t0 = t1 = t2 = NULL;
  2221. t0 = peek_state_token (0);
  2222. t1 = peek_state_token (1);
  2223. t2 = peek_state_token (2);
  2224. if (state_token_kind (t0) == STOK_LEFTPAR
  2225. && (state_token_is_name (t1, "!file")
  2226. || (issrcfile = state_token_is_name (t1, "!srcfile")))
  2227. && state_token_kind (t2) == STOK_INTEGER)
  2228. {
  2229. lang_bitmap bmap = t2->stok_un.stok_num;
  2230. next_state_tokens (3);
  2231. t0 = t1 = t2 = NULL;
  2232. t0 = peek_state_token (0);
  2233. t1 = peek_state_token (1);
  2234. if (state_token_kind (t0) == STOK_STRING
  2235. && state_token_kind (t1) == STOK_RIGHTPAR)
  2236. {
  2237. const char *fnam = t0->stok_un.stok_string;
  2238. /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
  2239. input_file *curgt = NULL;
  2240. if (issrcfile)
  2241. {
  2242. static const char dirsepstr[2] =
  2243. { DIR_SEPARATOR, (char) 0 };
  2244. char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
  2245. curgt = input_file_by_name (fullpath);
  2246. free (fullpath);
  2247. }
  2248. else
  2249. curgt = input_file_by_name (fnam);
  2250. set_lang_bitmap (curgt, bmap);
  2251. gt_files[i] = curgt;
  2252. next_state_tokens (2);
  2253. }
  2254. else
  2255. fatal_reading_state (t0,
  2256. "bad file in !fileslist of state file");
  2257. }
  2258. else
  2259. fatal_reading_state (t0,
  2260. "expecting file in !fileslist of state file");
  2261. };
  2262. t0 = peek_state_token (0);
  2263. if (state_token_kind (t0) != STOK_RIGHTPAR)
  2264. fatal_reading_state (t0, "missing ) for !fileslist in state file");
  2265. next_state_tokens (1);
  2266. }
  2267. else
  2268. fatal_reading_state (t0, "missing !fileslist in state file");
  2269. }
  2270. /* Read the trailer. */
  2271. static void
  2272. read_state_trailer (void)
  2273. {
  2274. struct state_token_st *t0 = peek_state_token (0);
  2275. struct state_token_st *t1 = peek_state_token (1);
  2276. struct state_token_st *t2 = peek_state_token (2);
  2277. if (state_token_kind (t0) == STOK_LEFTPAR
  2278. && state_token_is_name (t1, "!endfile")
  2279. && state_token_kind (t2) == STOK_RIGHTPAR)
  2280. next_state_tokens (3);
  2281. else
  2282. fatal_reading_state (t0, "missing !endfile in state file");
  2283. }
  2284. /* Utility functions for the state_seen_types hash table. */
  2285. static unsigned
  2286. hash_type_number (const void *ty)
  2287. {
  2288. const struct type *type = (const struct type *) ty;
  2289. return type->state_number;
  2290. }
  2291. static int
  2292. equals_type_number (const void *ty1, const void *ty2)
  2293. {
  2294. const struct type *type1 = (const struct type *) ty1;
  2295. const struct type *type2 = (const struct type *) ty2;
  2296. return type1->state_number == type2->state_number;
  2297. }
  2298. static int
  2299. string_eq (const void *a, const void *b)
  2300. {
  2301. const char *a0 = (const char *)a;
  2302. const char *b0 = (const char *)b;
  2303. return (strcmp (a0, b0) == 0);
  2304. }
  2305. /* The function reading the state, called by main from gengtype.c. */
  2306. void
  2307. read_state (const char *path)
  2308. {
  2309. state_file = fopen (path, "r");
  2310. if (state_file == NULL)
  2311. fatal ("Failed to open state file %s for reading [%s]", path,
  2312. xstrerror (errno));
  2313. state_path = path;
  2314. state_line = 1;
  2315. if (verbosity_level >= 1)
  2316. {
  2317. printf ("%s reading state file %s;", progname, state_path);
  2318. if (verbosity_level >= 2)
  2319. putchar ('\n');
  2320. fflush (stdout);
  2321. }
  2322. state_seen_types =
  2323. htab_create (2017, hash_type_number, equals_type_number, NULL);
  2324. state_ident_tab =
  2325. htab_create (4027, htab_hash_string, string_eq, NULL);
  2326. read_state_version (version_string);
  2327. read_state_srcdir ();
  2328. read_state_languages ();
  2329. read_state_files_list ();
  2330. read_state_structures (&structures);
  2331. if (ferror (state_file))
  2332. fatal_reading_state_printf
  2333. (NULL_STATE_TOKEN, "input error while reading state [%s]",
  2334. xstrerror (errno));
  2335. read_state_typedefs (&typedefs);
  2336. read_state_variables (&variables);
  2337. read_state_trailer ();
  2338. if (verbosity_level >= 1)
  2339. {
  2340. printf ("%s read %ld bytes.\n", progname, ftell (state_file));
  2341. fflush (stdout);
  2342. };
  2343. if (fclose (state_file))
  2344. fatal ("failed to close read state file %s [%s]",
  2345. path, xstrerror (errno));
  2346. state_file = NULL;
  2347. state_path = NULL;
  2348. }
  2349. /* End of file gengtype-state.c. */