GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/import/enum.c Lines: 51 51 100.0 %
Date: 2020-08-07 19:15:19 Branches: 23 30 76.7 %

Line Branch Exec Source
1
//! \file enum.c
2
//! \brief enumerations utils
3
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
//! start an enum definition
18
//! \arg the importer
19
//! \arg string defining a primitive type
20
//! why is return type m_int ?
21
738
ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type) {
22
738
  CHECK_BB(ck_ini(gwi, ck_edef))
23
737
  if(type)
24
//    CHECK_OB((gwi->ck->sym = str2sym(gwi, type)))
25
733
    CHECK_OB((gwi->ck->xid = str2sym(gwi, type)))
26
737
  vector_init(&gwi->ck->v);
27
737
  return GW_OK;
28
}
29
30
// adds the id_list to the enum
31
// change that algo?
32
1503
ANN static void add2list(struct ImportCK* ck, const ID_List list) {
33
1503
  if(!ck->tmpl)
34
737
    ck->tmpl = list;
35
  else
36
766
    ck->curr->next = list;
37
1503
  ck->curr = list;
38
1503
}
39
/*
40
void Append(DL_Enum* d, const ID_List list) {
41
  List* next = &d->base;
42
  while (*next != NULL) next = &(*next)->Next;
43
  *next = list;
44
  next->next = NULL;
45
}
46
*/
47
//! add an enum entry
48
//! \arg the importer
49
//! \arg name of the entry
50
//! TODO: change return type to m_bool
51
1504
ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) {
52
1504
  CHECK_BB(ck_ok(gwi, ck_edef))
53
1503
  DECL_OB(const ID_List, list, = str2symlist(gwi, name))
54
1503
  add2list(gwi->ck, list);
55
1503
  ALLOC_PTR(gwi->gwion->mp, addr, m_int, i);
56
1503
  vector_add(&gwi->ck->v, (vtype)addr);
57
1503
  return GW_OK;
58
}
59
60
//! set enum values
61
//! \arg the importer
62
//! \arg a vector of values
63
//! \note [internal]
64
736
ANN static void import_enum_end(const Gwi gwi, const Vector v) {
65
736
  ImportCK *ck = gwi->ck;
66
2237
  for(m_uint i = 0; i < vector_size(v); i++) {
67
1501
    const Value value = (Value)vector_at(v, i);
68
1501
    const m_uint addr = vector_at(&ck->v, i);
69
1501
    SET_FLAG(value, builtin);
70
//    ADD_REF(value->type); // what ?
71
1501
    if(!gwi->gwion->env->class_def)
72
1481
      value->d.ptr = (m_uint*)(addr ? addr : i);
73
    else
74
20
      value->d.ptr = (m_uint*)(addr ? *(m_uint*)addr : i);
75
  }
76
  // better clean ?
77
736
}
78
79
//! finish enum import
80
//! \arg the importer
81
//! TODO: check what happens in inside template class
82
736
ANN Type gwi_enum_end(const Gwi gwi) {
83
736
  CHECK_BO(ck_ok(gwi, ck_edef))
84
736
  const Gwion gwion = gwi->gwion;
85
2208
  const Enum_Def edef  = new_enum_def(gwion->mp, gwi->ck->tmpl,
86
1472
      gwi->ck->xid, loc(gwi));
87
736
  gwi->ck->tmpl = NULL;
88
736
  const m_bool ret = traverse_enum_def(gwion->env, edef);
89
736
  import_enum_end(gwi, &edef->values);
90
736
  const Type t = ret > 0 ? edef->t : NULL;
91
736
  free_enum_def(gwion->mp, edef);
92
736
  vector_release(&gwi->ck->v);
93
736
  gwi->ck->v.ptr = NULL;
94
736
  ck_end(gwi);
95
736
  return t;
96
}
97
98
1
ANN void ck_clean_edef(MemPool mp, ImportCK *ck) {
99
1
  if(ck->tmpl)
100
1
    free_id_list(mp, ck->tmpl);
101
1
  if(ck->v.ptr) {
102
2
    for(m_uint i = 0; i < vector_size(&ck->v); ++i)
103
1
      mp_free2(mp, SZ_INT, (m_uint*)vector_at(&ck->v, i));
104
1
    vector_release(&ck->v);
105
  }
106
1
}
107