cstyles.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* tripl/arch/cstyles.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * tripl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <klist.h>
  24. #include <tripl/tripl.h>
  25. #include <tripl/limits.h>
  26. #include <tripl/arch.h>
  27. /* compatible of c-style function defines */
  28. extern struct klist_node names_list;
  29. extern char *psave;
  30. const static char *c_keywords[] = {
  31. "char", "double", "enum", "float",
  32. "int", "long", "short", "signed",
  33. "struct", "union", "unsigned", "void",
  34. "for", "do", "while", "break",
  35. "continue",
  36. "if", "else", "goto",
  37. "switch", "case", "default",
  38. "return",
  39. "auto", "extern", "register", "static",
  40. "const", "sizeof", "typedef", "volatile",
  41. "inline", "restrict", "_Bool", "_Complex",
  42. "_Imaginary"
  43. };
  44. static int iskeyword(char *p, int l) {
  45. int i;
  46. for (i = 0; i < arrlen(c_keywords); i++)
  47. if (!strncmp(c_keywords[i], p, l))
  48. return 1;
  49. return 0;
  50. }
  51. __arch_templ int cstyle_declare(char *p, struct arch *arch_fn) {
  52. /* return-type function-name ( argument-type-1 argument-name, ...)
  53. *
  54. * and the preprocess is MUST done, so only keywords are before functions.
  55. * and an important thing is, we will do some check with the c-style declares. */
  56. struct klist_node *n;
  57. int type, out, nargs = 0;
  58. char *fname;
  59. /* get function name */
  60. _skip_spaces(p);
  61. for (;;) {
  62. getword(p, &type, &out);
  63. if (type == WORDTYPE_NAME) {
  64. if (iskeyword(p, out)) {
  65. p += out;
  66. _skip_spaces(p);
  67. continue;
  68. }
  69. }
  70. else if (type == WORDTYPE_KWORD)
  71. if (out == '*') {
  72. p++;
  73. _skip_spaces(p);
  74. continue;
  75. }
  76. /* now we found the name */
  77. break;
  78. }
  79. fname = strndup(p, out);
  80. p += out;
  81. _skip_spaces(p);
  82. getword(p, &type, &out);
  83. if (unlikely(type != WORDTYPE_KWORD || out != '('))
  84. /* not a c-style declare */
  85. return 0;
  86. p++;
  87. _skip_spaces(p);
  88. while (getword(p, &type, &out)) {
  89. switch (type) {
  90. case WORDTYPE_NAME:
  91. if (!iskeyword(p, out))
  92. nargs++;
  93. p += out;
  94. _skip_spaces(p);
  95. break;
  96. case WORDTYPE_KWORD:
  97. p++;
  98. _skip_spaces(p);
  99. break;
  100. case WORDTYPE_NR:
  101. default:
  102. exit(generic_error("invalid c-style declare: are you kidding me? ", psave, lno));
  103. }
  104. }
  105. /* join to the function-list */
  106. n = malloc(sizeof(struct klist_node));
  107. klist_init(n);
  108. if (!nargs)
  109. n->value = DECLARE_FN | DECLARE_NOARG;
  110. else
  111. n->value = DECLARE_FN | nargs;
  112. n->payload = fname;
  113. klist_add(&names_list, n);
  114. return 1;
  115. }