godump.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /* Output Go language descriptions of types.
  2. Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <iant@google.com>.
  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. /* This file is used during the build process to emit Go language
  17. descriptions of declarations from C header files. It uses the
  18. debug info hooks to emit the descriptions. The Go language
  19. descriptions then become part of the Go runtime support
  20. library.
  21. All global names are output with a leading underscore, so that they
  22. are all hidden in Go. */
  23. #include "config.h"
  24. #include "system.h"
  25. #include "coretypes.h"
  26. #include "diagnostic-core.h"
  27. #include "hash-set.h"
  28. #include "machmode.h"
  29. #include "vec.h"
  30. #include "double-int.h"
  31. #include "input.h"
  32. #include "alias.h"
  33. #include "symtab.h"
  34. #include "options.h"
  35. #include "wide-int.h"
  36. #include "inchash.h"
  37. #include "tree.h"
  38. #include "ggc.h"
  39. #include "hash-set.h"
  40. #include "obstack.h"
  41. #include "debug.h"
  42. #include "wide-int-print.h"
  43. #include "stor-layout.h"
  44. #include "defaults.h"
  45. /* We dump this information from the debug hooks. This gives us a
  46. stable and maintainable API to hook into. In order to work
  47. correctly when -g is used, we build our own hooks structure which
  48. wraps the hooks we need to change. */
  49. /* Our debug hooks. This is initialized by dump_go_spec_init. */
  50. static struct gcc_debug_hooks go_debug_hooks;
  51. /* The real debug hooks. */
  52. static const struct gcc_debug_hooks *real_debug_hooks;
  53. /* The file where we should write information. */
  54. static FILE *go_dump_file;
  55. /* A queue of decls to output. */
  56. static GTY(()) vec<tree, va_gc> *queue;
  57. /* A hash table of macros we have seen. */
  58. static htab_t macro_hash;
  59. /* The type of a value in macro_hash. */
  60. struct macro_hash_value
  61. {
  62. /* The name stored in the hash table. */
  63. char *name;
  64. /* The value of the macro. */
  65. char *value;
  66. };
  67. /* Returns the number of units necessary to represent an integer with the given
  68. PRECISION (in bits). */
  69. static inline unsigned int
  70. precision_to_units (unsigned int precision)
  71. {
  72. return (precision + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  73. }
  74. /* Calculate the hash value for an entry in the macro hash table. */
  75. static hashval_t
  76. macro_hash_hashval (const void *val)
  77. {
  78. const struct macro_hash_value *mhval = (const struct macro_hash_value *) val;
  79. return htab_hash_string (mhval->name);
  80. }
  81. /* Compare values in the macro hash table for equality. */
  82. static int
  83. macro_hash_eq (const void *v1, const void *v2)
  84. {
  85. const struct macro_hash_value *mhv1 = (const struct macro_hash_value *) v1;
  86. const struct macro_hash_value *mhv2 = (const struct macro_hash_value *) v2;
  87. return strcmp (mhv1->name, mhv2->name) == 0;
  88. }
  89. /* Free values deleted from the macro hash table. */
  90. static void
  91. macro_hash_del (void *v)
  92. {
  93. struct macro_hash_value *mhv = (struct macro_hash_value *) v;
  94. XDELETEVEC (mhv->name);
  95. XDELETEVEC (mhv->value);
  96. XDELETE (mhv);
  97. }
  98. /* For the string hash tables. */
  99. static int
  100. string_hash_eq (const void *y1, const void *y2)
  101. {
  102. return strcmp ((const char *) y1, (const char *) y2) == 0;
  103. }
  104. /* A macro definition. */
  105. static void
  106. go_define (unsigned int lineno, const char *buffer)
  107. {
  108. const char *p;
  109. const char *name_end;
  110. size_t out_len;
  111. char *out_buffer;
  112. char *q;
  113. bool saw_operand;
  114. bool need_operand;
  115. struct macro_hash_value *mhval;
  116. char *copy;
  117. hashval_t hashval;
  118. void **slot;
  119. real_debug_hooks->define (lineno, buffer);
  120. /* Skip macro functions. */
  121. for (p = buffer; *p != '\0' && *p != ' '; ++p)
  122. if (*p == '(')
  123. return;
  124. if (*p == '\0')
  125. return;
  126. name_end = p;
  127. ++p;
  128. if (*p == '\0')
  129. return;
  130. copy = XNEWVEC (char, name_end - buffer + 1);
  131. memcpy (copy, buffer, name_end - buffer);
  132. copy[name_end - buffer] = '\0';
  133. mhval = XNEW (struct macro_hash_value);
  134. mhval->name = copy;
  135. mhval->value = NULL;
  136. hashval = htab_hash_string (copy);
  137. slot = htab_find_slot_with_hash (macro_hash, mhval, hashval, NO_INSERT);
  138. /* For simplicity, we force all names to be hidden by adding an
  139. initial underscore, and let the user undo this as needed. */
  140. out_len = strlen (p) * 2 + 1;
  141. out_buffer = XNEWVEC (char, out_len);
  142. q = out_buffer;
  143. saw_operand = false;
  144. need_operand = false;
  145. while (*p != '\0')
  146. {
  147. switch (*p)
  148. {
  149. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  150. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  151. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  152. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  153. case 'Y': case 'Z':
  154. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  155. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  156. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  157. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  158. case 'y': case 'z':
  159. case '_':
  160. {
  161. /* The start of an identifier. Technically we should also
  162. worry about UTF-8 identifiers, but they are not a
  163. problem for practical uses of -fdump-go-spec so we
  164. don't worry about them. */
  165. const char *start;
  166. char *n;
  167. struct macro_hash_value idval;
  168. if (saw_operand)
  169. goto unknown;
  170. start = p;
  171. while (ISALNUM (*p) || *p == '_')
  172. ++p;
  173. n = XALLOCAVEC (char, p - start + 1);
  174. memcpy (n, start, p - start);
  175. n[p - start] = '\0';
  176. idval.name = n;
  177. idval.value = NULL;
  178. if (htab_find (macro_hash, &idval) == NULL)
  179. {
  180. /* This is a reference to a name which was not defined
  181. as a macro. */
  182. goto unknown;
  183. }
  184. *q++ = '_';
  185. memcpy (q, start, p - start);
  186. q += p - start;
  187. saw_operand = true;
  188. need_operand = false;
  189. }
  190. break;
  191. case '.':
  192. if (!ISDIGIT (p[1]))
  193. goto unknown;
  194. /* Fall through. */
  195. case '0': case '1': case '2': case '3': case '4':
  196. case '5': case '6': case '7': case '8': case '9':
  197. {
  198. const char *start;
  199. bool is_hex;
  200. start = p;
  201. is_hex = false;
  202. if (*p == '0' && (p[1] == 'x' || p[1] == 'X'))
  203. {
  204. p += 2;
  205. is_hex = true;
  206. }
  207. while (ISDIGIT (*p) || *p == '.' || *p == 'e' || *p == 'E'
  208. || (is_hex
  209. && ((*p >= 'a' && *p <= 'f')
  210. || (*p >= 'A' && *p <= 'F'))))
  211. ++p;
  212. memcpy (q, start, p - start);
  213. q += p - start;
  214. while (*p == 'u' || *p == 'U' || *p == 'l' || *p == 'L'
  215. || *p == 'f' || *p == 'F'
  216. || *p == 'd' || *p == 'D')
  217. {
  218. /* Go doesn't use any of these trailing type
  219. modifiers. */
  220. ++p;
  221. }
  222. /* We'll pick up the exponent, if any, as an
  223. expression. */
  224. saw_operand = true;
  225. need_operand = false;
  226. }
  227. break;
  228. case ' ': case '\t':
  229. *q++ = *p++;
  230. break;
  231. case '(':
  232. /* Always OK, not part of an operand, presumed to start an
  233. operand. */
  234. *q++ = *p++;
  235. saw_operand = false;
  236. need_operand = false;
  237. break;
  238. case ')':
  239. /* OK if we don't need an operand, and presumed to indicate
  240. an operand. */
  241. if (need_operand)
  242. goto unknown;
  243. *q++ = *p++;
  244. saw_operand = true;
  245. break;
  246. case '+': case '-':
  247. /* Always OK, but not part of an operand. */
  248. *q++ = *p++;
  249. saw_operand = false;
  250. break;
  251. case '*': case '/': case '%': case '|': case '&': case '^':
  252. /* Must be a binary operator. */
  253. if (!saw_operand)
  254. goto unknown;
  255. *q++ = *p++;
  256. saw_operand = false;
  257. need_operand = true;
  258. break;
  259. case '=':
  260. *q++ = *p++;
  261. if (*p != '=')
  262. goto unknown;
  263. /* Must be a binary operator. */
  264. if (!saw_operand)
  265. goto unknown;
  266. *q++ = *p++;
  267. saw_operand = false;
  268. need_operand = true;
  269. break;
  270. case '!':
  271. *q++ = *p++;
  272. if (*p == '=')
  273. {
  274. /* Must be a binary operator. */
  275. if (!saw_operand)
  276. goto unknown;
  277. *q++ = *p++;
  278. saw_operand = false;
  279. need_operand = true;
  280. }
  281. else
  282. {
  283. /* Must be a unary operator. */
  284. if (saw_operand)
  285. goto unknown;
  286. need_operand = true;
  287. }
  288. break;
  289. case '<': case '>':
  290. /* Must be a binary operand, may be << or >> or <= or >=. */
  291. if (!saw_operand)
  292. goto unknown;
  293. *q++ = *p++;
  294. if (*p == *(p - 1) || *p == '=')
  295. *q++ = *p++;
  296. saw_operand = false;
  297. need_operand = true;
  298. break;
  299. case '~':
  300. /* Must be a unary operand, must be translated for Go. */
  301. if (saw_operand)
  302. goto unknown;
  303. *q++ = '^';
  304. p++;
  305. need_operand = true;
  306. break;
  307. case '"':
  308. case '\'':
  309. {
  310. char quote;
  311. int count;
  312. if (saw_operand)
  313. goto unknown;
  314. quote = *p;
  315. *q++ = *p++;
  316. count = 0;
  317. while (*p != quote)
  318. {
  319. int c;
  320. if (*p == '\0')
  321. goto unknown;
  322. ++count;
  323. if (*p != '\\')
  324. {
  325. *q++ = *p++;
  326. continue;
  327. }
  328. *q++ = *p++;
  329. switch (*p)
  330. {
  331. case '0': case '1': case '2': case '3':
  332. case '4': case '5': case '6': case '7':
  333. c = 0;
  334. while (*p >= '0' && *p <= '7')
  335. {
  336. *q++ = *p++;
  337. ++c;
  338. }
  339. /* Go octal characters are always 3
  340. digits. */
  341. if (c != 3)
  342. goto unknown;
  343. break;
  344. case 'x':
  345. *q++ = *p++;
  346. c = 0;
  347. while (ISXDIGIT (*p))
  348. {
  349. *q++ = *p++;
  350. ++c;
  351. }
  352. /* Go hex characters are always 2 digits. */
  353. if (c != 2)
  354. goto unknown;
  355. break;
  356. case 'a': case 'b': case 'f': case 'n': case 'r':
  357. case 't': case 'v': case '\\': case '\'': case '"':
  358. *q++ = *p++;
  359. break;
  360. default:
  361. goto unknown;
  362. }
  363. }
  364. *q++ = *p++;
  365. if (quote == '\'' && count != 1)
  366. goto unknown;
  367. saw_operand = true;
  368. need_operand = false;
  369. break;
  370. }
  371. default:
  372. goto unknown;
  373. }
  374. }
  375. if (need_operand)
  376. goto unknown;
  377. gcc_assert ((size_t) (q - out_buffer) < out_len);
  378. *q = '\0';
  379. mhval->value = out_buffer;
  380. if (slot == NULL)
  381. {
  382. slot = htab_find_slot_with_hash (macro_hash, mhval, hashval, INSERT);
  383. gcc_assert (slot != NULL && *slot == NULL);
  384. }
  385. else
  386. {
  387. if (*slot != NULL)
  388. macro_hash_del (*slot);
  389. }
  390. *slot = mhval;
  391. return;
  392. unknown:
  393. fprintf (go_dump_file, "// unknowndefine %s\n", buffer);
  394. if (slot != NULL)
  395. htab_clear_slot (macro_hash, slot);
  396. XDELETEVEC (out_buffer);
  397. XDELETEVEC (copy);
  398. }
  399. /* A macro undef. */
  400. static void
  401. go_undef (unsigned int lineno, const char *buffer)
  402. {
  403. struct macro_hash_value mhval;
  404. void **slot;
  405. real_debug_hooks->undef (lineno, buffer);
  406. mhval.name = CONST_CAST (char *, buffer);
  407. mhval.value = NULL;
  408. slot = htab_find_slot (macro_hash, &mhval, NO_INSERT);
  409. if (slot != NULL)
  410. htab_clear_slot (macro_hash, slot);
  411. }
  412. /* A function or variable decl. */
  413. static void
  414. go_decl (tree decl)
  415. {
  416. if (!TREE_PUBLIC (decl)
  417. || DECL_IS_BUILTIN (decl)
  418. || DECL_NAME (decl) == NULL_TREE)
  419. return;
  420. vec_safe_push (queue, decl);
  421. }
  422. /* A function decl. */
  423. static void
  424. go_function_decl (tree decl)
  425. {
  426. real_debug_hooks->function_decl (decl);
  427. go_decl (decl);
  428. }
  429. /* A global variable decl. */
  430. static void
  431. go_global_decl (tree decl)
  432. {
  433. real_debug_hooks->global_decl (decl);
  434. go_decl (decl);
  435. }
  436. /* A type declaration. */
  437. static void
  438. go_type_decl (tree decl, int local)
  439. {
  440. real_debug_hooks->type_decl (decl, local);
  441. if (local || DECL_IS_BUILTIN (decl))
  442. return;
  443. if (DECL_NAME (decl) == NULL_TREE
  444. && (TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE
  445. || TREE_CODE (TYPE_NAME (TREE_TYPE (decl))) != IDENTIFIER_NODE)
  446. && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE)
  447. return;
  448. vec_safe_push (queue, decl);
  449. }
  450. /* A container for the data we pass around when generating information
  451. at the end of the compilation. */
  452. struct godump_container
  453. {
  454. /* DECLs that we have already seen. */
  455. hash_set<tree> decls_seen;
  456. /* Types which may potentially have to be defined as dummy
  457. types. */
  458. hash_set<const char *> pot_dummy_types;
  459. /* Go keywords. */
  460. htab_t keyword_hash;
  461. /* Global type definitions. */
  462. htab_t type_hash;
  463. /* Invalid types. */
  464. htab_t invalid_hash;
  465. /* Obstack used to write out a type definition. */
  466. struct obstack type_obstack;
  467. };
  468. /* Append an IDENTIFIER_NODE to OB. */
  469. static void
  470. go_append_string (struct obstack *ob, tree id)
  471. {
  472. obstack_grow (ob, IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
  473. }
  474. /* Given an integer PRECISION in bits, returns a constant string that is the
  475. matching go int or uint type (depending on the IS_UNSIGNED flag). Returns a
  476. NULL pointer if there is no matching go type. */
  477. static const char *
  478. go_get_uinttype_for_precision (unsigned int precision, bool is_unsigned)
  479. {
  480. switch (precision)
  481. {
  482. case 8:
  483. return is_unsigned ? "uint8" : "int8";
  484. case 16:
  485. return is_unsigned ? "uint16" : "int16";
  486. case 32:
  487. return is_unsigned ? "uint32" : "int32";
  488. case 64:
  489. return is_unsigned ? "uint64" : "int64";
  490. default:
  491. return NULL;
  492. }
  493. }
  494. /* Append an artificial variable name with the suffix _INDEX to OB. Returns
  495. INDEX + 1. */
  496. static unsigned int
  497. go_append_artificial_name (struct obstack *ob, unsigned int index)
  498. {
  499. char buf[100];
  500. /* FIXME: identifier may not be unique. */
  501. obstack_grow (ob, "Godump_", 7);
  502. snprintf (buf, sizeof buf, "%u", index);
  503. obstack_grow (ob, buf, strlen (buf));
  504. return index + 1;
  505. }
  506. /* Append the variable name from DECL to OB. If the name is in the
  507. KEYWORD_HASH, prepend an '_'. */
  508. static void
  509. go_append_decl_name (struct obstack *ob, tree decl, htab_t keyword_hash)
  510. {
  511. const char *var_name;
  512. void **slot;
  513. /* Start variable name with an underscore if a keyword. */
  514. var_name = IDENTIFIER_POINTER (DECL_NAME (decl));
  515. slot = htab_find_slot (keyword_hash, var_name, NO_INSERT);
  516. if (slot != NULL)
  517. obstack_1grow (ob, '_');
  518. go_append_string (ob, DECL_NAME (decl));
  519. }
  520. /* Appends a byte array with the necessary number of elements and the name
  521. "Godump_INDEX_pad" to pad from FROM_OFFSET to TO_OFFSET to OB assuming that
  522. the next field is automatically aligned to ALIGN_UNITS. Returns INDEX + 1,
  523. or INDEX if no padding had to be appended. The resulting offset where the
  524. next field is allocated is returned through RET_OFFSET. */
  525. static unsigned int
  526. go_append_padding (struct obstack *ob, unsigned int from_offset,
  527. unsigned int to_offset, unsigned int align_units,
  528. unsigned int index, unsigned int *ret_offset)
  529. {
  530. if (from_offset % align_units > 0)
  531. from_offset += align_units - (from_offset % align_units);
  532. gcc_assert (to_offset >= from_offset);
  533. if (to_offset > from_offset)
  534. {
  535. char buf[100];
  536. index = go_append_artificial_name (ob, index);
  537. snprintf (buf, sizeof buf, "_pad [%u]byte; ", to_offset - from_offset);
  538. obstack_grow (ob, buf, strlen (buf));
  539. }
  540. *ret_offset = to_offset;
  541. return index;
  542. }
  543. /* Appends an array of type TYPE_STRING with zero elements and the name
  544. "Godump_INDEX_align" to OB. If TYPE_STRING is a null pointer, ERROR_STRING
  545. is appended instead of the type. Returns INDEX + 1. */
  546. static unsigned int
  547. go_force_record_alignment (struct obstack *ob, const char *type_string,
  548. unsigned int index, const char *error_string)
  549. {
  550. index = go_append_artificial_name (ob, index);
  551. obstack_grow (ob, "_align ", 7);
  552. if (type_string == NULL)
  553. obstack_grow (ob, error_string, strlen (error_string));
  554. else
  555. {
  556. obstack_grow (ob, "[0]", 3);
  557. obstack_grow (ob, type_string, strlen (type_string));
  558. }
  559. obstack_grow (ob, "; ", 2);
  560. return index;
  561. }
  562. /* Write the Go version of TYPE to CONTAINER->TYPE_OBSTACK.
  563. USE_TYPE_NAME is true if we can simply use a type name here without
  564. needing to define it. IS_FUNC_OK is true if we can output a func
  565. type here; the "func" keyword will already have been added.
  566. Return true if the type can be represented in Go, false otherwise.
  567. P_ART_I is used for indexing artificial elements in nested structures and
  568. should always be a NULL pointer when called, except by certain recursive
  569. calls from go_format_type() itself. */
  570. static bool
  571. go_format_type (struct godump_container *container, tree type,
  572. bool use_type_name, bool is_func_ok, unsigned int *p_art_i,
  573. bool is_anon_record_or_union)
  574. {
  575. bool ret;
  576. struct obstack *ob;
  577. unsigned int art_i_dummy;
  578. bool is_union = false;
  579. if (p_art_i == NULL)
  580. {
  581. art_i_dummy = 0;
  582. p_art_i = &art_i_dummy;
  583. }
  584. ret = true;
  585. ob = &container->type_obstack;
  586. if (TYPE_NAME (type) != NULL_TREE
  587. && (container->decls_seen.contains (type)
  588. || container->decls_seen.contains (TYPE_NAME (type)))
  589. && (AGGREGATE_TYPE_P (type)
  590. || POINTER_TYPE_P (type)
  591. || TREE_CODE (type) == FUNCTION_TYPE))
  592. {
  593. tree name;
  594. void **slot;
  595. name = TYPE_IDENTIFIER (type);
  596. slot = htab_find_slot (container->invalid_hash, IDENTIFIER_POINTER (name),
  597. NO_INSERT);
  598. if (slot != NULL)
  599. ret = false;
  600. obstack_1grow (ob, '_');
  601. go_append_string (ob, name);
  602. return ret;
  603. }
  604. container->decls_seen.add (type);
  605. switch (TREE_CODE (type))
  606. {
  607. case ENUMERAL_TYPE:
  608. obstack_grow (ob, "int", 3);
  609. break;
  610. case TYPE_DECL:
  611. {
  612. void **slot;
  613. slot = htab_find_slot (container->invalid_hash,
  614. IDENTIFIER_POINTER (DECL_NAME (type)),
  615. NO_INSERT);
  616. if (slot != NULL)
  617. ret = false;
  618. obstack_1grow (ob, '_');
  619. go_append_string (ob, DECL_NAME (type));
  620. }
  621. break;
  622. case INTEGER_TYPE:
  623. {
  624. const char *s;
  625. char buf[100];
  626. s = go_get_uinttype_for_precision (TYPE_PRECISION (type),
  627. TYPE_UNSIGNED (type));
  628. if (s == NULL)
  629. {
  630. snprintf (buf, sizeof buf, "INVALID-int-%u%s",
  631. TYPE_PRECISION (type),
  632. TYPE_UNSIGNED (type) ? "u" : "");
  633. s = buf;
  634. ret = false;
  635. }
  636. obstack_grow (ob, s, strlen (s));
  637. }
  638. break;
  639. case REAL_TYPE:
  640. {
  641. const char *s;
  642. char buf[100];
  643. switch (TYPE_PRECISION (type))
  644. {
  645. case 32:
  646. s = "float32";
  647. break;
  648. case 64:
  649. s = "float64";
  650. break;
  651. default:
  652. snprintf (buf, sizeof buf, "INVALID-float-%u",
  653. TYPE_PRECISION (type));
  654. s = buf;
  655. ret = false;
  656. break;
  657. }
  658. obstack_grow (ob, s, strlen (s));
  659. }
  660. break;
  661. case COMPLEX_TYPE:
  662. {
  663. const char *s;
  664. char buf[100];
  665. tree real_type;
  666. real_type = TREE_TYPE (type);
  667. if (TREE_CODE (real_type) == REAL_TYPE)
  668. {
  669. switch (TYPE_PRECISION (real_type))
  670. {
  671. case 32:
  672. s = "complex64";
  673. break;
  674. case 64:
  675. s = "complex128";
  676. break;
  677. default:
  678. snprintf (buf, sizeof buf, "INVALID-complex-%u",
  679. 2 * TYPE_PRECISION (real_type));
  680. s = buf;
  681. ret = false;
  682. break;
  683. }
  684. }
  685. else
  686. {
  687. s = "INVALID-complex-non-real";
  688. ret = false;
  689. }
  690. obstack_grow (ob, s, strlen (s));
  691. }
  692. break;
  693. case BOOLEAN_TYPE:
  694. obstack_grow (ob, "bool", 4);
  695. break;
  696. case POINTER_TYPE:
  697. if (use_type_name
  698. && TYPE_NAME (TREE_TYPE (type)) != NULL_TREE
  699. && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type))
  700. || (POINTER_TYPE_P (TREE_TYPE (type))
  701. && (TREE_CODE (TREE_TYPE (TREE_TYPE (type)))
  702. == FUNCTION_TYPE))))
  703. {
  704. tree name;
  705. void **slot;
  706. name = TYPE_IDENTIFIER (TREE_TYPE (type));
  707. slot = htab_find_slot (container->invalid_hash,
  708. IDENTIFIER_POINTER (name), NO_INSERT);
  709. if (slot != NULL)
  710. ret = false;
  711. obstack_grow (ob, "*_", 2);
  712. go_append_string (ob, name);
  713. /* The pointer here can be used without the struct or union
  714. definition. So this struct or union is a potential dummy
  715. type. */
  716. if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type)))
  717. container->pot_dummy_types.add (IDENTIFIER_POINTER (name));
  718. return ret;
  719. }
  720. if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
  721. obstack_grow (ob, "func", 4);
  722. else
  723. obstack_1grow (ob, '*');
  724. if (VOID_TYPE_P (TREE_TYPE (type)))
  725. obstack_grow (ob, "byte", 4);
  726. else
  727. {
  728. if (!go_format_type (container, TREE_TYPE (type), use_type_name,
  729. true, NULL, false))
  730. ret = false;
  731. }
  732. break;
  733. case ARRAY_TYPE:
  734. obstack_1grow (ob, '[');
  735. if (TYPE_DOMAIN (type) != NULL_TREE
  736. && TREE_CODE (TYPE_DOMAIN (type)) == INTEGER_TYPE
  737. && TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
  738. && TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) == INTEGER_CST
  739. && tree_int_cst_sgn (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) == 0
  740. && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
  741. && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) == INTEGER_CST
  742. && tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
  743. {
  744. char buf[100];
  745. snprintf (buf, sizeof buf, HOST_WIDE_INT_PRINT_DEC "+1",
  746. tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))));
  747. obstack_grow (ob, buf, strlen (buf));
  748. }
  749. else
  750. obstack_1grow (ob, '0');
  751. obstack_1grow (ob, ']');
  752. if (!go_format_type (container, TREE_TYPE (type), use_type_name, false,
  753. NULL, false))
  754. ret = false;
  755. break;
  756. case UNION_TYPE:
  757. is_union = true;
  758. /* Fall through to RECORD_TYPE case. */
  759. case RECORD_TYPE:
  760. {
  761. unsigned int prev_field_end;
  762. unsigned int known_alignment;
  763. tree field;
  764. bool emitted_a_field;
  765. /* FIXME: Why is this necessary? Without it we can get a core
  766. dump on the s390x headers, or from a file containing simply
  767. "typedef struct S T;". */
  768. layout_type (type);
  769. prev_field_end = 0;
  770. known_alignment = 1;
  771. /* Anonymous records and unions are flattened, i.e. they are not put
  772. into "struct { ... }". */
  773. if (!is_anon_record_or_union)
  774. obstack_grow (ob, "struct { ", 9);
  775. for (field = TYPE_FIELDS (type), emitted_a_field = false;
  776. field != NULL_TREE;
  777. field = TREE_CHAIN (field))
  778. {
  779. if (TREE_CODE (field) != FIELD_DECL)
  780. continue;
  781. if (DECL_BIT_FIELD (field))
  782. /* Bit fields are replaced by padding. */
  783. continue;
  784. /* Only the first non-bitfield field is emitted for unions. */
  785. if (!is_union || !emitted_a_field)
  786. {
  787. /* Emit the field. */
  788. bool field_ok;
  789. bool is_anon_substructure;
  790. unsigned int decl_align_unit;
  791. unsigned int decl_offset;
  792. field_ok = true;
  793. emitted_a_field = true;
  794. is_anon_substructure =
  795. (DECL_NAME (field) == NULL
  796. && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
  797. || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE));
  798. /* Keep track of the alignment of named substructures, either
  799. of the whole record, or the alignment of the emitted field
  800. (for unions). */
  801. decl_align_unit = DECL_ALIGN_UNIT (field);
  802. if (!is_anon_substructure && decl_align_unit > known_alignment)
  803. known_alignment = decl_align_unit;
  804. /* Pad to start of field. */
  805. decl_offset =
  806. TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
  807. + precision_to_units
  808. (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field)));
  809. {
  810. unsigned int align_unit;
  811. /* For anonymous records and unions there is no automatic
  812. structure alignment, so use 1 as the alignment. */
  813. align_unit = (is_anon_substructure) ? 1 : decl_align_unit;
  814. *p_art_i = go_append_padding
  815. (ob, prev_field_end, decl_offset, align_unit, *p_art_i,
  816. &prev_field_end);
  817. }
  818. if (DECL_SIZE_UNIT (field))
  819. prev_field_end +=
  820. TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
  821. /* Emit the field name, but not for anonymous records and
  822. unions. */
  823. if (!is_anon_substructure)
  824. {
  825. if ((DECL_NAME (field) == NULL))
  826. *p_art_i = go_append_artificial_name (ob, *p_art_i);
  827. else
  828. go_append_decl_name
  829. (ob, field, container->keyword_hash);
  830. obstack_1grow (ob, ' ');
  831. }
  832. /* Do not expand type if a record or union type or a function
  833. pointer. */
  834. if (TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
  835. && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
  836. || (POINTER_TYPE_P (TREE_TYPE (field))
  837. && (TREE_CODE (TREE_TYPE (TREE_TYPE (field)))
  838. == FUNCTION_TYPE))))
  839. {
  840. tree name;
  841. void **slot;
  842. name = TYPE_IDENTIFIER (TREE_TYPE (field));
  843. slot = htab_find_slot (container->invalid_hash,
  844. IDENTIFIER_POINTER (name),
  845. NO_INSERT);
  846. if (slot != NULL)
  847. field_ok = false;
  848. obstack_1grow (ob, '_');
  849. go_append_string (ob, name);
  850. }
  851. else
  852. {
  853. if (!go_format_type (container, TREE_TYPE (field), true,
  854. false, p_art_i, is_anon_substructure))
  855. field_ok = false;
  856. }
  857. if (!is_anon_substructure)
  858. obstack_grow (ob, "; ", 2);
  859. if (!field_ok)
  860. ret = false;
  861. }
  862. }
  863. /* Padding. */
  864. {
  865. unsigned int align_unit;
  866. align_unit = (is_anon_record_or_union) ? 1 : TYPE_ALIGN_UNIT (type);
  867. *p_art_i = go_append_padding
  868. (ob, prev_field_end, TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type)),
  869. align_unit, *p_art_i, &prev_field_end);
  870. }
  871. /* Alignment. */
  872. if (!is_anon_record_or_union
  873. && known_alignment < TYPE_ALIGN_UNIT (type))
  874. {
  875. const char *s;
  876. char buf[100];
  877. /* Enforce proper record alignment. */
  878. s = go_get_uinttype_for_precision
  879. (TYPE_ALIGN (type), TYPE_UNSIGNED (type));
  880. if (s == NULL)
  881. {
  882. snprintf (buf, sizeof buf, "INVALID-int-%u%s",
  883. TYPE_ALIGN (type), TYPE_UNSIGNED (type) ? "u" : "");
  884. s = buf;
  885. ret = false;
  886. }
  887. *p_art_i = go_force_record_alignment (ob, s, *p_art_i, buf);
  888. }
  889. if (!is_anon_record_or_union)
  890. obstack_1grow (ob, '}');
  891. }
  892. break;
  893. case FUNCTION_TYPE:
  894. {
  895. tree arg_type;
  896. bool is_varargs;
  897. tree result;
  898. function_args_iterator iter;
  899. bool seen_arg;
  900. /* Go has no way to write a type which is a function but not a
  901. pointer to a function. */
  902. if (!is_func_ok)
  903. {
  904. obstack_grow (ob, "func*", 5);
  905. ret = false;
  906. }
  907. obstack_1grow (ob, '(');
  908. is_varargs = stdarg_p (type);
  909. seen_arg = false;
  910. FOREACH_FUNCTION_ARGS (type, arg_type, iter)
  911. {
  912. if (VOID_TYPE_P (arg_type))
  913. break;
  914. if (seen_arg)
  915. obstack_grow (ob, ", ", 2);
  916. if (!go_format_type (container, arg_type, true, false, NULL, false))
  917. ret = false;
  918. seen_arg = true;
  919. }
  920. if (is_varargs)
  921. {
  922. if (prototype_p (type))
  923. obstack_grow (ob, ", ", 2);
  924. obstack_grow (ob, "...interface{}", 14);
  925. }
  926. obstack_1grow (ob, ')');
  927. result = TREE_TYPE (type);
  928. if (!VOID_TYPE_P (result))
  929. {
  930. obstack_1grow (ob, ' ');
  931. if (!go_format_type (container, result, use_type_name, false, NULL,
  932. false))
  933. ret = false;
  934. }
  935. }
  936. break;
  937. default:
  938. obstack_grow (ob, "INVALID-type", 12);
  939. ret = false;
  940. break;
  941. }
  942. return ret;
  943. }
  944. /* Output the type which was built on the type obstack, and then free
  945. it. */
  946. static void
  947. go_output_type (struct godump_container *container)
  948. {
  949. struct obstack *ob;
  950. ob = &container->type_obstack;
  951. obstack_1grow (ob, '\0');
  952. fputs ((char *) obstack_base (ob), go_dump_file);
  953. obstack_free (ob, obstack_base (ob));
  954. }
  955. /* Output a function declaration. */
  956. static void
  957. go_output_fndecl (struct godump_container *container, tree decl)
  958. {
  959. if (!go_format_type (container, TREE_TYPE (decl), false, true, NULL, false))
  960. fprintf (go_dump_file, "// ");
  961. fprintf (go_dump_file, "func _%s ",
  962. IDENTIFIER_POINTER (DECL_NAME (decl)));
  963. go_output_type (container);
  964. fprintf (go_dump_file, " __asm__(\"%s\")\n",
  965. IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
  966. }
  967. /* Output a typedef or something like a struct definition. */
  968. static void
  969. go_output_typedef (struct godump_container *container, tree decl)
  970. {
  971. /* If we have an enum type, output the enum constants
  972. separately. */
  973. if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
  974. && TYPE_SIZE (TREE_TYPE (decl)) != 0
  975. && !container->decls_seen.contains (TREE_TYPE (decl))
  976. && (TYPE_CANONICAL (TREE_TYPE (decl)) == NULL_TREE
  977. || !container->decls_seen.contains
  978. (TYPE_CANONICAL (TREE_TYPE (decl)))))
  979. {
  980. tree element;
  981. for (element = TYPE_VALUES (TREE_TYPE (decl));
  982. element != NULL_TREE;
  983. element = TREE_CHAIN (element))
  984. {
  985. const char *name;
  986. struct macro_hash_value *mhval;
  987. void **slot;
  988. char buf[WIDE_INT_PRINT_BUFFER_SIZE];
  989. name = IDENTIFIER_POINTER (TREE_PURPOSE (element));
  990. /* Sometimes a name will be defined as both an enum constant
  991. and a macro. Avoid duplicate definition errors by
  992. treating enum constants as macros. */
  993. mhval = XNEW (struct macro_hash_value);
  994. mhval->name = xstrdup (name);
  995. mhval->value = NULL;
  996. slot = htab_find_slot (macro_hash, mhval, INSERT);
  997. if (*slot != NULL)
  998. macro_hash_del (*slot);
  999. if (tree_fits_shwi_p (TREE_VALUE (element)))
  1000. snprintf (buf, sizeof buf, HOST_WIDE_INT_PRINT_DEC,
  1001. tree_to_shwi (TREE_VALUE (element)));
  1002. else if (tree_fits_uhwi_p (TREE_VALUE (element)))
  1003. snprintf (buf, sizeof buf, HOST_WIDE_INT_PRINT_UNSIGNED,
  1004. tree_to_uhwi (TREE_VALUE (element)));
  1005. else
  1006. print_hex (element, buf);
  1007. mhval->value = xstrdup (buf);
  1008. *slot = mhval;
  1009. }
  1010. container->decls_seen.add (TREE_TYPE (decl));
  1011. if (TYPE_CANONICAL (TREE_TYPE (decl)) != NULL_TREE)
  1012. container->decls_seen.add (TYPE_CANONICAL (TREE_TYPE (decl)));
  1013. }
  1014. if (DECL_NAME (decl) != NULL_TREE)
  1015. {
  1016. void **slot;
  1017. const char *type;
  1018. type = IDENTIFIER_POINTER (DECL_NAME (decl));
  1019. /* If type defined already, skip. */
  1020. slot = htab_find_slot (container->type_hash, type, INSERT);
  1021. if (*slot != NULL)
  1022. return;
  1023. *slot = CONST_CAST (void *, (const void *) type);
  1024. if (!go_format_type (container, TREE_TYPE (decl), false, false, NULL,
  1025. false))
  1026. {
  1027. fprintf (go_dump_file, "// ");
  1028. slot = htab_find_slot (container->invalid_hash, type, INSERT);
  1029. *slot = CONST_CAST (void *, (const void *) type);
  1030. }
  1031. fprintf (go_dump_file, "type _%s ",
  1032. IDENTIFIER_POINTER (DECL_NAME (decl)));
  1033. go_output_type (container);
  1034. if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
  1035. {
  1036. HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
  1037. if (size > 0)
  1038. fprintf (go_dump_file,
  1039. "\nconst _sizeof_%s = " HOST_WIDE_INT_PRINT_DEC,
  1040. IDENTIFIER_POINTER (DECL_NAME (decl)),
  1041. size);
  1042. }
  1043. container->decls_seen.add (decl);
  1044. }
  1045. else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
  1046. {
  1047. void **slot;
  1048. const char *type;
  1049. HOST_WIDE_INT size;
  1050. type = IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE ((decl))));
  1051. /* If type defined already, skip. */
  1052. slot = htab_find_slot (container->type_hash, type, INSERT);
  1053. if (*slot != NULL)
  1054. return;
  1055. *slot = CONST_CAST (void *, (const void *) type);
  1056. if (!go_format_type (container, TREE_TYPE (decl), false, false, NULL,
  1057. false))
  1058. {
  1059. fprintf (go_dump_file, "// ");
  1060. slot = htab_find_slot (container->invalid_hash, type, INSERT);
  1061. *slot = CONST_CAST (void *, (const void *) type);
  1062. }
  1063. fprintf (go_dump_file, "type _%s ",
  1064. IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))));
  1065. go_output_type (container);
  1066. size = int_size_in_bytes (TREE_TYPE (decl));
  1067. if (size > 0)
  1068. fprintf (go_dump_file,
  1069. "\nconst _sizeof_%s = " HOST_WIDE_INT_PRINT_DEC,
  1070. IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))),
  1071. size);
  1072. }
  1073. else
  1074. return;
  1075. fprintf (go_dump_file, "\n");
  1076. }
  1077. /* Output a variable. */
  1078. static void
  1079. go_output_var (struct godump_container *container, tree decl)
  1080. {
  1081. bool is_valid;
  1082. tree type_name;
  1083. tree id;
  1084. if (container->decls_seen.contains (decl)
  1085. || container->decls_seen.contains (DECL_NAME (decl)))
  1086. return;
  1087. container->decls_seen.add (decl);
  1088. container->decls_seen.add (DECL_NAME (decl));
  1089. type_name = TYPE_NAME (TREE_TYPE (decl));
  1090. id = NULL_TREE;
  1091. if (type_name != NULL_TREE && TREE_CODE (type_name) == IDENTIFIER_NODE)
  1092. id = type_name;
  1093. else if (type_name != NULL_TREE && TREE_CODE (type_name) == TYPE_DECL
  1094. && DECL_SOURCE_LOCATION (type_name) != BUILTINS_LOCATION
  1095. && DECL_NAME (type_name))
  1096. id = DECL_NAME (type_name);
  1097. if (id != NULL_TREE
  1098. && (!htab_find_slot (container->type_hash, IDENTIFIER_POINTER (id),
  1099. NO_INSERT)
  1100. || htab_find_slot (container->invalid_hash, IDENTIFIER_POINTER (id),
  1101. NO_INSERT)))
  1102. id = NULL_TREE;
  1103. if (id != NULL_TREE)
  1104. {
  1105. struct obstack *ob;
  1106. ob = &container->type_obstack;
  1107. obstack_1grow (ob, '_');
  1108. go_append_string (ob, id);
  1109. is_valid = htab_find_slot (container->type_hash, IDENTIFIER_POINTER (id),
  1110. NO_INSERT) != NULL;
  1111. }
  1112. else
  1113. is_valid = go_format_type (container, TREE_TYPE (decl), true, false, NULL,
  1114. false);
  1115. if (is_valid
  1116. && htab_find_slot (container->type_hash,
  1117. IDENTIFIER_POINTER (DECL_NAME (decl)),
  1118. NO_INSERT) != NULL)
  1119. {
  1120. /* There is already a type with this name, probably from a
  1121. struct tag. Prefer the type to the variable. */
  1122. is_valid = false;
  1123. }
  1124. if (!is_valid)
  1125. fprintf (go_dump_file, "// ");
  1126. fprintf (go_dump_file, "var _%s ",
  1127. IDENTIFIER_POINTER (DECL_NAME (decl)));
  1128. go_output_type (container);
  1129. fprintf (go_dump_file, "\n");
  1130. /* Sometimes an extern variable is declared with an unknown struct
  1131. type. */
  1132. if (type_name != NULL_TREE && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
  1133. {
  1134. if (TREE_CODE (type_name) == IDENTIFIER_NODE)
  1135. container->pot_dummy_types.add (IDENTIFIER_POINTER (type_name));
  1136. else if (TREE_CODE (type_name) == TYPE_DECL)
  1137. container->pot_dummy_types.add
  1138. (IDENTIFIER_POINTER (DECL_NAME (type_name)));
  1139. }
  1140. }
  1141. /* Output the final value of a preprocessor macro or enum constant.
  1142. This is called via htab_traverse_noresize. */
  1143. static int
  1144. go_print_macro (void **slot, void *arg ATTRIBUTE_UNUSED)
  1145. {
  1146. struct macro_hash_value *mhval = (struct macro_hash_value *) *slot;
  1147. fprintf (go_dump_file, "const _%s = %s\n", mhval->name, mhval->value);
  1148. return 1;
  1149. }
  1150. /* Build a hash table with the Go keywords. */
  1151. static const char * const keywords[] = {
  1152. "__asm__", "break", "case", "chan", "const", "continue", "default",
  1153. "defer", "else", "fallthrough", "for", "func", "go", "goto", "if",
  1154. "import", "interface", "map", "package", "range", "return", "select",
  1155. "struct", "switch", "type", "var"
  1156. };
  1157. static void
  1158. keyword_hash_init (struct godump_container *container)
  1159. {
  1160. size_t i;
  1161. size_t count = sizeof (keywords) / sizeof (keywords[0]);
  1162. void **slot;
  1163. for (i = 0; i < count; i++)
  1164. {
  1165. slot = htab_find_slot (container->keyword_hash, keywords[i], INSERT);
  1166. *slot = CONST_CAST (void *, (const void *) keywords[i]);
  1167. }
  1168. }
  1169. /* Traversing the pot_dummy_types and seeing which types are present
  1170. in the global types hash table and creating dummy definitions if
  1171. not found. This function is invoked by hash_set::traverse. */
  1172. bool
  1173. find_dummy_types (const char *const &ptr, godump_container *adata)
  1174. {
  1175. struct godump_container *data = (struct godump_container *) adata;
  1176. const char *type = (const char *) ptr;
  1177. void **slot;
  1178. void **islot;
  1179. slot = htab_find_slot (data->type_hash, type, NO_INSERT);
  1180. islot = htab_find_slot (data->invalid_hash, type, NO_INSERT);
  1181. if (slot == NULL || islot != NULL)
  1182. fprintf (go_dump_file, "type _%s struct {}\n", type);
  1183. return true;
  1184. }
  1185. /* Output symbols. */
  1186. static void
  1187. go_finish (const char *filename)
  1188. {
  1189. struct godump_container container;
  1190. unsigned int ix;
  1191. tree decl;
  1192. real_debug_hooks->finish (filename);
  1193. container.type_hash = htab_create (100, htab_hash_string,
  1194. string_hash_eq, NULL);
  1195. container.invalid_hash = htab_create (10, htab_hash_string,
  1196. string_hash_eq, NULL);
  1197. container.keyword_hash = htab_create (50, htab_hash_string,
  1198. string_hash_eq, NULL);
  1199. obstack_init (&container.type_obstack);
  1200. keyword_hash_init (&container);
  1201. FOR_EACH_VEC_SAFE_ELT (queue, ix, decl)
  1202. {
  1203. switch (TREE_CODE (decl))
  1204. {
  1205. case FUNCTION_DECL:
  1206. go_output_fndecl (&container, decl);
  1207. break;
  1208. case TYPE_DECL:
  1209. go_output_typedef (&container, decl);
  1210. break;
  1211. case VAR_DECL:
  1212. go_output_var (&container, decl);
  1213. break;
  1214. default:
  1215. gcc_unreachable ();
  1216. }
  1217. }
  1218. htab_traverse_noresize (macro_hash, go_print_macro, NULL);
  1219. /* To emit dummy definitions. */
  1220. container.pot_dummy_types.traverse<godump_container *, find_dummy_types>
  1221. (&container);
  1222. htab_delete (container.type_hash);
  1223. htab_delete (container.invalid_hash);
  1224. htab_delete (container.keyword_hash);
  1225. obstack_free (&container.type_obstack, NULL);
  1226. vec_free (queue);
  1227. if (fclose (go_dump_file) != 0)
  1228. error ("could not close Go dump file: %m");
  1229. go_dump_file = NULL;
  1230. }
  1231. /* Set up our hooks. */
  1232. const struct gcc_debug_hooks *
  1233. dump_go_spec_init (const char *filename, const struct gcc_debug_hooks *hooks)
  1234. {
  1235. go_dump_file = fopen (filename, "w");
  1236. if (go_dump_file == NULL)
  1237. {
  1238. error ("could not open Go dump file %qs: %m", filename);
  1239. return hooks;
  1240. }
  1241. go_debug_hooks = *hooks;
  1242. real_debug_hooks = hooks;
  1243. go_debug_hooks.finish = go_finish;
  1244. go_debug_hooks.define = go_define;
  1245. go_debug_hooks.undef = go_undef;
  1246. go_debug_hooks.function_decl = go_function_decl;
  1247. go_debug_hooks.global_decl = go_global_decl;
  1248. go_debug_hooks.type_decl = go_type_decl;
  1249. macro_hash = htab_create (100, macro_hash_hashval, macro_hash_eq,
  1250. macro_hash_del);
  1251. return &go_debug_hooks;
  1252. }
  1253. #include "gt-godump.h"