GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/import/import_udef.c Lines: 60 60 100.0 %
Date: 2020-09-22 13:02:15 Branches: 44 50 88.0 %

Line Branch Exec Source
1
#include <stdlib.h>
2
#include <string.h>
3
#include <ctype.h>
4
#include "gwion_util.h"
5
#include "gwion_ast.h"
6
#include "gwion_env.h"
7
#include "vm.h"
8
#include "traverse.h"
9
#include "instr.h"
10
#include "object.h"
11
#include "emit.h"
12
#include "gwion.h"
13
#include "operator.h"
14
#include "import.h"
15
#include "gwi.h"
16
17
// move me
18
22112
ANN Exp make_exp(const Gwi gwi, const m_str type, const m_str name) {
19
22112
  DECL_OO(Type_Decl*, td, = str2decl(gwi, type))
20
22108
  const Var_Decl_List vlist = str2varlist(gwi, name);
21
22108
  if(vlist)
22
22105
    return new_exp_decl(gwi->gwion->mp, td, vlist);
23
3
  free_type_decl(gwi->gwion->mp, td);
24
3
  return NULL;
25
}
26
27
11
ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str type, const m_str name) {
28
11
  CHECK_BB(ck_ini(gwi, ck_udef))
29
10
  if(name)
30
3
    CHECK_OB((gwi->ck->xid = str2sym(gwi, name)))
31
10
  gwi->ck->name = type;
32
10
  if(type)
33
5
    CHECK_BB(check_typename_def(gwi, gwi->ck))
34
10
  return GW_OK;
35
}
36
37
21
ANN m_int gwi_union_add(const Gwi gwi, const restrict m_str type, const restrict m_str name) {
38
21
  CHECK_BB(ck_ok(gwi, ck_udef))
39
21
  DECL_OB(const Exp, exp, = make_exp(gwi, type, name))
40
20
  SET_FLAG(exp->d.exp_decl.td, ref); // might not be needed
41
20
  gwi->ck->list = new_decl_list(gwi->gwion->mp, exp, gwi->ck->list);
42
20
  return GW_OK;
43
}
44
45
5
ANN static Type union_type(const Gwi gwi, const Union_Def udef) {
46
5
  CHECK_BO(scan0_union_def(gwi->gwion->env, udef))
47
5
  CHECK_BO(traverse_union_def(gwi->gwion->env, udef))
48
4
  if(!udef->tmpl)
49
3
    emit_union_offset(udef->l, udef->o);
50

4
  if(gwi->gwion->env->class_def && !GET_FLAG(udef, static))
51
2
      gwi->gwion->env->class_def->nspc->info->offset =
52
2
       udef->o + udef->s;
53

4
  if(udef->xid || !udef->type_xid) {
54
2
    SET_FLAG(udef->value, builtin);
55
2
    const M_Object o = new_object(gwi->gwion->mp, NULL, udef->value->type);
56
2
    udef->value->d.ptr = (m_uint*)o;
57
2
   return udef->value->type;
58
  }
59
2
  return udef->type;
60
}
61
62
8
ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag) {
63
8
  CHECK_BO(ck_ok(gwi, ck_udef))
64
8
  if(!gwi->ck->list)
65
1
    GWI_ERR_O(_("union is empty"));
66

7
  if(gwi->ck->tmpl && gwi->ck->xid)
67
2
    GWI_ERR_O(_("Template union type can't declare instance at declaration"));
68
5
  const Union_Def udef = new_union_def(gwi->gwion->mp, gwi->ck->list, loc(gwi));
69
5
  gwi->ck->list = NULL;
70
5
  udef->flag = flag;
71
5
  udef->xid = gwi->ck->xid;
72
5
  udef->type_xid = gwi->ck->sym;
73
5
  if(gwi->ck->tmpl) {
74
1
    udef->tmpl = gwi_tmpl(gwi);
75
1
    gwi->ck->tmpl = NULL;
76
  }
77
5
  const Type t = union_type(gwi, udef);
78

5
  if(!SAFE_FLAG(t, template))
79
4
    free_union_def(gwi->gwion->mp, udef);
80
5
  ck_end(gwi);
81
5
  return t;
82
}
83
84
5
ANN void ck_clean_udef(MemPool mp, ImportCK* ck) {
85
5
  if(ck->list)
86
3
    free_decl_list(mp, ck->list);
87
5
  if(ck->tmpl)
88
3
    free_id_list(mp, ck->tmpl);
89
5
}