parser_typedec.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdio.h>
  2. #include "parser_internal.h"
  3. void cp_ctx_destroy_state(stack_state_t* st) {
  4. VEC_FREE(&st->type_specs);
  5. VEC_FREE(&st->type_names);
  6. VEC_FREE(&st->idents);
  7. VEC_FREE(&st->array_dims);
  8. st->id = 0;
  9. }
  10. ast_type_t* cp_ctx_process_type(cp_ctx_t* ctx) {
  11. ast_type_t* t = calloc(1, sizeof(*t));
  12. #define X(a, ...) int has_##a = 0;
  13. PARSER_TYPESPEC_LIST(X)
  14. X(struct)
  15. X(enum)
  16. X(union)
  17. #undef X
  18. VEC_EACH(&ctx->state->type_specs, i, tok) {
  19. #define X(a, ...) if(tok->text == ctx->_##a) has_##a++;
  20. PARSER_TYPESPEC_LIST(X)
  21. X(struct)
  22. X(enum)
  23. X(union)
  24. #undef X
  25. #define X(a, ...) if(tok->text == ctx->_##a) t->specs |= TYPESPEC_##a;
  26. PARSER_TYPESPEC_LIST(X)
  27. #undef X
  28. }
  29. // TODO: check duplicates, invalid combos
  30. ast_typename_t* typename = NULL;
  31. if(VEC_LEN(&ctx->state->type_names) > 1) {
  32. printf("too many type names\n");
  33. }
  34. else if(VEC_LEN(&ctx->state->type_names) == 0) {
  35. if(has_union || has_struct) {
  36. printf("%s type\n", has_struct ? "struct" : "union");
  37. }
  38. else {
  39. printf("missing type name\n");
  40. }
  41. }
  42. else {
  43. typename = &VEC_ITEM(&ctx->state->type_names, 0);
  44. t->type_name = typename->name;
  45. }
  46. return t;
  47. }