1
0

decl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2014 Hugh 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,
  19. const char *storage)
  20. {
  21. cf_adderror(cfp, "'$1' specifier already exists", LEX_ERROR,
  22. storage, NULL, NULL);
  23. }
  24. static inline void err_reserved_name(struct cf_parser *cfp, const char *name)
  25. {
  26. cf_adderror(cfp, "'$1' is a reserved name", LEX_ERROR,
  27. name, NULL, NULL);
  28. }
  29. static inline void err_existing_name(struct cf_parser *cfp, const char *name)
  30. {
  31. cf_adderror(cfp, "'$1' already exists", LEX_ERROR, name, NULL, NULL);
  32. }
  33. static bool is_in_out_specifier(struct cf_parser *cfp, struct strref *name,
  34. uint32_t *type)
  35. {
  36. if (strref_cmp(name, "in") == 0) {
  37. if (*type & CALL_PARAM_IN)
  38. err_specifier_exists(cfp, "in");
  39. *type |= CALL_PARAM_IN;
  40. } else if (strref_cmp(name, "out") == 0) {
  41. if (*type & CALL_PARAM_OUT)
  42. err_specifier_exists(cfp, "out");
  43. *type |= CALL_PARAM_OUT;
  44. } else {
  45. return false;
  46. }
  47. return true;
  48. }
  49. #define TYPE_OR_STORAGE "type or storage specifier"
  50. static bool get_type(struct strref *ref, enum call_param_type *type,
  51. bool is_return)
  52. {
  53. if (strref_cmp(ref, "int") == 0)
  54. *type = CALL_PARAM_TYPE_INT;
  55. else if (strref_cmp(ref, "float") == 0)
  56. *type = CALL_PARAM_TYPE_FLOAT;
  57. else if (strref_cmp(ref, "bool") == 0)
  58. *type = CALL_PARAM_TYPE_BOOL;
  59. else if (strref_cmp(ref, "ptr") == 0)
  60. *type = CALL_PARAM_TYPE_PTR;
  61. else if (strref_cmp(ref, "string") == 0)
  62. *type = CALL_PARAM_TYPE_STRING;
  63. else if (is_return && strref_cmp(ref, "void") == 0)
  64. *type = CALL_PARAM_TYPE_VOID;
  65. else
  66. return false;
  67. return true;
  68. }
  69. static bool is_reserved_name(const char *str)
  70. {
  71. return (strcmp(str, "int") == 0) ||
  72. (strcmp(str, "float") == 0) ||
  73. (strcmp(str, "bool") == 0) ||
  74. (strcmp(str, "ptr") == 0) ||
  75. (strcmp(str, "string") == 0) ||
  76. (strcmp(str, "void") == 0) ||
  77. (strcmp(str, "return") == 0);
  78. }
  79. static bool name_exists(struct decl_info *decl, const char *name)
  80. {
  81. for (size_t i = 0; i < decl->params.num; i++) {
  82. const char *param_name = decl->params.array[i].name;
  83. if (strcmp(name, param_name) == 0)
  84. return true;
  85. }
  86. return false;
  87. }
  88. static int parse_param(struct cf_parser *cfp, struct decl_info *decl)
  89. {
  90. struct strref ref;
  91. int code;
  92. struct decl_param param = {0};
  93. /* get storage specifiers */
  94. code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
  95. if (code != PARSE_SUCCESS)
  96. return code;
  97. while (is_in_out_specifier(cfp, &ref, &param.flags)) {
  98. code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
  99. if (code != PARSE_SUCCESS)
  100. return code;
  101. }
  102. /* parameters not marked with specifiers are input parameters */
  103. if (param.flags == 0)
  104. param.flags = CALL_PARAM_IN;
  105. if (!get_type(&ref, &param.type, false)) {
  106. cf_adderror_expecting(cfp, "type");
  107. cf_go_to_token(cfp, ",", ")");
  108. return PARSE_CONTINUE;
  109. }
  110. /* name */
  111. code = cf_next_name(cfp, &param.name, "parameter name", ",");
  112. if (code != PARSE_SUCCESS)
  113. return code;
  114. if (name_exists(decl, param.name))
  115. err_existing_name(cfp, param.name);
  116. if (is_reserved_name(param.name))
  117. err_reserved_name(cfp, param.name);
  118. da_push_back(decl->params, &param);
  119. return PARSE_SUCCESS;
  120. }
  121. static void parse_params(struct cf_parser *cfp, struct decl_info *decl)
  122. {
  123. struct cf_token peek;
  124. int code;
  125. if (!cf_peek_valid_token(cfp, &peek))
  126. return;
  127. while (peek.type == CFTOKEN_NAME) {
  128. code = parse_param(cfp, decl);
  129. if (code == PARSE_EOF)
  130. return;
  131. if (code != PARSE_CONTINUE && !cf_next_valid_token(cfp))
  132. return;
  133. if (cf_token_is(cfp, ")"))
  134. break;
  135. else if (cf_token_should_be(cfp, ",", ",", NULL) == PARSE_EOF)
  136. return;
  137. if (!cf_peek_valid_token(cfp, &peek))
  138. return;
  139. }
  140. if (!cf_token_is(cfp, ")"))
  141. cf_next_token_should_be(cfp, ")", NULL, NULL);
  142. }
  143. static void print_errors(struct cf_parser *cfp, const char *decl_string)
  144. {
  145. char *errors = error_data_buildstring(&cfp->error_list);
  146. if (errors) {
  147. blog(LOG_WARNING, "Errors/warnings for '%s':\n\n%s",
  148. decl_string, errors);
  149. bfree(errors);
  150. }
  151. }
  152. bool parse_decl_string(struct decl_info *decl, const char *decl_string)
  153. {
  154. struct cf_parser cfp;
  155. struct strref ret_type;
  156. struct decl_param ret_param = {0};
  157. int code;
  158. bool success;
  159. decl->decl_string = decl_string;
  160. ret_param.flags = CALL_PARAM_OUT;
  161. cf_parser_init(&cfp);
  162. if (!cf_parser_parse(&cfp, decl_string, "declaration"))
  163. goto fail;
  164. code = cf_get_name_ref(&cfp, &ret_type, "return type", NULL);
  165. if (code == PARSE_EOF)
  166. goto fail;
  167. if (!get_type(&ret_type, &ret_param.type, true))
  168. cf_adderror_expecting(&cfp, "return type");
  169. code = cf_next_name(&cfp, &decl->name, "function name", "(");
  170. if (code == PARSE_EOF)
  171. goto fail;
  172. if (is_reserved_name(decl->name))
  173. err_reserved_name(&cfp, decl->name);
  174. code = cf_next_token_should_be(&cfp, "(", "(", NULL);
  175. if (code == PARSE_EOF)
  176. goto fail;
  177. parse_params(&cfp, decl);
  178. fail:
  179. success = !error_data_has_errors(&cfp.error_list);
  180. if (success && ret_param.type != CALL_PARAM_TYPE_VOID) {
  181. ret_param.name = bstrdup("return");
  182. da_push_back(decl->params, &ret_param);
  183. }
  184. if (!success)
  185. decl_info_free(decl);
  186. print_errors(&cfp, decl_string);
  187. cf_parser_free(&cfp);
  188. return success;
  189. }