decl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "../util/cf-parser.h"
  17. #include "decl.h"
  18. static inline void err_specifier_exists(struct cf_parser *cfp, const char *storage)
  19. {
  20. cf_adderror(cfp, "'$1' specifier already exists", LEX_ERROR, storage, NULL, NULL);
  21. }
  22. static inline void err_reserved_name(struct cf_parser *cfp, const char *name)
  23. {
  24. cf_adderror(cfp, "'$1' is a reserved name", LEX_ERROR, name, NULL, NULL);
  25. }
  26. static inline void err_existing_name(struct cf_parser *cfp, const char *name)
  27. {
  28. cf_adderror(cfp, "'$1' already exists", LEX_ERROR, name, NULL, NULL);
  29. }
  30. static bool is_in_out_specifier(struct cf_parser *cfp, struct strref *name, uint32_t *type)
  31. {
  32. if (strref_cmp(name, "in") == 0) {
  33. if (*type & CALL_PARAM_IN)
  34. err_specifier_exists(cfp, "in");
  35. *type |= CALL_PARAM_IN;
  36. } else if (strref_cmp(name, "out") == 0) {
  37. if (*type & CALL_PARAM_OUT)
  38. err_specifier_exists(cfp, "out");
  39. *type |= CALL_PARAM_OUT;
  40. } else {
  41. return false;
  42. }
  43. return true;
  44. }
  45. #define TYPE_OR_STORAGE "type or storage specifier"
  46. static bool get_type(struct strref *ref, enum call_param_type *type, bool is_return)
  47. {
  48. if (strref_cmp(ref, "int") == 0)
  49. *type = CALL_PARAM_TYPE_INT;
  50. else if (strref_cmp(ref, "float") == 0)
  51. *type = CALL_PARAM_TYPE_FLOAT;
  52. else if (strref_cmp(ref, "bool") == 0)
  53. *type = CALL_PARAM_TYPE_BOOL;
  54. else if (strref_cmp(ref, "ptr") == 0)
  55. *type = CALL_PARAM_TYPE_PTR;
  56. else if (strref_cmp(ref, "string") == 0)
  57. *type = CALL_PARAM_TYPE_STRING;
  58. else if (is_return && strref_cmp(ref, "void") == 0)
  59. *type = CALL_PARAM_TYPE_VOID;
  60. else
  61. return false;
  62. return true;
  63. }
  64. static bool is_reserved_name(const char *str)
  65. {
  66. return (strcmp(str, "int") == 0) || (strcmp(str, "float") == 0) || (strcmp(str, "bool") == 0) ||
  67. (strcmp(str, "ptr") == 0) || (strcmp(str, "string") == 0) || (strcmp(str, "void") == 0) ||
  68. (strcmp(str, "return") == 0);
  69. }
  70. static bool name_exists(struct decl_info *decl, const char *name)
  71. {
  72. for (size_t i = 0; i < decl->params.num; i++) {
  73. const char *param_name = decl->params.array[i].name;
  74. if (strcmp(name, param_name) == 0)
  75. return true;
  76. }
  77. return false;
  78. }
  79. static int parse_param(struct cf_parser *cfp, struct decl_info *decl)
  80. {
  81. struct strref ref;
  82. int code;
  83. struct decl_param param = {0};
  84. /* get storage specifiers */
  85. code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
  86. if (code != PARSE_SUCCESS)
  87. return code;
  88. while (is_in_out_specifier(cfp, &ref, &param.flags)) {
  89. code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
  90. if (code != PARSE_SUCCESS)
  91. return code;
  92. }
  93. /* parameters not marked with specifiers are input parameters */
  94. if (param.flags == 0)
  95. param.flags = CALL_PARAM_IN;
  96. if (!get_type(&ref, &param.type, false)) {
  97. cf_adderror_expecting(cfp, "type");
  98. cf_go_to_token(cfp, ",", ")");
  99. return PARSE_CONTINUE;
  100. }
  101. /* name */
  102. code = cf_next_name(cfp, &param.name, "parameter name", ",");
  103. if (code != PARSE_SUCCESS)
  104. return code;
  105. if (name_exists(decl, param.name))
  106. err_existing_name(cfp, param.name);
  107. if (is_reserved_name(param.name))
  108. err_reserved_name(cfp, param.name);
  109. da_push_back(decl->params, &param);
  110. return PARSE_SUCCESS;
  111. }
  112. static void parse_params(struct cf_parser *cfp, struct decl_info *decl)
  113. {
  114. struct cf_token peek;
  115. int code;
  116. if (!cf_peek_valid_token(cfp, &peek))
  117. return;
  118. while (peek.type == CFTOKEN_NAME) {
  119. code = parse_param(cfp, decl);
  120. if (code == PARSE_EOF)
  121. return;
  122. if (code != PARSE_CONTINUE && !cf_next_valid_token(cfp))
  123. return;
  124. if (cf_token_is(cfp, ")"))
  125. break;
  126. else if (cf_token_should_be(cfp, ",", ",", NULL) == PARSE_EOF)
  127. return;
  128. if (!cf_peek_valid_token(cfp, &peek))
  129. return;
  130. }
  131. if (!cf_token_is(cfp, ")"))
  132. cf_next_token_should_be(cfp, ")", NULL, NULL);
  133. }
  134. static void print_errors(struct cf_parser *cfp, const char *decl_string)
  135. {
  136. char *errors = error_data_buildstring(&cfp->error_list);
  137. if (errors) {
  138. blog(LOG_WARNING, "Errors/warnings for '%s':\n\n%s", decl_string, errors);
  139. bfree(errors);
  140. }
  141. }
  142. bool parse_decl_string(struct decl_info *decl, const char *decl_string)
  143. {
  144. struct cf_parser cfp;
  145. struct strref ret_type;
  146. struct decl_param ret_param = {0};
  147. int code;
  148. bool success = false;
  149. decl->decl_string = decl_string;
  150. ret_param.flags = CALL_PARAM_OUT;
  151. cf_parser_init(&cfp);
  152. if (!cf_parser_parse(&cfp, decl_string, "declaration"))
  153. goto fail;
  154. code = cf_get_name_ref(&cfp, &ret_type, "return type", NULL);
  155. if (code == PARSE_EOF)
  156. goto fail;
  157. if (!get_type(&ret_type, &ret_param.type, true))
  158. cf_adderror_expecting(&cfp, "return type");
  159. code = cf_next_name(&cfp, &decl->name, "function name", "(");
  160. if (code == PARSE_EOF)
  161. goto fail;
  162. if (is_reserved_name(decl->name))
  163. err_reserved_name(&cfp, decl->name);
  164. code = cf_next_token_should_be(&cfp, "(", "(", NULL);
  165. if (code == PARSE_EOF)
  166. goto fail;
  167. parse_params(&cfp, decl);
  168. success = true;
  169. fail:
  170. if (error_data_has_errors(&cfp.error_list))
  171. success = false;
  172. if (success && ret_param.type != CALL_PARAM_TYPE_VOID) {
  173. ret_param.name = bstrdup("return");
  174. da_push_back(decl->params, &ret_param);
  175. }
  176. if (!success)
  177. decl_info_free(decl);
  178. print_errors(&cfp, decl_string);
  179. cf_parser_free(&cfp);
  180. return success;
  181. }