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

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