1
0

decl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, storage,
  22. 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, name, NULL,
  27. 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) || (strcmp(str, "float") == 0) ||
  72. (strcmp(str, "bool") == 0) || (strcmp(str, "ptr") == 0) ||
  73. (strcmp(str, "string") == 0) || (strcmp(str, "void") == 0) ||
  74. (strcmp(str, "return") == 0);
  75. }
  76. static bool name_exists(struct decl_info *decl, const char *name)
  77. {
  78. for (size_t i = 0; i < decl->params.num; i++) {
  79. const char *param_name = decl->params.array[i].name;
  80. if (strcmp(name, param_name) == 0)
  81. return true;
  82. }
  83. return false;
  84. }
  85. static int parse_param(struct cf_parser *cfp, struct decl_info *decl)
  86. {
  87. struct strref ref;
  88. int code;
  89. struct decl_param param = {0};
  90. /* get storage specifiers */
  91. code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
  92. if (code != PARSE_SUCCESS)
  93. return code;
  94. while (is_in_out_specifier(cfp, &ref, &param.flags)) {
  95. code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
  96. if (code != PARSE_SUCCESS)
  97. return code;
  98. }
  99. /* parameters not marked with specifiers are input parameters */
  100. if (param.flags == 0)
  101. param.flags = CALL_PARAM_IN;
  102. if (!get_type(&ref, &param.type, false)) {
  103. cf_adderror_expecting(cfp, "type");
  104. cf_go_to_token(cfp, ",", ")");
  105. return PARSE_CONTINUE;
  106. }
  107. /* name */
  108. code = cf_next_name(cfp, &param.name, "parameter name", ",");
  109. if (code != PARSE_SUCCESS)
  110. return code;
  111. if (name_exists(decl, param.name))
  112. err_existing_name(cfp, param.name);
  113. if (is_reserved_name(param.name))
  114. err_reserved_name(cfp, param.name);
  115. da_push_back(decl->params, &param);
  116. return PARSE_SUCCESS;
  117. }
  118. static void parse_params(struct cf_parser *cfp, struct decl_info *decl)
  119. {
  120. struct cf_token peek;
  121. int code;
  122. if (!cf_peek_valid_token(cfp, &peek))
  123. return;
  124. while (peek.type == CFTOKEN_NAME) {
  125. code = parse_param(cfp, decl);
  126. if (code == PARSE_EOF)
  127. return;
  128. if (code != PARSE_CONTINUE && !cf_next_valid_token(cfp))
  129. return;
  130. if (cf_token_is(cfp, ")"))
  131. break;
  132. else if (cf_token_should_be(cfp, ",", ",", NULL) == PARSE_EOF)
  133. return;
  134. if (!cf_peek_valid_token(cfp, &peek))
  135. return;
  136. }
  137. if (!cf_token_is(cfp, ")"))
  138. cf_next_token_should_be(cfp, ")", NULL, NULL);
  139. }
  140. static void print_errors(struct cf_parser *cfp, const char *decl_string)
  141. {
  142. char *errors = error_data_buildstring(&cfp->error_list);
  143. if (errors) {
  144. blog(LOG_WARNING, "Errors/warnings for '%s':\n\n%s",
  145. decl_string, errors);
  146. bfree(errors);
  147. }
  148. }
  149. bool parse_decl_string(struct decl_info *decl, const char *decl_string)
  150. {
  151. struct cf_parser cfp;
  152. struct strref ret_type;
  153. struct decl_param ret_param = {0};
  154. int code;
  155. bool success;
  156. decl->decl_string = decl_string;
  157. ret_param.flags = CALL_PARAM_OUT;
  158. cf_parser_init(&cfp);
  159. if (!cf_parser_parse(&cfp, decl_string, "declaration"))
  160. goto fail;
  161. code = cf_get_name_ref(&cfp, &ret_type, "return type", NULL);
  162. if (code == PARSE_EOF)
  163. goto fail;
  164. if (!get_type(&ret_type, &ret_param.type, true))
  165. cf_adderror_expecting(&cfp, "return type");
  166. code = cf_next_name(&cfp, &decl->name, "function name", "(");
  167. if (code == PARSE_EOF)
  168. goto fail;
  169. if (is_reserved_name(decl->name))
  170. err_reserved_name(&cfp, decl->name);
  171. code = cf_next_token_should_be(&cfp, "(", "(", NULL);
  172. if (code == PARSE_EOF)
  173. goto fail;
  174. parse_params(&cfp, decl);
  175. fail:
  176. success = !error_data_has_errors(&cfp.error_list);
  177. if (success && ret_param.type != CALL_PARAM_TYPE_VOID) {
  178. ret_param.name = bstrdup("return");
  179. da_push_back(decl->params, &ret_param);
  180. }
  181. if (!success)
  182. decl_info_free(decl);
  183. print_errors(&cfp, decl_string);
  184. cf_parser_free(&cfp);
  185. return success;
  186. }