GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/type_special.c Lines: 33 33 100.0 %
Date: 2020-08-07 19:15:19 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
4431
ANN static Type specialtype_create(const Env env, const SpecialType *s) {
20
4431
  const Type t = type_copy(env->gwion->mp, s->type);
21
4431
  if(t->nspc)
22
4388
    ADD_REF(t->nspc)
23
4431
  t->name = s_name(s->name);
24
4431
  t->flag = s->type->flag | s->flag;
25
4431
  t->e->parent = s->type;
26
4431
  nspc_add_type_front(s->type->e->owner, s->name, t);
27
4431
  mk_class(env, t);
28
4431
  return t;
29
}
30
31
17613
static inline int get_flag(const Type t, const ae_flag flag) {
32
17613
  return (t->flag & flag) == flag;
33
}
34
35
36
35226
ANN static void specialtype_flag(SpecialType *s, m_str c, const uint i) {
37
35226
  const ae_flag flag = special_flag[i];
38

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