GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/parse/type_decl.c Lines: 30 32 93.8 %
Date: 2020-10-03 11:19:27 Branches: 28 30 93.3 %

Line Branch Exec Source
1
#include "gwion_util.h"
2
#include "gwion_ast.h"
3
#include "gwion_env.h"
4
#include "vm.h"
5
#include "traverse.h"
6
#include "parse.h"
7
8
9
438681
ANN static Type resolve(const Env env, Type_Decl* td) {
10
438681
  DECL_OO(const Type, base, = find_type(env, td))
11

438653
  if(base->e->ctx && base->e->ctx->error)
12
    ERR_O(td_pos(td), _("type '%s' is invalid"), base->name)
13
438653
  DECL_OO(const Type, t, = scan_type(env, base, td))
14
438646
  const Type ret = !td->array ? t : array_type(env, t, td->array->depth);
15
438646
  if(GET_FLAG(td, nonnull)) {
16
17860
    if(isa(ret, env->gwion->type[et_void]) > 0)
17
1
      ERR_O(td_pos(td), _("void types can't be nonnull."))
18

17859
    if(isa(ret, env->gwion->type[et_object]) < 0 && isa(ret, env->gwion->type[et_fptr]) < 0)
19
      return ret;
20
17859
    return nonnul_type(env, ret);
21
  }
22
420786
  return ret;
23
}
24
25
struct td_info {
26
  Type_List tl;
27
  GwText text;
28
};
29
30
158
ANN static m_bool td_info_run(const Env env, struct td_info* info) {
31
158
  Type_List tl = info->tl;
32
  do {
33
164
    DECL_OB(const Type,  t, = known_type(env, tl->td))
34
163
    text_add(&info->text, t->name);
35
163
    if(tl->next)
36
6
      text_add(&info->text, ",");
37
163
  } while((tl = tl->next));
38
157
  return GW_OK;
39
}
40
41
158
ANEW ANN m_str tl2str(const Env env, Type_List tl) {
42
158
  struct td_info info = { .tl=tl, { .mp=env->gwion->mp} };
43
158
  CHECK_BO(td_info_run(env, &info))
44
157
  return info.text.str;
45
}
46
47
36
ANN static inline void* type_unknown(const Env env, const Type_Decl* td) {
48
36
  env_err(env, td->pos, _("unknown type '%s'"), s_name(td->xid));
49
36
  return NULL;
50
}
51
52
438691
ANN Type known_type(const Env env, Type_Decl* td) {
53
438691
  if(!td->xid)
54
10
    return env->gwion->type[et_undefined];
55
438681
  return resolve(env, td) ?:type_unknown(env, td);
56
}