decl.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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_WARNING,
  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 bool is_in_out_specifier(struct cf_parser *cfp, struct strref *name,
  30. 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,
  47. bool is_return)
  48. {
  49. if (strref_cmp(ref, "int") == 0)
  50. *type = CALL_PARAM_TYPE_INT;
  51. else if (strref_cmp(ref, "float") == 0)
  52. *type = CALL_PARAM_TYPE_FLOAT;
  53. else if (strref_cmp(ref, "bool") == 0)
  54. *type = CALL_PARAM_TYPE_BOOL;
  55. else if (strref_cmp(ref, "ptr") == 0)
  56. *type = CALL_PARAM_TYPE_PTR;
  57. else if (strref_cmp(ref, "string") == 0)
  58. *type = CALL_PARAM_TYPE_STRING;
  59. else if (is_return && strref_cmp(ref, "void") == 0)
  60. *type = CALL_PARAM_TYPE_VOID;
  61. else
  62. return false;
  63. return true;
  64. }
  65. static bool is_reserved_name(const char *str)
  66. {
  67. return (strcmp(str, "int") == 0) ||
  68. (strcmp(str, "float") == 0) ||
  69. (strcmp(str, "bool") == 0) ||
  70. (strcmp(str, "ptr") == 0) ||
  71. (strcmp(str, "string") == 0) ||
  72. (strcmp(str, "void") == 0) ||
  73. (strcmp(str, "return") == 0);
  74. }
  75. static int parse_param(struct cf_parser *cfp, struct decl_info *decl)
  76. {
  77. struct strref ref;
  78. int code;
  79. struct decl_param param = {0};
  80. /* get stprage specifiers */
  81. code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
  82. if (code != PARSE_SUCCESS)
  83. return code;
  84. while (is_in_out_specifier(cfp, &ref, &param.flags)) {
  85. code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
  86. if (code != PARSE_SUCCESS)
  87. return code;
  88. }
  89. /* parameters not marked with specifers are input parameters */
  90. if (param.flags == 0)
  91. param.flags = CALL_PARAM_IN;
  92. if (!get_type(&ref, &param.type, false)) {
  93. cf_adderror_expecting(cfp, "type");
  94. cf_go_to_token(cfp, ",", ")");
  95. return PARSE_CONTINUE;
  96. }
  97. /* name */
  98. code = cf_next_name(cfp, &param.name, "parameter name", ",");
  99. if (code != PARSE_SUCCESS)
  100. return code;
  101. if (is_reserved_name(param.name))
  102. err_reserved_name(cfp, param.name);
  103. da_push_back(decl->params, &param);
  104. return PARSE_SUCCESS;
  105. }
  106. static void parse_params(struct cf_parser *cfp, struct decl_info *decl)
  107. {
  108. struct cf_token peek;
  109. int code;
  110. if (!cf_peek_valid_token(cfp, &peek))
  111. return;
  112. while (peek.type == CFTOKEN_NAME) {
  113. code = parse_param(cfp, decl);
  114. if (code == PARSE_EOF)
  115. return;
  116. if (code != PARSE_CONTINUE && !cf_next_valid_token(cfp))
  117. return;
  118. if (cf_token_is(cfp, ")"))
  119. break;
  120. else if (cf_token_should_be(cfp, ",", ",", NULL) == PARSE_EOF)
  121. return;
  122. if (!cf_peek_valid_token(cfp, &peek))
  123. return;
  124. }
  125. if (!cf_token_is(cfp, ")"))
  126. cf_next_token_should_be(cfp, ")", NULL, NULL);
  127. }
  128. static void print_errors(struct cf_parser *cfp, const char *decl_string)
  129. {
  130. char *errors = error_data_buildstring(&cfp->error_list);
  131. if (errors) {
  132. blog(LOG_WARNING, "Errors/warnings for '%s':\n\n%s",
  133. decl_string, errors);
  134. bfree(errors);
  135. }
  136. }
  137. bool parse_decl_string(struct decl_info *decl, const char *decl_string)
  138. {
  139. struct cf_parser cfp;
  140. struct strref ret_type;
  141. struct decl_param ret_param = {0};
  142. int code;
  143. bool success;
  144. decl->decl_string = decl_string;
  145. ret_param.flags = CALL_PARAM_OUT;
  146. cf_parser_init(&cfp);
  147. if (!cf_parser_parse(&cfp, decl_string, "declaraion"))
  148. goto fail;
  149. code = cf_get_name_ref(&cfp, &ret_type, "return type", NULL);
  150. if (code == PARSE_EOF)
  151. goto fail;
  152. if (!get_type(&ret_type, &ret_param.type, true))
  153. cf_adderror_expecting(&cfp, "return type");
  154. code = cf_next_name(&cfp, &decl->name, "function name", "(");
  155. if (code == PARSE_EOF)
  156. goto fail;
  157. if (is_reserved_name(decl->name))
  158. err_reserved_name(&cfp, decl->name);
  159. code = cf_next_token_should_be(&cfp, "(", "(", NULL);
  160. if (code == PARSE_EOF)
  161. goto fail;
  162. parse_params(&cfp, decl);
  163. fail:
  164. success = !error_data_has_errors(&cfp.error_list);
  165. if (success && ret_param.type != CALL_PARAM_TYPE_VOID) {
  166. ret_param.name = bstrdup("return");
  167. da_push_back(decl->params, &ret_param);
  168. }
  169. if (!success)
  170. decl_info_free(decl);
  171. print_errors(&cfp, decl_string);
  172. cf_parser_free(&cfp);
  173. return success;
  174. }