ctf-decl.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* C declarator syntax glue.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of libctf.
  4. libctf is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* CTF Declaration Stack
  16. In order to implement ctf_type_name(), we must convert a type graph back
  17. into a C type declaration. Unfortunately, a type graph represents a storage
  18. class ordering of the type whereas a type declaration must obey the C rules
  19. for operator precedence, and the two orderings are frequently in conflict.
  20. For example, consider these CTF type graphs and their C declarations:
  21. CTF_K_POINTER -> CTF_K_FUNCTION -> CTF_K_INTEGER : int (*)()
  22. CTF_K_POINTER -> CTF_K_ARRAY -> CTF_K_INTEGER : int (*)[]
  23. In each case, parentheses are used to raise operator * to higher lexical
  24. precedence, so the string form of the C declaration cannot be constructed by
  25. walking the type graph links and forming the string from left to right.
  26. The functions in this file build a set of stacks from the type graph nodes
  27. corresponding to the C operator precedence levels in the appropriate order.
  28. The code in ctf_type_name() can then iterate over the levels and nodes in
  29. lexical precedence order and construct the final C declaration string. */
  30. #include <ctf-impl.h>
  31. #include <string.h>
  32. void
  33. ctf_decl_init (ctf_decl_t *cd)
  34. {
  35. int i;
  36. memset (cd, 0, sizeof (ctf_decl_t));
  37. for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)
  38. cd->cd_order[i] = CTF_PREC_BASE - 1;
  39. cd->cd_qualp = CTF_PREC_BASE;
  40. cd->cd_ordp = CTF_PREC_BASE;
  41. }
  42. void
  43. ctf_decl_fini (ctf_decl_t *cd)
  44. {
  45. ctf_decl_node_t *cdp, *ndp;
  46. int i;
  47. for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)
  48. {
  49. for (cdp = ctf_list_next (&cd->cd_nodes[i]); cdp != NULL; cdp = ndp)
  50. {
  51. ndp = ctf_list_next (cdp);
  52. free (cdp);
  53. }
  54. }
  55. free (cd->cd_buf);
  56. }
  57. void
  58. ctf_decl_push (ctf_decl_t *cd, ctf_dict_t *fp, ctf_id_t type)
  59. {
  60. ctf_decl_node_t *cdp;
  61. ctf_decl_prec_t prec;
  62. uint32_t kind, n = 1;
  63. int is_qual = 0;
  64. const ctf_type_t *tp;
  65. ctf_arinfo_t ar;
  66. if ((tp = ctf_lookup_by_id (&fp, type)) == NULL)
  67. {
  68. cd->cd_err = fp->ctf_errno;
  69. return;
  70. }
  71. switch (kind = LCTF_INFO_KIND (fp, tp->ctt_info))
  72. {
  73. case CTF_K_ARRAY:
  74. (void) ctf_array_info (fp, type, &ar);
  75. ctf_decl_push (cd, fp, ar.ctr_contents);
  76. n = ar.ctr_nelems;
  77. prec = CTF_PREC_ARRAY;
  78. break;
  79. case CTF_K_TYPEDEF:
  80. if (ctf_strptr (fp, tp->ctt_name)[0] == '\0')
  81. {
  82. ctf_decl_push (cd, fp, tp->ctt_type);
  83. return;
  84. }
  85. prec = CTF_PREC_BASE;
  86. break;
  87. case CTF_K_FUNCTION:
  88. ctf_decl_push (cd, fp, tp->ctt_type);
  89. prec = CTF_PREC_FUNCTION;
  90. break;
  91. case CTF_K_POINTER:
  92. ctf_decl_push (cd, fp, tp->ctt_type);
  93. prec = CTF_PREC_POINTER;
  94. break;
  95. case CTF_K_SLICE:
  96. /* Slices themselves have no print representation and should not appear in
  97. the decl stack. */
  98. ctf_decl_push (cd, fp, ctf_type_reference (fp, type));
  99. return;
  100. case CTF_K_VOLATILE:
  101. case CTF_K_CONST:
  102. case CTF_K_RESTRICT:
  103. ctf_decl_push (cd, fp, tp->ctt_type);
  104. prec = cd->cd_qualp;
  105. is_qual++;
  106. break;
  107. default:
  108. prec = CTF_PREC_BASE;
  109. }
  110. if ((cdp = malloc (sizeof (ctf_decl_node_t))) == NULL)
  111. {
  112. cd->cd_err = EAGAIN;
  113. return;
  114. }
  115. cdp->cd_type = type;
  116. cdp->cd_kind = kind;
  117. cdp->cd_n = n;
  118. if (ctf_list_next (&cd->cd_nodes[prec]) == NULL)
  119. cd->cd_order[prec] = cd->cd_ordp++;
  120. /* Reset cd_qualp to the highest precedence level that we've seen so
  121. far that can be qualified (CTF_PREC_BASE or CTF_PREC_POINTER). */
  122. if (prec > cd->cd_qualp && prec < CTF_PREC_ARRAY)
  123. cd->cd_qualp = prec;
  124. /* By convention qualifiers of base types precede the type specifier (e.g.
  125. const int vs. int const) even though the two forms are equivalent. */
  126. if (is_qual && prec == CTF_PREC_BASE)
  127. ctf_list_prepend (&cd->cd_nodes[prec], cdp);
  128. else
  129. ctf_list_append (&cd->cd_nodes[prec], cdp);
  130. }
  131. _libctf_printflike_ (2, 3)
  132. void ctf_decl_sprintf (ctf_decl_t *cd, const char *format, ...)
  133. {
  134. va_list ap;
  135. char *str;
  136. int n;
  137. if (cd->cd_enomem)
  138. return;
  139. va_start (ap, format);
  140. n = vasprintf (&str, format, ap);
  141. va_end (ap);
  142. if (n > 0)
  143. {
  144. char *newbuf;
  145. if ((newbuf = ctf_str_append (cd->cd_buf, str)) != NULL)
  146. cd->cd_buf = newbuf;
  147. }
  148. /* Sticky error condition. */
  149. if (n < 0 || cd->cd_buf == NULL)
  150. {
  151. free (cd->cd_buf);
  152. cd->cd_buf = NULL;
  153. cd->cd_enomem = 1;
  154. }
  155. free (str);
  156. }
  157. char *ctf_decl_buf (ctf_decl_t *cd)
  158. {
  159. char *buf = cd->cd_buf;
  160. cd->cd_buf = NULL;
  161. return buf;
  162. }