GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/type_special.c Lines: 33 33 100.0 %
Date: 2020-09-21 18:02:52 Branches: 10 10 100.0 %

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 "gwion.h"
6
7
static m_str const special_name[] = { "^nonnull", "^force" };
8
#define SPECIAL_LEN strlen(special_name[0]) + strlen(special_name[1])
9
static const ae_flag special_flag[] = { ae_flag_nonnull, ae_flag_force };
10
11
typedef struct {
12
  const Type    type;
13
  Symbol  name;
14
  ae_flag flag;
15
  uint st_type;
16
} SpecialType;
17
18
19
4327
ANN static Type specialtype_create(const Env env, const SpecialType *s) {
20
4327
  const Type t = type_copy(env->gwion->mp, s->type);
21
4327
  if(t->nspc)
22
4281
    ADD_REF(t->nspc)
23
4327
  t->name = s_name(s->name);
24
4327
  t->flag = s->type->flag | s->flag;
25
4327
  t->e->parent = unflag_type(s->type);
26
4327
  nspc_add_type_front(s->type->e->owner, s->name, t);
27
4327
  mk_class(env, t);
28
4327
  return t;
29
}
30
31
17901
static inline int get_flag(const Type t, const ae_flag flag) {
32
17901
  return (t->flag & flag) == flag;
33
}
34
35
36
35802
ANN static void specialtype_flag(SpecialType *s, m_str c, const uint i) {
37
35802
  const ae_flag flag = special_flag[i];
38

35802
  if(i == s->st_type || get_flag(s->type, flag)) {
39
17906
    strcat(c, special_name[i]);
40
17906
    s->flag |= flag;
41
  }
42
35802
}
43
44
17901
ANN static void specialtype_init(SymTable *st, SpecialType *s) {
45
17901
  const size_t sz = strlen(s->type->name);
46
17901
  char c[sz + SPECIAL_LEN + 1];
47
17901
  strcpy(c, s->type->name);
48
17901
  m_str flagged = strchr(c, '^');
49
17901
  if(flagged)
50
5
    *flagged = '\0';
51
17901
  specialtype_flag(s, c, 0);
52
17901
  specialtype_flag(s, c, 1);
53
17901
  s->name = insert_symbol(st, c);
54
17901
}
55
56
17901
ANN Type special_type(const Env env, const Type t, const uint st_type) {
57
17901
  SpecialType s = { .type=t, .st_type=st_type };
58
17901
  specialtype_init(env->gwion->st, &s);
59
17901
  return nspc_lookup_type1(t->e->owner, s.name) ?:
60
    specialtype_create(env, &s);
61
}